-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nenad Misić
authored
Jul 4, 2024
1 parent
d518b82
commit e75f503
Showing
9 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"authors": [ | ||
"misicnenad" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/lib.cairo", | ||
"Scarb.toml" | ||
], | ||
"test": [ | ||
"src/tests.cairo" | ||
], | ||
"example": [ | ||
".meta/example.cairo" | ||
] | ||
}, | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
fn score(word: ByteArray) -> u16 { | ||
let mut values = dictionary(); | ||
let mut score = 0; | ||
let mut i = 0; | ||
while let Option::Some(char) = word | ||
.at(i) { | ||
score += values.get(lowercase(char).into()); | ||
i += 1; | ||
}; | ||
score | ||
} | ||
|
||
fn dictionary() -> Felt252Dict<u16> { | ||
let mut values: Felt252Dict<u16> = Default::default(); | ||
values.insert('a', 1); | ||
values.insert('b', 3); | ||
values.insert('c', 3); | ||
values.insert('d', 2); | ||
values.insert('e', 1); | ||
values.insert('f', 4); | ||
values.insert('g', 2); | ||
values.insert('h', 4); | ||
values.insert('i', 1); | ||
values.insert('j', 8); | ||
values.insert('k', 5); | ||
values.insert('l', 1); | ||
values.insert('m', 3); | ||
values.insert('n', 1); | ||
values.insert('o', 1); | ||
values.insert('p', 3); | ||
values.insert('q', 10); | ||
values.insert('r', 1); | ||
values.insert('s', 1); | ||
values.insert('t', 1); | ||
values.insert('u', 1); | ||
values.insert('v', 4); | ||
values.insert('w', 4); | ||
values.insert('x', 8); | ||
values.insert('y', 4); | ||
values.insert('z', 10); | ||
values | ||
} | ||
|
||
fn lowercase(char: u8) -> u8 { | ||
if char < 97 { | ||
char + 32 | ||
} else { | ||
char | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "scrabble_score" | ||
version = "0.1.0" | ||
edition = "2023_11" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn score(word: ByteArray) -> u16 { | ||
panic!("Score {word} in Scrabble.") | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use scrabble_score::score; | ||
|
||
#[test] | ||
fn lowercase_letter() { | ||
let input = "a"; | ||
let output = score(input); | ||
let expected = 1; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn uppercase_letter() { | ||
let input = "A"; | ||
let output = score(input); | ||
let expected = 1; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn valuable_letter() { | ||
let input = "f"; | ||
let output = score(input); | ||
let expected = 4; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn short_word() { | ||
let input = "at"; | ||
let output = score(input); | ||
let expected = 2; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn short_valuable_word() { | ||
let input = "zoo"; | ||
let output = score(input); | ||
let expected = 12; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn medium_word() { | ||
let input = "street"; | ||
let output = score(input); | ||
let expected = 6; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn medium_valuable_word() { | ||
let input = "quirky"; | ||
let output = score(input); | ||
let expected = 22; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn long_mixed_case_word() { | ||
let input = "OxyphenButazone"; | ||
let output = score(input); | ||
let expected = 41; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn english_like_word() { | ||
let input = "pinata"; | ||
let output = score(input); | ||
let expected = 8; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn empty_input() { | ||
let input = ""; | ||
let output = score(input); | ||
let expected = 0; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn entire_alphabet_available() { | ||
let input = "abcdefghijklmnopqrstuvwxyz"; | ||
let output = score(input); | ||
let expected = 87; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn numbers_do_not_score() { | ||
let input = "pi3ata9"; | ||
let output = score(input); | ||
let expected = 7; | ||
assert_eq!(output, expected); | ||
} | ||
|
||
#[test] | ||
fn special_characters_do_not_score() { | ||
let input = "@STRA^E$"; | ||
let output = score(input); | ||
let expected = 5; | ||
assert_eq!(output, expected); | ||
} |