Skip to content

Commit

Permalink
Add scrabble-score exercise (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jan 3, 2025
1 parent 1febd9b commit 8d4b850
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
"uuid": "f626cdcd-12d0-4540-988d-ccae4c6c8299",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "square-root",
"name": "Square Root",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Instructions append

## Reserved Memory

The buffer for the input string uses bytes 64-191 of linear memory.
25 changes: 25 additions & 0 deletions exercises/practice/scrabble-score/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

Your task is to compute a word's Scrabble score by summing the values of its letters.

The letters are valued as follows:

| 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 |

For example, the word "cabbage" is worth 14 points:

- 3 points for C
- 1 point for A
- 3 points for B
- 3 points for B
- 1 point for A
- 2 points for G
- 1 point for E
7 changes: 7 additions & 0 deletions exercises/practice/scrabble-score/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

[Scrabble][wikipedia] is a word game where players place letter tiles on a board to form words.
Each letter has a value.
A word's score is the sum of its letters' values.

[wikipedia]: https://en.wikipedia.org/wiki/Scrabble
18 changes: 18 additions & 0 deletions exercises/practice/scrabble-score/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [
"*.spec.js"
],
"excludedFiles": [
"custom.spec.js"
],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
28 changes: 28 additions & 0 deletions exercises/practice/scrabble-score/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"scrabble-score.wat"
],
"test": [
"scrabble-score.spec.js"
],
"example": [
".meta/proof.ci.wat"
],
"invalidator": [
"package.json"
]
},
"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",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
44 changes: 44 additions & 0 deletions exercises/practice/scrabble-score/.meta/proof.ci.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(module
(memory (export "mem") 1)

;; ':' is the ASCII character after '9' and indicates a letter score of 10.
(data (i32.const 0) "1332142418513113:11114484:")

(global $ZERO i32 (i32.const 48))
(global $A i32 (i32.const 65))

;;
;; Given a word, compute the Scrabble score for that word
;;
;; @param {i32} offset - The offset of the input string in linear memory
;; @param {i32} length - The length of the input string in linear memory
;;
;; @returns {i32} - the computed score
;;
(func (export "score") (param $offset i32) (param $len i32) (result i32)
(local $source i32)
(local $result i32)
(local $letter i32)

(local.set $source (i32.add (local.get $offset)
(local.get $len)))
(local.set $result (i32.const 0))

(loop $body
(if (i32.ne (local.get $source) (local.get $offset)) (then
(local.set $source (i32.sub (local.get $source)
(i32.const 1)))
(local.set $letter (i32.sub (i32.and (i32.load8_u (local.get $source))
(i32.const -33))
(global.get $A)))
(if (i32.lt_u (local.get $letter) (i32.const 26)) (then
(local.set $result (i32.add (local.get $result)
(i32.sub (i32.load8_u (local.get $letter))
(global.get $ZERO))))
))
(br $body)
))
)
(return (local.get $result))
)
)
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"
1 change: 1 addition & 0 deletions exercises/practice/scrabble-score/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/scrabble-score/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/scrabble-score/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
presets: ["@exercism/babel-preset-javascript"],
plugins: [],
};
34 changes: 34 additions & 0 deletions exercises/practice/scrabble-score/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@exercism/wasm-scrabble-score",
"description": "Exercism exercises in WebAssembly.",
"type": "module",
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/exercism/wasm",
"directory": "exercises/practice/scrabble-score"
},
"jest": {
"maxWorkers": 1
},
"devDependencies": {
"@babel/core": "^7.23.3",
"@exercism/babel-preset-javascript": "^0.4.0",
"@exercism/eslint-config-javascript": "^0.6.0",
"@types/jest": "^29.5.8",
"@types/node": "^20.9.1",
"babel-jest": "^29.7.0",
"core-js": "^3.33.2",
"eslint": "^8.54.0",
"jest": "^29.7.0"
},
"dependencies": {
"@exercism/wasm-lib": "^0.2.0"
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*",
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*",
"lint": "eslint ."
}
}
95 changes: 95 additions & 0 deletions exercises/practice/scrabble-score/scrabble-score.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { compileWat, WasmRunner } from "@exercism/wasm-lib";

let wasmModule;
let currentInstance;

beforeAll(async () => {
try {
const watPath = new URL("./scrabble-score.wat", import.meta.url);
const { buffer } = await compileWat(watPath);
wasmModule = await WebAssembly.compile(buffer);
} catch (err) {
console.log(`Error compiling *.wat: \n${err}`);
process.exit(1);
}
});

function score(word = "") {
const inputBufferOffset = 64;
const inputBufferCapacity = 128;

const inputLengthEncoded = new TextEncoder().encode(word).length;
if (inputLengthEncoded > inputBufferCapacity) {
throw new Error(
`String is too large for buffer of size ${inputBufferCapacity} bytes`
);
}

currentInstance.set_mem_as_utf8(inputBufferOffset, inputLengthEncoded, word);

return currentInstance.exports.score(
inputBufferOffset,
inputLengthEncoded
);
}

describe("ScrabbleScore", () => {
beforeEach(async () => {
currentInstance = null;

if (!wasmModule) {
return Promise.reject();
}
try {
currentInstance = await new WasmRunner(wasmModule);
return Promise.resolve();
} catch (err) {
console.log(`Error instantiating WebAssembly module: ${err}`);
return Promise.reject();
}
});

test("lowercase letter", () => {
expect(score("a")).toBe(1);
});

xtest("uppercase letter", () => {
expect(score("A")).toBe(1);
});

xtest("valuable letter", () => {
expect(score("f")).toBe(4);
});

xtest("short word", () => {
expect(score("at")).toBe(2);
});

xtest("short, valuable word", () => {
expect(score("zoo")).toBe(12);
});

xtest("medium word", () => {
expect(score("street")).toBe(6);
});

xtest("medium, valuable word", () => {
expect(score("quirky")).toBe(22);
});

xtest("long, mixed-case word", () => {
expect(score("OxyphenButazone")).toBe(41);
});

xtest("english-like word", () => {
expect(score("pinata")).toBe(8);
});

xtest("empty input", () => {
expect(score("")).toBe(0);
});

xtest("entire alphabet available", () => {
expect(score("abcdefghijklmnopqrstuvwxyz")).toBe(87);
});
});
15 changes: 15 additions & 0 deletions exercises/practice/scrabble-score/scrabble-score.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(module
(memory (export "mem") 1)

;;
;; Given a word, compute the Scrabble score for that word
;;
;; @param {i32} offset - The offset of the input string in linear memory
;; @param {i32} length - The length of the input string in linear memory
;;
;; @returns {i32} - the computed score
;;
(func (export "score") (param $offset i32) (param $len i32) (result i32)
(return (i32.const -1))
)
)

0 comments on commit 8d4b850

Please sign in to comment.