35
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.
8Nice first challenge! – Alex A. – 2016-07-08T02:44:50.803
2Should the program accept uppercase input? – busukxuan – 2016-07-08T02:51:43.573
2@busukxuan Good question - no, it doesn't need to accept uppercase input. – charredgrass – 2016-07-08T03:00:22.800
@cat Not sure I quite understand what you're asking. But you can just assume all input will be lowercase and you don't need to catch any uppercase input. – charredgrass – 2016-07-08T04:15:19.417
1Can we assume an upper or lower bound on the luck of a given input? i.e. what's the smallest number of bits / data type I can get away with, or is it as big as my language can handle? that is, should it be
int8_t str_luck(const char* str);
or should it beuint64_t str_luck(const char* str);
? – cat – 2016-07-08T04:18:04.930@cat I haven't thought about that, but I'd just use something reasonable - whatever makes it easiest or shortest but still works for at least the 3 test cases I mentioned (so it would need to be capable of negatives, and handle numbers larger than 32) – charredgrass – 2016-07-08T04:23:59.560
What's the output for
luuck
? – msh210 – 2016-07-08T04:31:00.807@msh210 8, because
uck
is the longest string of consecutive matching characters – charredgrass – 2016-07-08T04:32:18.630Oh, the question post says "If the word has multiple occurrences where it shares consecutive characters with lucky, use the longest consecutive string of the characters." and I missed it. Sorry. – msh210 – 2016-07-08T04:34:57.503