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.
Clojure:
codesandbox.io/s/daily-challenges-...
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:
Ooh I love Scrabble! I'm excited for this one lol
Here is my over engineered solution in Rust!
I broke it down into multiple different Rust strucs and enums! I'm getting more and more familiar with the Rust type system, and breaking down a game like this is great practice!
One thing I slightly added, is the ability to parse multiple word modifiers together! I opted for a
(t)(d)
syntax do indicate both a triple and double word score. This is possible in Scrabble, so I wanted to include support for itMy solution and test cases is just over 250 lines so here is the link to the source file in Github!
github.com/coreyja/dev-to-challeng...
Oh no, I didn't get to it today! Tomorrow will have to be a double day!
Clojure: