Skip to main content
Tweeted twitter.com/StackCodeGolf/status/751326822772641792
added 48 characters in body
Source Link

Given a string, return that string's "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is an integer, determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed a String and return a Number)

Edit: You can assume any input is lowercase.

Given a string, return that string's "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is an integer, determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed a String and return a Number)

Given a string, return that string's "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is an integer, determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed a String and return a Number)

Edit: You can assume any input is lowercase.

removed sorting aspect
Source Link

Sort strings by their "luck" Determine the "Luck" of a string

Given a list of stringsstring, return that list of strings sorted by theirstring's "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is an integer, determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

However, we were given the task to sort them, so...

sortByLuck(["lucky","memes","firetruck"])
>>["lucky","firetruck","memes"]

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed ana ArrayString ofand return a StringNumbers)

Sort strings by their "luck"

Given a list of strings, return that list of strings sorted by their "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

However, we were given the task to sort them, so...

sortByLuck(["lucky","memes","firetruck"])
>>["lucky","firetruck","memes"]

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed an Array of Strings)

Determine the "Luck" of a string

Given a string, return that string's "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is an integer, determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed a String and return a Number)

Source Link

Sort strings by their "luck"

Given a list of strings, return that list of strings sorted by their "luck".

A string's luck, as I completely just made up for the purpose of this challenge, is determined as so:

  • The base luck for a string is 1.
  • For each consecutive letter it shares with the word "lucky" (case insensitive), multiply the luck by 2. For instance, if your string was "lumberjack" or "smack" you'd multiply by 4. (More specifically, 2^number of consecutive characters shared.)
    • The shared letters have to be in the same consecutive order it appears in "lucky" but can start anywhere in the word for the same value ("luc" has the same 8* multiplier as "cky").
    • If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters.
  • For ANY letter it shares with the word "omen" subtract 2 from the luck.
    • It can match a character any amount of times, in any order. For instance the string "nnnnnomemenn" loses 24 luck (12 matching letters)

Example:

luck("lucky")
>>32

2^5 (5 consecutive letters) = 32

luck("firetruck")
>>6

2^3 - 2 (3 consecutive letters from uck, e shared with omen)

luck("memes")
>>-7

1 - 8 (base amount, 4 shared with "omen")

However, we were given the task to sort them, so...

sortByLuck(["lucky","memes","firetruck"])
>>["lucky","firetruck","memes"]

This is code golf, so the answer with the fewest bytes wins.

You can input and output any way you'd like - write a function, use standard input, etc.

For functions, assume whatever data type would make sense for that language. (For example, in JavaScript, you'd be passed an Array of Strings)