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)



2^5 (5 consecutive letters) = 32


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


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.

  • 8
    \$\begingroup\$ Nice first challenge! \$\endgroup\$ – Alex A. Jul 8 '16 at 2:44
  • 2
    \$\begingroup\$ Should the program accept uppercase input? \$\endgroup\$ – busukxuan Jul 8 '16 at 2:51
  • 2
    \$\begingroup\$ @busukxuan Good question - no, it doesn't need to accept uppercase input. \$\endgroup\$ – charredgrass Jul 8 '16 at 3:00
  • \$\begingroup\$ @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. \$\endgroup\$ – charredgrass Jul 8 '16 at 4:15
  • 1
    \$\begingroup\$ Can 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 be uint64_t str_luck(const char* str);? \$\endgroup\$ – cat Jul 8 '16 at 4:18

16 Answers 16


05AB1E, 36 32 28 26 bytes



Œv         }                  # for each substring of input
  '¸éyåi  }                   # if substring is part of "lucky"
        yˆ                    # add it to global array
            ¯é¤               # get the longest such substring
               go             # raise 2 to its length
                 ¹'ƒÖ¦Ã       # remove all chars from input that isn't in "omen"
                       g·     # get length and multiply by 2
                         -    # subtract
                              # implicitly display

Try it online

Saved 2 bytes thanks to Adnan

  • \$\begingroup\$ Compression for 1 word can also be done with ', so for 26: Œv'¸éyåiyˆ}}¯é¤go¹'ƒÖ¦Ãg·- :). \$\endgroup\$ – Adnan Jul 8 '16 at 14:03
  • \$\begingroup\$ @Adnan: Weird. I was certain I had tried that. Apparently not. Thanks! \$\endgroup\$ – Emigna Jul 8 '16 at 14:14
  • \$\begingroup\$ why isn't this the top answer? \$\endgroup\$ – noɥʇʎԀʎzɐɹƆ Jul 9 '16 at 13:35

JavaScript (ES7), 123 112 107 bytes


Edit: Saved 11 bytes thanks to @Titus by assuming that the letter L does not appear in the input. Saved 5 bytes thanks to @Oriol. ES6 version for 125 114 109 bytes:

<input oninput=o.textContent=f(this.value)><pre id=o></pre>

  • \$\begingroup\$ Why do you use replace([^]) instead of match([])? Do you waste 3 bytes or is there a reason? \$\endgroup\$ – Titus Jul 8 '16 at 13:02
  • \$\begingroup\$ @Titus How many bytes does it cost to deal with a null match result? \$\endgroup\$ – Neil Jul 8 '16 at 14:24
  • 1
    \$\begingroup\$ Four for a string, and a pair of () in this case; eating up all six that you would save with match(/[omen]/). Pity. \$\endgroup\$ – Titus Jul 8 '16 at 15:14
  • 1
    \$\begingroup\$ @Titus Not sure if that's what you meant but by adding an L to the end of the substr (which will never appear in the original string) I don't have to worry about extraneous matches and I can actually use the same array [5,4,3,2,1,0] both times, saving a whopping 13 bytes! \$\endgroup\$ – Neil Jul 8 '16 at 18:33
  • 1
    \$\begingroup\$ -2*s.split(/[omen]/).length+2 is shorter. \$\endgroup\$ – Oriol Jul 9 '16 at 2:44

Pyth, 27 26 28 bytes


1 byte saved thanks to OP :-)


                                 Implicit Q as input
                .:Q              Find all substrings of input
     +k}#"lucky"                 Filter for substring of "lucky", prepend "" in case of []
    e                            Take last element, which is longest
   l                             Get its length
 ^2                              Raise two to that
                      @"omen"Q   Filter Q for characters in "omen"
                     l           Get length; counts how many characters in "omen" there are
                    y            Double that
-                                Find the difference

Test it here.

  • 1
    \$\begingroup\$ I'm not an expert in Pyth but I believe you can change "omen" to just "omen and Pyth will understand \$\endgroup\$ – charredgrass Jul 8 '16 at 3:52
  • \$\begingroup\$ @charredgrass Oops, my mistake :-) \$\endgroup\$ – busukxuan Jul 8 '16 at 4:34
  • 1
    \$\begingroup\$ Doesn't seem to work for string without chars form "lucky" in it. "memes" for example. \$\endgroup\$ – Emigna Jul 8 '16 at 12:30
  • 1
    \$\begingroup\$ @Emigna Ah. the zero case again.... Thanks, fixed it! \$\endgroup\$ – busukxuan Jul 8 '16 at 12:56

Ruby, 91 87 bytes

String#count's finnicky usage strikes again! (When passed a String, it counts all occurrences of each letter in the function argument instead of all occurrences of the entire string.)

Try it online

->s{2**(z=0..5).max_by{|j|z.map{|i|s[b="lucky"[i,j]]?b.size: 0}.max}-2*s.count("omen")}

A version that takes in lines from STDIN and prints them: 89 bytes (86 +3 from the -n flag)

p 2**(z=0..5).max_by{|j|z.map{|i|$_[b="lucky"[i,j]]?b.size: 0}.max}-2*$_.count("omen")
  • 1
    \$\begingroup\$ ._. that String#count is weird. +1 for (ab)using it. Also is it shorter to use gets rather than a function? \$\endgroup\$ – Downgoat Jul 8 '16 at 4:40
  • 1
    \$\begingroup\$ @Downgoat if I gets I also has to puts for output, so not in this case. \$\endgroup\$ – Value Ink Jul 8 '16 at 6:21

Ruby: 100 bytes

->s{2**(m=0;4.times{|j|1.upto(5){|i|m=[i,m].max if s.match"lucky"[j,i]}};m)-s.scan(/[omen]/).size*2}
  • \$\begingroup\$ Try /[omen]/ as the regex to golf it down - it will match any character and is better in practical use than chaining |s for single characters. \$\endgroup\$ – charredgrass Jul 8 '16 at 3:21

Javascript - 206 Bytes

r=>{var a="lucky";r:for(var e=5;e>0;e--)for(var n=0;6>n+e;n++){var o=a.substring(n,e+n);if(r.includes(o))break r}for(var t=0,e=0;e<r.length;e++)('omen'.indexOf(r[e])+1)&&t++;return Math.pow(2,o.length)-2*t}
  • 1
    \$\begingroup\$ You could change this condition: s[k]=='o'||s[k]=='m'||s[k]=='e'||s[k]=='n' to look like this: "oman".split("").includes(s[k]) \$\endgroup\$ – addison Jul 8 '16 at 3:38
  • 1
    \$\begingroup\$ Welcome to PPCG! You can golf this down by removing whitespace to conserve bytes. Also, instead of (s[k]=='o'||s[k]=='m'||s[k]=='e'||s[k]=='n') you could use ('omen'.indexOf(s[k])+1) (assuming this is JavaScript) \$\endgroup\$ – charredgrass Jul 8 '16 at 3:38
  • \$\begingroup\$ Thanks for the tips! Got it down to 237, although it looks like the Ruby crowd's got me beat. \$\endgroup\$ – Christopher Burgdorff Jul 8 '16 at 3:45
  • \$\begingroup\$ Another small thing: you can shorthand function luck(r) down to r=> to just make it an anonymous function, that's all that is necessary for this challenge. Also I made an edit to the challenge so you don't have to worry about case so you can remove the r=r.toLowerCase(); \$\endgroup\$ – charredgrass Jul 8 '16 at 3:49
  • \$\begingroup\$ Instead of substring you can use slice i believe (test this though, as I'm not sure) \$\endgroup\$ – Downgoat Jul 8 '16 at 4:39

Ruby, 57 bytes

p 2**$./2-2*b

gets sets $. to 1 as a side effect, then we increment it until the regular expression matching $. consecutive lucky characters no longer matches.


Haskell, 99

Another approach... I just learned about function aliasing

import Data.List
f n=2^(l$last$i(s"lucky")$s n)-2*l(i n$"omen")





Mathematica, 86 bytes




LongestCommonSubsequence returns the longest contiguous substring common to the input and "lucky". StringLength gives its length. StringCount counts the number of occurrences of the characters of "omen" in the input.


Python (139 Bytes)

import itertools as t
print 2**max([j-i for i,j in t.combinations(range(6),2)if'lucky'[i:j]in s]+[0])-2*sum(_ in'omen'for _ in s)
  • \$\begingroup\$ You can save one byte by using from intertools import* \$\endgroup\$ – wnnmaw Jul 8 '16 at 19:44

TSQL, 233 bytes


DECLARE @t varchar(99)='oluck'

,@z INT=0,@a INT=0,@ INT=1,@c INT=0WHILE @a<LEN(@t)SELECT
@a+=IIF(@=1,1,0),@z=IIF('LUCKY'LIKE'%'+x+'%'and @>@z,@,@z),@c+=IIF(x
SUBSTRING(@t,@a,@)x)x PRINT POWER(2,@z)-@c


DECLARE @t varchar(99)='oluck'

,@z INT=0
,@a INT=0
,@  INT=1
,@c INT=0
WHILE @a<LEN(@t)
    @z=IIF('LUCKY'LIKE'%'+x+'%'and @>@z,@,@z),
    @c+=IIF(x IN('O','M','E','N'),2,0),
PRINT POWER(2,@z)-@c

Try it online


Haskell (134 132 Bytes)

import Data.List
c s@(_:x)=inits s++c x
g q=2^(maximum$map(\z->l q-l(q\\z))$c"lucky")-2*(l$intersect q"omen")

Not a code golfer nor a Haskell programmer, so would love some tips on this one.

(Example: g "firetruck")

  • \$\begingroup\$ I am no Haskell Expert either, but managed to carve off a few byes by changing the algorithm slightly and using function aliases on reused functions. \$\endgroup\$ – Zylviij Jul 9 '16 at 1:23

Python 3, 168 157 152 139 144 136 bytes

EDIT: Really obvious things I should have seen easier have been changed, and some slightly less obvious.

Edit 2: stoopid (˚n˚). The program threw errors. I fixed it up. not actually 153 :(

Thanks to Leaky Nun for saving 5 bytes, and to jmilloy for saving 13 8 bytes.

print(2**m-2*sum(i in"omen"for i in s))

The program runs through all possibly possible substrings in input (possibly possible, because it computes impossible substrings as well, 8 to 7, for example), checks if the substring is in "lucky", then sets the exponent of 2 to the length of the substring should it be greater than the current value. Possibly could be improved by using only one while loop. Could possibly use some improvement; I'm still getting the hang of this.

  • \$\begingroup\$ while p+1 becomes while-~p \$\endgroup\$ – Leaky Nun Jul 8 '16 at 11:06
  • \$\begingroup\$ since b=s[p:q], len(b) must be q-p right? \$\endgroup\$ – Leaky Nun Jul 8 '16 at 11:11
  • \$\begingroup\$ I stole you input and print method but did the rest very different, thanks! I think if you just do print(2**m-2*sum(i in"omen" for i in s)) for your last three lines you'll do better, like 148? \$\endgroup\$ – jmilloy Jul 8 '16 at 16:45
  • \$\begingroup\$ Oh, and you can just move s[p:q] into the if clause while-~q:n=q-p;m=n if(s[p:q]in"lucky")*n>m else m;q-=1 for 143? \$\endgroup\$ – jmilloy Jul 8 '16 at 16:51
  • \$\begingroup\$ sum(map(s.count,"omen")) saves one byte, making it 135 \$\endgroup\$ – Black Owl Kai Feb 2 '19 at 14:50

PHP program, 139 135 108 bytes

quantum leap fails for multiple substrings where the first occurence is shorter. :(

actually I could save another 7 bytes in PHP<5.4 with register_globals on


usage: php -d error_reporting=0 <filename> <string>

+5 for a function:

function f($s){for(;$i<5;$i++)for($j=6;--$j;)$r=max($r,strstr($s,substr('lucky*',$i,$j))?2**$j:1);return$r-2*preg_match_all('/[omen]/',$s);}

tests (on the function)

echo '<table border=1><tr><th>input</th><th>output</th><th>expected</th><th>ok?</th></tr>';
    'lumberjack'=>0,        'smack'=>2,
    'nnnnnomemenn'=>-23,    'lucky'=>32,
    'omen'=>-7,             'firetruck'=>6,
    'memes'=>-7,            'determine the “luck” of a string'=>0,
    'amazing'=>-3,          'wicked'=>2,
    'chucky'=>16,           'uckyuke'=>14,
    'ugly'=>2,              'lucy'=>8,
] as $x=>$e){
}echo '</table>';

Scala, 155 bytes

def f(w:String)=(1::List.fill((for(a<-1 to 5;s<-"lucky".sliding(a))yield if(w.contains(s)) a else 0).max){2}).product-2*w.filter("omen".contains(_)).length

C# (Visual C# Interactive Compiler) with flag /u:System.Text.RegularExpressions.Regex, 99 bytes


Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.