Parallel Letter Frequency
This commit is contained in:
parent
3ca9867a11
commit
af89290bbc
45 changed files with 2862 additions and 0 deletions
47
rust/anagram/.exercism/config.json
Normal file
47
rust/anagram/.exercism/config.json
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
{
|
||||
"authors": [
|
||||
"EduardoBautista"
|
||||
],
|
||||
"contributors": [
|
||||
"andrewclarkson",
|
||||
"ashleygwilliams",
|
||||
"bobahop",
|
||||
"chancancode",
|
||||
"ClashTheBunny",
|
||||
"coriolinus",
|
||||
"cwhakes",
|
||||
"Dimkar3000",
|
||||
"EduardoBautista",
|
||||
"efx",
|
||||
"ErikSchierboom",
|
||||
"gris",
|
||||
"IanWhitney",
|
||||
"kytrinyx",
|
||||
"lutostag",
|
||||
"mkantor",
|
||||
"nfiles",
|
||||
"petertseng",
|
||||
"pminten",
|
||||
"quartsize",
|
||||
"rofrol",
|
||||
"stevejb71",
|
||||
"stringparser",
|
||||
"xakon",
|
||||
"ZapAnton"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"src/lib.rs",
|
||||
"Cargo.toml"
|
||||
],
|
||||
"test": [
|
||||
"tests/anagram.rs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.rs"
|
||||
]
|
||||
},
|
||||
"blurb": "Given a word and a list of possible anagrams, select the correct sublist.",
|
||||
"source": "Inspired by the Extreme Startup game",
|
||||
"source_url": "https://github.com/rchatley/extreme_startup"
|
||||
}
|
||||
2
rust/anagram/.gitignore
vendored
Normal file
2
rust/anagram/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
9
rust/anagram/Cargo.toml
Normal file
9
rust/anagram/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "anagram"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# Not all libraries from crates.io are available in Exercism's test runner.
|
||||
# The full list of available libraries is here:
|
||||
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
|
||||
[dependencies]
|
||||
86
rust/anagram/HELP.md
Normal file
86
rust/anagram/HELP.md
Normal file
|
|
@ -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
|
||||
6
rust/anagram/HINTS.md
Normal file
6
rust/anagram/HINTS.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# Hints
|
||||
|
||||
## General
|
||||
|
||||
The solution is case insensitive, which means `"WOrd"` is the same as `"word"` or `"woRd"`.
|
||||
It may help to take a peek at the [std library](https://doc.rust-lang.org/std/primitive.char.html) for functions that can convert between them.
|
||||
74
rust/anagram/README.md
Normal file
74
rust/anagram/README.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# Anagram
|
||||
|
||||
Welcome to Anagram on Exercism's Rust Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||
|
||||
## Introduction
|
||||
|
||||
At a garage sale, you find a lovely vintage typewriter at a bargain price!
|
||||
Excitedly, you rush home, insert a sheet of paper, and start typing away.
|
||||
However, your excitement wanes when you examine the output: all words are garbled!
|
||||
For example, it prints "stop" instead of "post" and "least" instead of "stale."
|
||||
Carefully, you try again, but now it prints "spot" and "slate."
|
||||
After some experimentation, you find there is a random delay before each letter is printed, which messes up the order.
|
||||
You now understand why they sold it for so little money!
|
||||
|
||||
You realize this quirk allows you to generate anagrams, which are words formed by rearranging the letters of another word.
|
||||
Pleased with your finding, you spend the rest of the day generating hundreds of anagrams.
|
||||
|
||||
## Instructions
|
||||
|
||||
Given a target word and one or more candidate words, your task is to find the candidates that are anagrams of the target.
|
||||
|
||||
An anagram is a rearrangement of letters to form a new word: for example `"owns"` is an anagram of `"snow"`.
|
||||
A word is _not_ its own anagram: for example, `"stop"` is not an anagram of `"stop"`.
|
||||
|
||||
The target word and candidate words are made up of one or more ASCII alphabetic characters (`A`-`Z` and `a`-`z`).
|
||||
Lowercase and uppercase characters are equivalent: for example, `"PoTS"` is an anagram of `"sTOp"`, but `"StoP"` is not an anagram of `"sTOp"`.
|
||||
The words you need to find should be taken from the candidate words, using the same letter case.
|
||||
|
||||
Given the target `"stone"` and the candidate words `"stone"`, `"tones"`, `"banana"`, `"tons"`, `"notes"`, and `"Seton"`, the anagram words you need to find are `"tones"`, `"notes"`, and `"Seton"`.
|
||||
|
||||
The Rust track extends the possible letters to be any unicode character, not just ASCII alphabetic ones.
|
||||
|
||||
You are going to have to adjust the function signature provided in the stub in order for the lifetimes to work out properly.
|
||||
This is intentional: what's there demonstrates the basics of lifetime syntax, and what's missing teaches how to interpret lifetime-related compiler errors.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @EduardoBautista
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @andrewclarkson
|
||||
- @ashleygwilliams
|
||||
- @bobahop
|
||||
- @chancancode
|
||||
- @ClashTheBunny
|
||||
- @coriolinus
|
||||
- @cwhakes
|
||||
- @Dimkar3000
|
||||
- @EduardoBautista
|
||||
- @efx
|
||||
- @ErikSchierboom
|
||||
- @gris
|
||||
- @IanWhitney
|
||||
- @kytrinyx
|
||||
- @lutostag
|
||||
- @mkantor
|
||||
- @nfiles
|
||||
- @petertseng
|
||||
- @pminten
|
||||
- @quartsize
|
||||
- @rofrol
|
||||
- @stevejb71
|
||||
- @stringparser
|
||||
- @xakon
|
||||
- @ZapAnton
|
||||
|
||||
### Based on
|
||||
|
||||
Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup
|
||||
8
rust/anagram/src/lib.rs
Normal file
8
rust/anagram/src/lib.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
pub fn anagrams_for<'a>(
|
||||
word: &str,
|
||||
possible_anagrams: &[&str]
|
||||
) -> HashSet<&'a str> {
|
||||
todo!("For the '{word}' word find anagrams among the following words: {possible_anagrams:?}");
|
||||
}
|
||||
188
rust/anagram/tests/anagram.rs
Normal file
188
rust/anagram/tests/anagram.rs
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
use anagram::*;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[test]
|
||||
fn no_matches() {
|
||||
let word = "diaper";
|
||||
let inputs = &["hello", "world", "zombies", "pants"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_two_anagrams() {
|
||||
let word = "solemn";
|
||||
let inputs = &["lemons", "cherry", "melons"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["lemons", "melons"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn does_not_detect_anagram_subsets() {
|
||||
let word = "good";
|
||||
let inputs = &["dog", "goody"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_anagram() {
|
||||
let word = "listen";
|
||||
let inputs = &["enlists", "google", "inlets", "banana"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["inlets"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_three_anagrams() {
|
||||
let word = "allergy";
|
||||
let inputs = &[
|
||||
"gallery",
|
||||
"ballerina",
|
||||
"regally",
|
||||
"clergy",
|
||||
"largely",
|
||||
"leading",
|
||||
];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["gallery", "regally", "largely"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_multiple_anagrams_with_different_case() {
|
||||
let word = "nose";
|
||||
let inputs = &["Eons", "ONES"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["Eons", "ONES"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn does_not_detect_non_anagrams_with_identical_checksum() {
|
||||
let word = "mass";
|
||||
let inputs = &["last"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_anagrams_case_insensitively() {
|
||||
let word = "Orchestra";
|
||||
let inputs = &["cashregister", "Carthorse", "radishes"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["Carthorse"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_anagrams_using_case_insensitive_subject() {
|
||||
let word = "Orchestra";
|
||||
let inputs = &["cashregister", "carthorse", "radishes"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["carthorse"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn detects_anagrams_using_case_insensitive_possible_matches() {
|
||||
let word = "orchestra";
|
||||
let inputs = &["cashregister", "Carthorse", "radishes"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["Carthorse"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn does_not_detect_an_anagram_if_the_original_word_is_repeated() {
|
||||
let word = "go";
|
||||
let inputs = &["goGoGO"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn anagrams_must_use_all_letters_exactly_once() {
|
||||
let word = "tapper";
|
||||
let inputs = &["patter"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn words_are_not_anagrams_of_themselves() {
|
||||
let word = "BANANA";
|
||||
let inputs = &["BANANA"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different() {
|
||||
let word = "BANANA";
|
||||
let inputs = &["Banana"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different() {
|
||||
let word = "BANANA";
|
||||
let inputs = &["banana"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn words_other_than_themselves_can_be_anagrams() {
|
||||
let word = "LISTEN";
|
||||
let inputs = &["LISTEN", "Silent"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["Silent"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn handles_case_of_greek_letters() {
|
||||
let word = "ΑΒΓ";
|
||||
let inputs = &["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter(["ΒΓΑ", "γβα"]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn different_characters_may_have_the_same_bytes() {
|
||||
let word = "a⬂";
|
||||
let inputs = &["€a"];
|
||||
let output = anagrams_for(word, inputs);
|
||||
let expected = HashSet::from_iter([]);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue