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
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.
JavaScript
Live demo on CodePen.
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.
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.
Ruby solution
I borrowed your tests, @yzhernand . Thank you for writing them, so I didn't have to.
My overly complex (nim) solution :)
Calculates only by letter points
Elixir. Admittedly my Exercism solution with a bunch of String.replace calls to handle the more sophisticated way the problem is stated here.