Skip to content

Commit

Permalink
Add scrabble-score
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Mar 8, 2024
1 parent ea20865 commit a4ebd4e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@
"nested_classes",
"reactive_programming"
]
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
"uuid": "753c4d89-02b0-4856-935b-6471a84b4892",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
40 changes: 40 additions & 0 deletions exercises/practice/scrabble-score/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Instructions

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

## Letter Values

You'll need these:

```text
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.
19 changes: 19 additions & 0 deletions exercises/practice/scrabble-score/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"source/scrabble_score.d"
],
"test": [
"source/scrabble_score.d"
],
"example": [
"example/scrabble_score.d"
]
},
"blurb": "Given a word, compute the Scrabble score for that word.",
"source": "Inspired by the Extreme Startup game",
"source_url": "https://github.com/rchatley/extreme_startup"
}
43 changes: 43 additions & 0 deletions exercises/practice/scrabble-score/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[f46cda29-1ca5-4ef2-bd45-388a767e3db2]
description = "lowercase letter"

[f7794b49-f13e-45d1-a933-4e48459b2201]
description = "uppercase letter"

[eaba9c76-f9fa-49c9-a1b0-d1ba3a5b31fa]
description = "valuable letter"

[f3c8c94e-bb48-4da2-b09f-e832e103151e]
description = "short word"

[71e3d8fa-900d-4548-930e-68e7067c4615]
description = "short, valuable word"

[d3088ad9-570c-4b51-8764-c75d5a430e99]
description = "medium word"

[fa20c572-ad86-400a-8511-64512daac352]
description = "medium, valuable word"

[9336f0ba-9c2b-4fa0-bd1c-2e2d328cf967]
description = "long, mixed-case word"

[1e34e2c3-e444-4ea7-b598-3c2b46fd2c10]
description = "english-like word"

[4efe3169-b3b6-4334-8bae-ff4ef24a7e4f]
description = "empty input"

[3b305c1c-f260-4e15-a5b5-cb7d3ea7c3d7]
description = "entire alphabet available"
2 changes: 2 additions & 0 deletions exercises/practice/scrabble-score/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name "scrabble-score"
buildRequirements "disallowDeprecations"
57 changes: 57 additions & 0 deletions exercises/practice/scrabble-score/example/scrabble_score.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module scrabble_scores;

import std.string;

pure int score(immutable string word)
{
int points = 0;
string upper = toUpper(word);
foreach (char letter; upper) {
switch (letter) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'L':
case 'N':
case 'R':
case 'S':
case 'T':
points += 1;
break;
case 'D':
case 'G':
points += 2;
break;
case 'B':
case 'C':
case 'M':
case 'P':
points += 3;
break;
case 'F':
case 'H':
case 'V':
case 'W':
case 'Y':
points += 4;
break;
case 'K':
points += 5;
break;
case 'J':
case 'X':
points += 8;
break;
case 'Q':
case 'Z':
points += 10;
break;
default:
break;
}
}

return points;
}
47 changes: 47 additions & 0 deletions exercises/practice/scrabble-score/source/scrabble_score.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module scrabble_scores;

pure int score(immutable string word)
{
// Implement this function
}

unittest
{
immutable int allTestsEnabled = 0;

// Lowercase letter
assert(score("a") == 1);

static if (allTestsEnabled)
{
// Uppercase letter
assert(score("A") == 1);

// Valuable letter
assert(score("f") == 4);

// Short word
assert(score("at") == 2);

// Short, valuable word
assert(score("zoo") == 12);

// Medium word
assert(score("street") == 6);

// Medium, valuable word
assert(score("quirky") == 22);

// Long, mixed-case word
assert(score("OxyphenButazone") == 41);

// English-like word
assert(score("pinata") == 8);

// Empty input
assert(score("") == 0);

// Entire alphabet available
assert(score("abcdefghijklmnopqrstuvwxyz") == 87);
}
}

0 comments on commit a4ebd4e

Please sign in to comment.