Determine the "Luck" of a string
JavaScript (ES7), 123 112 107 bytes
s=>2**[5,4,3,2,1,0].find((i,_,a)=>a.some(j=>s.includes("luckyL".substr(j,i))))-2*~-s.split(/[omen]/).length
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:
f=
s=>(1<<[5,4,3,2,1,0].find((i,_,a)=>a.some(j=>s.includes("luckyL".substr(j,i)))))-2*~-s.split(/[omen]/).length
;
<input oninput=o.textContent=f(this.value)><pre id=o></pre>
05AB1E, 36 32 28 26 bytes
Œv'¸éyåiyˆ}}¯é¤go¹'ƒÖ¦Ãg·-
Explanation
Œ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
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")