diff --git a/rust/README.md b/rust/README.md index e42fba91..ed1f24cc 100644 --- a/rust/README.md +++ b/rust/README.md @@ -38,3 +38,4 @@ - [allergies](./allergies/README.md) - [difference-of-squares](./difference-of-squares/README.md) - [anagram](./anagram/README.md) +- [scrabble-score](./scrabble-score/README.md) diff --git a/rust/scrabble-score/.exercism/config.json b/rust/scrabble-score/.exercism/config.json new file mode 100644 index 00000000..4050334a --- /dev/null +++ b/rust/scrabble-score/.exercism/config.json @@ -0,0 +1,40 @@ +{ + "authors": [ + "IanWhitney" + ], + "contributors": [ + "ashleygwilliams", + "AvasDream", + "coriolinus", + "cwhakes", + "efx", + "ErikSchierboom", + "IanWhitney", + "juleskers", + "kotp", + "lutostag", + "mkantor", + "navossoc", + "nfiles", + "petertseng", + "rofrol", + "stringparser", + "xakon", + "ZapAnton" + ], + "files": { + "solution": [ + "src/lib.rs", + "Cargo.toml" + ], + "test": [ + "tests/scrabble-score.rs" + ], + "example": [ + ".meta/example.rs" + ] + }, + "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" +} diff --git a/rust/scrabble-score/.exercism/metadata.json b/rust/scrabble-score/.exercism/metadata.json new file mode 100644 index 00000000..632c635d --- /dev/null +++ b/rust/scrabble-score/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"scrabble-score","id":"4cf847a06ce14af8a0e1f6061e332429","url":"https://exercism.org/tracks/rust/exercises/scrabble-score","handle":"vpayno","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/scrabble-score/.gitignore b/rust/scrabble-score/.gitignore new file mode 100644 index 00000000..db7f315c --- /dev/null +++ b/rust/scrabble-score/.gitignore @@ -0,0 +1,8 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +**/*.rs.bk + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock diff --git a/rust/scrabble-score/Cargo.toml b/rust/scrabble-score/Cargo.toml new file mode 100644 index 00000000..7de526ac --- /dev/null +++ b/rust/scrabble-score/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2021" +name = "scrabble-score" +version = "1.1.0" diff --git a/rust/scrabble-score/HELP.md b/rust/scrabble-score/HELP.md new file mode 100644 index 00000000..67add768 --- /dev/null +++ b/rust/scrabble-score/HELP.md @@ -0,0 +1,86 @@ +# Help + +## Running the tests + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, open the tests source file which is located in the `tests` directory +and remove the `#[ignore]` flag from the next test and get the tests to pass +again. Each separate test is a function with `#[test]` flag above it. +Continue, until you pass every test. + +If you wish to run _only ignored_ tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +If you are using Rust 1.51 or later, you can run _all_ tests with + +```bash +$ cargo test -- --include-ignored +``` + +To run a specific test, for example `some_test`, you can use: + +```bash +$ cargo test some_test +``` + +If the specific test is ignored, use: + +```bash +$ cargo test some_test -- --ignored +``` + +To learn more about Rust tests refer to the online [test documentation][rust-tests]. + +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Submitting your solution + +You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Rust track's documentation](https://exercism.org/docs/tracks/rust) +- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Submitting the solution + +Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. + +## Feedback, Issues, Pull Requests + +The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! + +If you want to know more about Exercism, take a look at the [contribution guide]. + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + +[help-page]: https://exercism.org/tracks/rust/learning +[github]: https://github.com/exercism/rust +[contribution guide]: https://exercism.org/docs/community/contributors \ No newline at end of file diff --git a/rust/scrabble-score/README.md b/rust/scrabble-score/README.md new file mode 100644 index 00000000..5a3072c3 --- /dev/null +++ b/rust/scrabble-score/README.md @@ -0,0 +1,81 @@ +# Scrabble Score + +Welcome to Scrabble Score on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## 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. + +## Source + +### Created by + +- @IanWhitney + +### Contributed to by + +- @ashleygwilliams +- @AvasDream +- @coriolinus +- @cwhakes +- @efx +- @ErikSchierboom +- @IanWhitney +- @juleskers +- @kotp +- @lutostag +- @mkantor +- @navossoc +- @nfiles +- @petertseng +- @rofrol +- @stringparser +- @xakon +- @ZapAnton + +### Based on + +Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup + +### My Solution + +- [my solution](./src/lib.rs) +- [run-tests](./run-tests-rust.txt) diff --git a/rust/scrabble-score/coverage-annotations.txt b/rust/scrabble-score/coverage-annotations.txt new file mode 100644 index 00000000..ccb21edb --- /dev/null +++ b/rust/scrabble-score/coverage-annotations.txt @@ -0,0 +1,64 @@ + 1| |/// Compute the Scrabble score for a word. + 2| | + 3| |/// Scores a scrabble word. + 4| |/// + 5| |/// ```rust + 6| |/// use scrabble_score::*; + 7| |/// + 8| |/// let want: u64 = 87; + 9| |/// let got: u64 = score("abcdefghijklmnopqrstuvwxyz"); + 10| |/// + 11| |/// assert_eq!(got, want); + 12| |/// ``` + 13| 42|pub fn score(word: &str) -> u64 { + 14| 42| word.to_lowercase() + 15| 42| .trim() + 16| 42| .chars() + 17| 258| .map(|c| match c { + 18| 165| 'a' | 'e' | 'i' | 'o' | 'u' | 'l' | 'n' | 'r' | 's' | 't' => 1, + 19| 6| 'd' | 'g' => 2, + 20| 24| 'b' | 'c' | 'm' | 'p' => 3, + 21| 27| 'f' | 'h' | 'v' | 'w' | 'y' => 4, + 22| 6| 'k' => 5, + 23| 9| 'j' | 'x' => 8, + 24| 15| 'q' | 'z' => 10, + 25| 6| _ => 0, + 26| 258| }) + ------------------ + | scrabble_score::score::{closure#0}: + | 17| 258| .map(|c| match c { + | 18| 165| 'a' | 'e' | 'i' | 'o' | 'u' | 'l' | 'n' | 'r' | 's' | 't' => 1, + | 19| 6| 'd' | 'g' => 2, + | 20| 24| 'b' | 'c' | 'm' | 'p' => 3, + | 21| 27| 'f' | 'h' | 'v' | 'w' | 'y' => 4, + | 22| 6| 'k' => 5, + | 23| 9| 'j' | 'x' => 8, + | 24| 15| 'q' | 'z' => 10, + | 25| 6| _ => 0, + | 26| 258| }) + ------------------ + | Unexecuted instantiation: scrabble_score::score::{closure#0} + ------------------ + 27| 42| .sum() + 28| 42|} + ------------------ + | scrabble_score::score: + | 13| 42|pub fn score(word: &str) -> u64 { + | 14| 42| word.to_lowercase() + | 15| 42| .trim() + | 16| 42| .chars() + | 17| 42| .map(|c| match c { + | 18| | 'a' | 'e' | 'i' | 'o' | 'u' | 'l' | 'n' | 'r' | 's' | 't' => 1, + | 19| | 'd' | 'g' => 2, + | 20| | 'b' | 'c' | 'm' | 'p' => 3, + | 21| | 'f' | 'h' | 'v' | 'w' | 'y' => 4, + | 22| | 'k' => 5, + | 23| | 'j' | 'x' => 8, + | 24| | 'q' | 'z' => 10, + | 25| | _ => 0, + | 26| 42| }) + | 27| 42| .sum() + | 28| 42|} + ------------------ + | Unexecuted instantiation: scrabble_score::score + ------------------ \ No newline at end of file diff --git a/rust/scrabble-score/coverage-missing-lines.txt b/rust/scrabble-score/coverage-missing-lines.txt new file mode 100644 index 00000000..46d54138 --- /dev/null +++ b/rust/scrabble-score/coverage-missing-lines.txt @@ -0,0 +1,8 @@ +Running: cargo llvm-cov --no-clean --all-features --workspace --show-missing-lines + +Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score/src/lib.rs 12 0 100.00% 2 0 100.00% 18 0 100.00% 0 0 - +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +TOTAL 12 0 100.00% 2 0 100.00% 18 0 100.00% 0 0 - + diff --git a/rust/scrabble-score/report.lcov b/rust/scrabble-score/report.lcov new file mode 100644 index 00000000..873e7e05 --- /dev/null +++ b/rust/scrabble-score/report.lcov @@ -0,0 +1,32 @@ +SF:/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score/src/lib.rs +FN:17,_RNCNvCsbE52QhvtJFr_14scrabble_score5score0B3_ +FN:13,_RNvCsbE52QhvtJFr_14scrabble_score5score +FN:17,_RNCNvCsfA8C7faOhHu_14scrabble_score5score0B3_ +FN:13,_RNvCsfA8C7faOhHu_14scrabble_score5score +FNDA:86,_RNCNvCsbE52QhvtJFr_14scrabble_score5score0B3_ +FNDA:14,_RNvCsbE52QhvtJFr_14scrabble_score5score +FNDA:0,_RNCNvCsfA8C7faOhHu_14scrabble_score5score0B3_ +FNDA:0,_RNvCsfA8C7faOhHu_14scrabble_score5score +FNF:2 +FNH:2 +DA:13,14 +DA:14,14 +DA:15,14 +DA:16,14 +DA:17,86 +DA:18,55 +DA:19,2 +DA:20,8 +DA:21,9 +DA:22,2 +DA:23,3 +DA:24,5 +DA:25,2 +DA:26,86 +DA:27,14 +DA:28,14 +BRF:0 +BRH:0 +LF:18 +LH:18 +end_of_record \ No newline at end of file diff --git a/rust/scrabble-score/run-tests-rust.txt b/rust/scrabble-score/run-tests-rust.txt new file mode 100644 index 00000000..8e51c7a4 --- /dev/null +++ b/rust/scrabble-score/run-tests-rust.txt @@ -0,0 +1,485 @@ +Running automated test file(s): + + +=============================================================================== + +Running: ../../.github/citools/rust/rust-lint-check --release + +Running Rust Cargo Check + +Rust version: + + rustc 1.72.0 (5680fa18f 2023-08-23) + + + ============================================================================== + +Running: cargo clean + + +real 0m0.015s +user 0m0.007s +sys 0m0.008s + + + ============================================================================== + +Running: cargo check --future-incompat-report --release + + Checking scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished release [optimized] target(s) in 0.10s +note: 0 dependencies had future-incompatible warnings + +real 0m0.113s +user 0m0.028s +sys 0m0.026s + + + ============================================================================== + +Exit code: 0 + +real 0m0.146s +user 0m0.041s +sys 0m0.046s + +real 0m0.149s +user 0m0.043s +sys 0m0.047s + +=============================================================================== + +Running: ../../.github/citools/rust/rust-lint-clippy --release + +Running Rust Cargo Clippy + +Rust version: + + rustc 1.72.0 (5680fa18f 2023-08-23) + + + ============================================================================== + +Running: cargo clean + + +real 0m0.009s +user 0m0.005s +sys 0m0.004s + + + ============================================================================== + +Running: cargo clippy --release + + Checking scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished release [optimized] target(s) in 0.06s + +real 0m0.093s +user 0m0.045s +sys 0m0.047s + + + ============================================================================== + +Exit code: 0 + +real 0m0.119s +user 0m0.055s +sys 0m0.064s + +real 0m0.120s +user 0m0.056s +sys 0m0.064s + +=============================================================================== + +Running: ../../.github/citools/rust/rust-lint-audit +No deps found, skipping cargo audit. + +real 0m0.044s +user 0m0.020s +sys 0m0.025s + +real 0m0.045s +user 0m0.021s +sys 0m0.025s + +=============================================================================== + +rm -fv ./*.profraw ./*.profdata + +=============================================================================== + +Running: ../../.github/citools/rust/rust-test-with-tarpaulin + +Running Rust Tests With Tarpaulin + +Rust version: + + rustc 1.72.0 (5680fa18f 2023-08-23) + + + ============================================================================== + +Running: cargo clean + + +real 0m0.010s +user 0m0.007s +sys 0m0.003s + + + ============================================================================== + +Running: cargo test --all-features + + Compiling scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished test [unoptimized + debuginfo] target(s) in 0.31s + Running unittests src/lib.rs (target/debug/deps/scrabble_score-17e4a989f76efe6c) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/scrabble-score.rs (target/debug/deps/scrabble_score-a35cf42499fcd551) + +running 12 tests +test a_is_worth_one_point ... ok +test all_letters_work ... ok +test empty_words_are_worth_zero ... ok +test f_is_worth_four ... ok +test german_letters_do_not_score ... ok +test long_mixed_case_word ... ok +test medium_word ... ok +test longer_words_with_valuable_letters ... ok +test non_english_scrabble_letters_do_not_score ... ok +test three_letter_word ... ok +test two_one_point_letters_make_a_two_point_word ... ok +test scoring_is_case_insensitive ... ok + +test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Doc-tests scrabble-score + +running 1 test +test src/lib.rs - score (line 4) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.13s + + +real 0m0.473s +user 0m0.473s +sys 0m0.253s + + + ============================================================================== + +Running: cargo tarpaulin --release --timeout=300 + +Sep 18 09:58:39.686  INFO cargo_tarpaulin::config: Creating config +Sep 18 09:58:39.697  INFO cargo_tarpaulin: Running Tarpaulin +Sep 18 09:58:39.697  INFO cargo_tarpaulin: Building project +Sep 18 09:58:39.697  INFO cargo_tarpaulin::cargo: Cleaning project + Compiling scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished release [optimized] target(s) in 0.38s +Sep 18 09:58:40.124  INFO cargo_tarpaulin::process_handling::linux: Launching test +Sep 18 09:58:40.124  INFO cargo_tarpaulin::process_handling: running /home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score/target/release/deps/scrabble_score-3309945b89de8b75 + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +Sep 18 09:58:40.569  INFO cargo_tarpaulin::process_handling::linux: Launching test +Sep 18 09:58:40.569  INFO cargo_tarpaulin::process_handling: running /home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score/target/release/deps/scrabble_score-99f310b7d2d90a3d + +running 12 tests +test empty_words_are_worth_zero ... ok +test medium_word ... ok +test longer_words_with_valuable_letters ... ok +test long_mixed_case_word ... ok +test german_letters_do_not_score ... ok +test f_is_worth_four ... ok +test all_letters_work ... ok +test two_one_point_letters_make_a_two_point_word ... ok +test three_letter_word ... ok +test a_is_worth_one_point ... ok +test scoring_is_case_insensitive ... ok +test non_english_scrabble_letters_do_not_score ... ok + +test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s + +Sep 18 09:58:41.041  INFO cargo_tarpaulin::report: Coverage Results: +|| Uncovered Lines: +|| Tested/Total Lines: +|| src/lib.rs: 3/3 +|| +100.00% coverage, 3/3 lines covered + +real 0m1.372s +user 0m0.506s +sys 0m0.969s + + + ============================================================================== + +Exit code: 0 + +real 0m1.868s +user 0m0.993s +sys 0m1.233s + +real 0m1.870s +user 0m0.994s +sys 0m1.234s + +=============================================================================== + +Running: ../../.github/citools/rust/rust-test-with-llvm-coverage + +Running Rust Tests With LLVM Coverage + +Rust version: + + rustc 1.72.0 (5680fa18f 2023-08-23) + + + ============================================================================== + +Running: cargo clean + + +real 0m0.013s +user 0m0.004s +sys 0m0.009s + + + ============================================================================== + +Running: cargo llvm-cov clean --workspace + + +real 0m0.189s +user 0m0.098s +sys 0m0.094s + + + ============================================================================== + +Running: cargo test --all-features --doc + + Compiling scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished test [unoptimized + debuginfo] target(s) in 0.12s + Doc-tests scrabble-score + +running 1 test +test src/lib.rs - score (line 4) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.11s + + +real 0m0.259s +user 0m0.172s +sys 0m0.125s + + + ============================================================================== + +Running: cargo llvm-cov --no-clean --all-features --workspace + + Compiling scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished test [unoptimized + debuginfo] target(s) in 0.31s + Running unittests src/lib.rs (target/llvm-cov-target/debug/deps/scrabble_score-e79c80208243ac63) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/scrabble-score.rs (target/llvm-cov-target/debug/deps/scrabble_score-ab55caec03cf22f9) + +running 12 tests +test a_is_worth_one_point ... ok +test all_letters_work ... ok +test empty_words_are_worth_zero ... ok +test f_is_worth_four ... ok +test german_letters_do_not_score ... ok +test long_mixed_case_word ... ok +test longer_words_with_valuable_letters ... ok +test medium_word ... ok +test non_english_scrabble_letters_do_not_score ... ok +test scoring_is_case_insensitive ... ok +test three_letter_word ... ok +test two_one_point_letters_make_a_two_point_word ... ok + +test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + +Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score/src/lib.rs 12 0 100.00% 2 0 100.00% 18 0 100.00% 0 0 - +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +TOTAL 12 0 100.00% 2 0 100.00% 18 0 100.00% 0 0 - + +real 0m0.447s +user 0m0.349s +sys 0m0.187s + + + ============================================================================== + +Running: cargo llvm-cov report --lcov --output-path report.lcov + + + Finished report saved to report.lcov + +real 0m0.125s +user 0m0.069s +sys 0m0.057s + + Finished test [unoptimized + debuginfo] target(s) in 0.00s + Running unittests src/lib.rs (target/llvm-cov-target/debug/deps/scrabble_score-e79c80208243ac63) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/scrabble-score.rs (target/llvm-cov-target/debug/deps/scrabble_score-ab55caec03cf22f9) + +running 12 tests +test a_is_worth_one_point ... ok +test all_letters_work ... ok +test empty_words_are_worth_zero ... ok +test f_is_worth_four ... ok +test german_letters_do_not_score ... ok +test long_mixed_case_word ... ok +test longer_words_with_valuable_letters ... ok +test medium_word ... ok +test non_english_scrabble_letters_do_not_score ... ok +test scoring_is_case_insensitive ... ok +test three_letter_word ... ok +test two_one_point_letters_make_a_two_point_word ... ok + +test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + +real 0m0.172s +user 0m0.097s +sys 0m0.076s + + ============================================================================== + +Running: cargo llvm-cov --no-clean --all-features --workspace --text --output-path=coverage-annotations.txt + + Finished test [unoptimized + debuginfo] target(s) in 0.00s + Running unittests src/lib.rs (target/llvm-cov-target/debug/deps/scrabble_score-e79c80208243ac63) + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + Running tests/scrabble-score.rs (target/llvm-cov-target/debug/deps/scrabble_score-ab55caec03cf22f9) + +running 12 tests +test a_is_worth_one_point ... ok +test all_letters_work ... ok +test empty_words_are_worth_zero ... ok +test f_is_worth_four ... ok +test german_letters_do_not_score ... ok +test long_mixed_case_word ... ok +test longer_words_with_valuable_letters ... ok +test medium_word ... ok +test non_english_scrabble_letters_do_not_score ... ok +test scoring_is_case_insensitive ... ok +test three_letter_word ... ok +test two_one_point_letters_make_a_two_point_word ... ok + +test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + + + Finished report saved to coverage-annotations.txt + +real 0m0.156s +user 0m0.084s +sys 0m0.073s + + + ============================================================================== + +Running: cargo llvm-cov clean --workspace + + +real 0m0.141s +user 0m0.082s +sys 0m0.061s + + + ============================================================================== + +Running: lcov --list report.lcov + +Reading tracefile report.lcov + |Lines |Functions |Branches +Filename |Rate Num|Rate Num|Rate Num +================================================== +[/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score/src/] +lib.rs | 100% 16|50.0% 4| - 0 +================================================== + Total:| 100% 16|50.0% 4| - 0 + +real 0m0.036s +user 0m0.030s +sys 0m0.005s + + +Running: lcov --summary report.lcov + +Reading tracefile report.lcov +Summary coverage rate: + lines......: 100.0% (16 of 16 lines) + functions..: 50.0% (2 of 4 functions) + branches...: no data found + +real 0m0.033s +user 0m0.030s +sys 0m0.004s + + + ============================================================================== + +Exit code: 0 + +real 0m1.584s +user 0m1.020s +sys 0m0.698s + +real 0m1.586s +user 0m1.021s +sys 0m0.699s + +=============================================================================== + +Running: misspell . + +real 0m0.026s +user 0m0.036s +sys 0m0.014s + +=============================================================================== + +Running: cargo doc + Documenting scrabble-score v1.1.0 (/home/vpayno/git_vpayno/exercism-workspace/rust/scrabble-score) + Finished dev [unoptimized + debuginfo] target(s) in 0.19s + +real 0m0.198s +user 0m0.152s +sys 0m0.047s + +=============================================================================== + +Running: cargo clean + +real 0m0.010s +user 0m0.007s +sys 0m0.002s + +=============================================================================== + diff --git a/rust/scrabble-score/src/lib.rs b/rust/scrabble-score/src/lib.rs new file mode 100644 index 00000000..8750385d --- /dev/null +++ b/rust/scrabble-score/src/lib.rs @@ -0,0 +1,28 @@ +/// Compute the Scrabble score for a word. + +/// Scores a scrabble word. +/// +/// ```rust +/// use scrabble_score::*; +/// +/// let want: u64 = 87; +/// let got: u64 = score("abcdefghijklmnopqrstuvwxyz"); +/// +/// assert_eq!(got, want); +/// ``` +pub fn score(word: &str) -> u64 { + word.to_lowercase() + .trim() + .chars() + .map(|c| match c { + '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, + _ => 0, + }) + .sum() +} diff --git a/rust/scrabble-score/tests/scrabble-score.rs b/rust/scrabble-score/tests/scrabble-score.rs new file mode 100644 index 00000000..8d6348a0 --- /dev/null +++ b/rust/scrabble-score/tests/scrabble-score.rs @@ -0,0 +1,63 @@ +use scrabble_score::*; + +#[test] +fn a_is_worth_one_point() { + assert_eq!(score("a"), 1); +} + +#[test] +fn scoring_is_case_insensitive() { + assert_eq!(score("A"), 1); +} + +#[test] +fn f_is_worth_four() { + assert_eq!(score("f"), 4); +} + +#[test] +fn two_one_point_letters_make_a_two_point_word() { + assert_eq!(score("at"), 2); +} + +#[test] +fn three_letter_word() { + assert_eq!(score("zoo"), 12); +} + +#[test] +fn medium_word() { + assert_eq!(score("street"), 6); +} + +#[test] +fn longer_words_with_valuable_letters() { + assert_eq!(score("quirky"), 22); +} + +#[test] +fn long_mixed_case_word() { + assert_eq!(score("OxyphenButazone"), 41); +} + +#[test] +fn non_english_scrabble_letters_do_not_score() { + assert_eq!(score("pinata"), 8, "'n' should score 1"); + assert_eq!(score("piñata"), 7, "'ñ' should score 0"); +} + +#[test] +fn empty_words_are_worth_zero() { + assert_eq!(score(""), 0); +} + +#[test] +fn all_letters_work() { + assert_eq!(score("abcdefghijklmnopqrstuvwxyz"), 87); +} + +#[test] +fn german_letters_do_not_score() { + assert_eq!(score("STRASSE"), 7, "\"SS\" should score 2"); + assert_eq!(score("STRAßE"), 5, "'ß' should score 0"); +}