🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of bkhl

bkhl's solution

to Scrabble Score in the Perl 5 Track

Published at Sep 09 2019 · 0 comments
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.

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 perl
use Test2::V0;
use JSON::PP;

use FindBin qw($Bin);
use lib $Bin, "$Bin/local/lib/perl5";

use ScrabbleScore qw(score);

my $C_DATA = do { local $/; decode_json(<DATA>); };
plan 12;

imported_ok qw(score) or bail_out;

for my $case ( @{ $C_DATA->{cases} } ) {
  is score( $case->{input}{word} ), $case->{expected},
    $case->{description};
}

__DATA__
{
  "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
    }
  ]
}
package ScrabbleScore;
use strict;
use warnings FATAL => 'all';

use Exporter qw(import);
our @EXPORT_OK = qw(score);

use List::Util qw(pairmap sum0);

my %SCORES = pairmap { map { $_ => $b } split '', $a } (
    'AEIOULNRST' => 1,
    'DG' => 2,
    'BCMP' => 3,
    'FHVWY' => 4,
    'K' => 5,
    'JX' => 8,
    'QZ' => 10,
);

sub score {
    my ($input) = @_;

    return sum0 map { $SCORES{$_} } split '', uc $input;
}

1;

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?