4d47's solution

to Scrabble Score in the Perl 6 Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Given a word, compute the scrabble score for that word.

Letter Values

You'll need these:

``````Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
``````

Examples

"cabbage" should be scored as worth 14 points:

• 3 points for C
• 1 point for A, twice
• 3 points for B, twice
• 2 points for G
• 1 point for E

And to total:

• `3 + 2*1 + 2*3 + 2 + 1`
• = `3 + 2 + 6 + 3`
• = `5 + 9`
• = 14

Extensions

• You can play a double or a triple letter.
• You can play a double or a triple word.

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension `.t`) will attempt to run routines from the module (a file with the extension `.pm6`). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command `perl6 --doc *.t` (* being the name of the test suite), and run the test suite for the exercise by executing the command `prove6 .` in the exercise directory.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

scrabble-score.t

``````#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib \$?FILE.IO.dirname;
use Scrabble;
plan 11;

my \$c-data = from-json \$=pod.pop.contents;
is .<input><word>.&score, |.<expected description> for @(\$c-data<cases>);

=begin code
{
"exercise": "scrabble-score",
"version": "1.1.0",
"cases": [
{
"description": "lowercase letter",
"property": "score",
"input": {
"word": "a"
},
"expected": 1
},
{
"description": "uppercase letter",
"property": "score",
"input": {
"word": "A"
},
"expected": 1
},
{
"description": "valuable letter",
"property": "score",
"input": {
"word": "f"
},
"expected": 4
},
{
"description": "short word",
"property": "score",
"input": {
"word": "at"
},
"expected": 2
},
{
"description": "short, valuable word",
"property": "score",
"input": {
"word": "zoo"
},
"expected": 12
},
{
"description": "medium word",
"property": "score",
"input": {
"word": "street"
},
"expected": 6
},
{
"description": "medium, valuable word",
"property": "score",
"input": {
"word": "quirky"
},
"expected": 22
},
{
"description": "long, mixed-case word",
"property": "score",
"input": {
"word": "OxyphenButazone"
},
"expected": 41
},
{
"description": "english-like word",
"property": "score",
"input": {
"word": "pinata"
},
"expected": 8
},
{
"description": "empty input",
"property": "score",
"input": {
"word": ""
},
"expected": 0
},
{
"description": "entire alphabet available",
"property": "score",
"input": {
"word": "abcdefghijklmnopqrstuvwxyz"
},
"expected": 87
}
]
}
=end code``````
``````unit class Scrabble;

my %score-table =
<A E I O U L N R S T>.any => 1,
<D G>.any                 => 2,
<B C M P>.any             => 3,
<F H V W Y>.any           => 4,
<K>.any                   => 5,
<J X>.any                 => 8,
<Q Z>.any                 => 10;

method score(Str \$word --> UInt) {
\$word.trim.uc.comb.map( { %score-table{\$_} } ).sum;
}``````

Note to self: should remove diacritical marks, so eg. "ÉCOLE" scores

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?