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

genebot's solution

to Scrabble Score in the Nim Track

Published at Jan 31 2021 · 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.

Running the tests

To compile and run the tests, just run the following in your exercise directory:

$ nim c -r test_scrabble_score.nim

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/nim/scrabble-score directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Exercises Directory.

Need help?

These guides should help you,

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.

test_scrabble_score.nim

import unittest
import scrabble_score

suite "Scrabble Score":
  test "lowercase letter":
    check score("a") == 1

  test "uppercase letter":
    check score("A") == 1

  test "valuable letter":
    check score("f") == 4

  test "short word":
    check score("at") == 2

  test "short, valuable word":
    check score("zoo") == 12

  test "medium word":
    check score("street") == 6

  test "medium, valuable word":
    check score("quirky") == 22

  test "long, mixed-case word":
    check score("OxyphenButazone") == 41

  test "english-like word":
    check score("pinata") == 8

  test "empty input":
    check score("") == 0

  test "entire alphabet available":
    check score("abcdefghijklmnopqrstuvwxyz") == 87
from math import sum
from sequtils import mapIt, toSeq
from strutils import toUpperAscii
from tables import toTable, `[]`

const SCORES = {
    'A': 1,
    'B': 3,
    'C': 3,
    'D': 2,
    'E': 1,
    'F': 4,
    'G': 2,
    'H': 4,
    'I': 1,
    'J': 8,
    'K': 5,
    'L': 1,
    'M': 3,
    'N': 1,
    'O': 1,
    'P': 3,
    'Q': 10,
    'R': 1,
    'S': 1,
    'T': 1,
    'U': 1,
    'V': 4,
    'W': 4,
    'X': 8,
    'Y': 4,
    'Z': 10,
}.toTable

proc score*(s: string): int =
    s.toUpperAscii.toSeq.mapIt(SCORES[it]).sum

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?