letscramblestr1str2=letletterLookup=str1|>Seq.countByid|>Map.ofSeqletrequiredCounts=str2|>Seq.countByidrequiredCounts|>Seq.forall(fun(letter,requiredCount)->matchletterLookup|>Map.tryFindletterwith|None->false// letter missing|Somecount->requiredCount<=count)
Edit: I had previously posted a version that had ever-so-slightly more performance but was more code. I'm also including that version below since it solves the problem differently.
letscramblestr1str2=letletterPool=str1|>Seq.sort|>List.ofSeqletrequiredLetters=str2|>Seq.sort|>List.ofSeqletreclooprequiredLettersletterPool=matchrequiredLetters,letterPoolwith|[],_->true// found all|_::_,[]->false// pool ran out|letter::_,next::poolwhenletter>next->looprequiredLetterspool// skip next in pool|letter::required,next::poolwhenletter=next->looprequiredpool|_,_->false// letter not available in the poollooprequiredLettersletterPool
Nice! yeah -- I only sometimes think about the efficiencies of these. They're contrived and we're doing them for fun. Part of me cares, part of me doesn't haha
I agree. I would have posted a much more expressive version, but you beat me to it! I did find an alternative way of solving the problem that was a little more expressive and nearly the same perf. I updated my post to include it.
(defunscramble(sourcetarget)(let((source(coercesource'list))(target(coercetarget'list)))(cond((nulltarget)t)((member(firsttarget)source)(scramble(remove(firsttarget)source:count1)(resttarget))))))(mapcar#'(lambda(args)(apply#'scrambleargs))'(("rkqodlw""world")("cedewaraaossoqqyt""codewars")("katas""steak")("scriptjavx""javascript")("scriptingjava""javascript")("scriptsjava""javascripts")("javscripts""javascript")("aabbcamaomsccdd""commas")("commas""commas")("sammoc""commas")));; => (T T NIL NIL T T NIL T T T)
Thursday
Scramblies (5 KYU):
Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false
CodeWars
Solved it awhile ago in C#.
⚠ Warning: Really ugly...
F#
Edit: I had previously posted a version that had ever-so-slightly more performance but was more code. I'm also including that version below since it solves the problem differently.
Here are the tests (console).
Nice! yeah -- I only sometimes think about the efficiencies of these. They're contrived and we're doing them for fun. Part of me cares, part of me doesn't haha
I agree. I would have posted a much more expressive version, but you beat me to it! I did find an alternative way of solving the problem that was a little more expressive and nearly the same perf. I updated my post to include it.
Not a fancy solution, but it seems to work 😃
Rust
Go
Common Lisp
Python!
Python style
def scramble(s1, s2):
return all([s1.count(l) >= s2.count(l) for l in s2])