Everyone loves a game of Scrabble! Your challenge today is to calculate the scrabble score of a given word.
Scoring per tile:
To make things even more challenging, please consider additional scoring as follows:
Double letter (doubles the value of the letter)
-A double letter will be represented with an asterisk after the letter. he*llo would make a double letter on the e.Triple letter (triples the value of the letter)
-A triple letter will be represented with two asterisks after the letter. he**llo would make a triple letter on the e.Double word (double the value of the word after letter rules have been applied)
-A double word is represented by the word ending in (d)Triple word (triple the value of the word after letter rules have been applied)
-A triple word is represented by the word ending in (t)A blank (the letter given will score 0)
-A blank tile will be represented with a caret after the letter or asterisk is the letter has a double or triple letter value. he^llo would mean the e scores 0.Bonus 50!
-If the word is a seven letter word an additional 50 points are awarded.
Good luck and happy coding!
This challenge comes from user grantw1991 on Codewars
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (18)
Here's my solution in Perl, along with a few tests.
Edit: fixed a bug, stray print, and added the 7 letter bonus
JavaScript
Live demo on CodePen.
Erlang:
Ah, neat. Pattern matching was definitely the way to go about it functionally, wish I'd done that instead!
Good old C. Be advised though there are ways to bork this one with some inputs not consistent with the rubric.
Because these are fun in languages you don't actually know, here's Haskell:
I didn't provide tests, but I think it works. Maybe I'll write some later on.
My overly complex (nim) solution :)
A few days late, but here's my JavaScript solution, using
reduce
.I changed the rules a bit, since it's hard to distinguish between double/triple words and words that naturally end in d or t, I decided to use 2 or 3 instead.
Ruby solution
I borrowed your tests, @yzhernand . Thank you for writing them, so I didn't have to.
I’m learning Erlang.
I had forgotten that you can have several elements to the Head in a pattern match (then I saw @stevemoon ’s solution), so I used
lists:foldr
to process the word from the end, which means I accumulate the multipliers and apply them once I encounter a letter. I could have named variables better, but with short names it’s easier on the eye, with this state tuple I’m moving around.For fun I decided to allow several word multipliers, and to allow the blank indicator before, after, or in the middle of the asterisks.
I made my own tests, and stole other peoples’ as well.
To run:
codesandbox.io/s/daily-challenges-...