Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust anagram #249

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
- [triangle](./triangle/README.md)
- [allergies](./allergies/README.md)
- [difference-of-squares](./difference-of-squares/README.md)
- [anagram](./anagram/README.md)
47 changes: 47 additions & 0 deletions rust/anagram/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions rust/anagram/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"rust","exercise":"anagram","id":"742c3f6004134b3eb7f47d39b721e3d0","url":"https://exercism.org/tracks/rust/exercises/anagram","handle":"vpayno","is_requester":true,"auto_approve":false}
8 changes: 8 additions & 0 deletions rust/anagram/.gitignore
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions rust/anagram/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
edition = "2021"
name = "anagram"
version = "0.0.0"

[dependencies]
unicode-segmentation = "1.10.1"
85 changes: 85 additions & 0 deletions rust/anagram/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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)
- [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
66 changes: 66 additions & 0 deletions rust/anagram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Anagram

Welcome to Anagram on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

An anagram is a rearrangement of letters to form a new word.
Given a word and a list of candidates, select the sublist of anagrams of the given word.

Given `"listen"` and a list of candidates like `"enlists" "google"
"inlets" "banana"` the program should return a list containing
`"inlets"`.

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.

The solution cannot contain the input word. A word is always an anagram of itself, which means it is not an interesting result. Given `"hello"` and the list `["hello", "olleh"]` the answer is `["olleh"]`.

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.

Try to limit case changes. Case changes are expensive in terms of time, so it's faster to minimize them.

If sorting, consider [sort_unstable](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable) which is typically faster than stable sorting. When applicable, unstable sorting is preferred because it is generally faster than stable sorting and it doesn't allocate auxiliary memory.

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

### My Solution

- [my solution](./src/lib.rs)
- [run-tests](./run-tests-rust.txt)
142 changes: 142 additions & 0 deletions rust/anagram/coverage-annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
1| |use std::collections::HashSet;
2| |use unicode_segmentation::UnicodeSegmentation;
3| |
4| 42|pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
5| 42| let mut anagrams: HashSet<&'a str> = HashSet::new();
6| 42|
7| 42| let lc_word: String = word.to_lowercase().graphemes(true).collect();
8| 42| let mut vec_word: Vec<u16> = lc_word.encode_utf16().collect();
9| 42| vec_word.sort();
10| |
11| 42| let test_word: String = match String::from_utf16(&vec_word.clone()) {
12| 42| Ok(value) => value,
13| 0| Err(error) => {
14| 0| println!(
15| 0| "falied to convert test word {:?} to a string: {}",
16| 0| vec_word, error
17| 0| );
18| 0|
19| 0| return anagrams.clone();
20| | }
21| | };
22| |
23| 42| let test_length = vec_word.len();
24| 42|
25| 42| println!("\ntest_word: {}", word);
26| |
27| 138| for candidate in possible_anagrams {
^96
28| 96| let lc_candidate: String = candidate.to_lowercase().graphemes(true).collect();
29| 96|
30| 96| if lc_word.eq(&lc_candidate) {
31| 9| println!("{} == {} | skipping", lc_word, lc_candidate);
32| 9| continue;
33| 87| }
34| 87|
35| 87| let mut vec_candidate: Vec<u16> = lc_candidate.encode_utf16().collect();
36| 87| vec_candidate.sort();
37| |
38| 87| let test_candidate: String = match String::from_utf16(&vec_candidate.clone()) {
39| 87| Ok(value) => value,
40| 0| Err(error) => {
41| 0| println!(
42| 0| "falied to convert candidate {:?} to a string: {}",
43| 0| vec_candidate, error
44| 0| );
45| 0| continue;
46| | }
47| | };
48| |
49| 87| if test_length != vec_candidate.len() {
50| 39| continue;
51| 48| }
52| 48|
53| 48| println!(
54| 48| "test_word: {}\ttest_candidate: {}",
55| 48| test_word, test_candidate
56| 48| );
57| 48|
58| 48| if test_word.eq(&test_candidate) {
59| 24| println!("{} == {} | adding {}", test_word, test_candidate, candidate);
60| 24| anagrams.insert(*candidate);
61| 24| }
62| |
63| 48| println!();
64| | }
65| |
66| 42| println!("found anagrams: {:?}", anagrams);
67| 42|
68| 42| anagrams.clone()
69| 42|}
------------------
| Unexecuted instantiation: anagram::anagrams_for
------------------
| anagram::anagrams_for:
| 4| 42|pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
| 5| 42| let mut anagrams: HashSet<&'a str> = HashSet::new();
| 6| 42|
| 7| 42| let lc_word: String = word.to_lowercase().graphemes(true).collect();
| 8| 42| let mut vec_word: Vec<u16> = lc_word.encode_utf16().collect();
| 9| 42| vec_word.sort();
| 10| |
| 11| 42| let test_word: String = match String::from_utf16(&vec_word.clone()) {
| 12| 42| Ok(value) => value,
| 13| 0| Err(error) => {
| 14| 0| println!(
| 15| 0| "falied to convert test word {:?} to a string: {}",
| 16| 0| vec_word, error
| 17| 0| );
| 18| 0|
| 19| 0| return anagrams.clone();
| 20| | }
| 21| | };
| 22| |
| 23| 42| let test_length = vec_word.len();
| 24| 42|
| 25| 42| println!("\ntest_word: {}", word);
| 26| |
| 27| 138| for candidate in possible_anagrams {
| ^96
| 28| 96| let lc_candidate: String = candidate.to_lowercase().graphemes(true).collect();
| 29| 96|
| 30| 96| if lc_word.eq(&lc_candidate) {
| 31| 9| println!("{} == {} | skipping", lc_word, lc_candidate);
| 32| 9| continue;
| 33| 87| }
| 34| 87|
| 35| 87| let mut vec_candidate: Vec<u16> = lc_candidate.encode_utf16().collect();
| 36| 87| vec_candidate.sort();
| 37| |
| 38| 87| let test_candidate: String = match String::from_utf16(&vec_candidate.clone()) {
| 39| 87| Ok(value) => value,
| 40| 0| Err(error) => {
| 41| 0| println!(
| 42| 0| "falied to convert candidate {:?} to a string: {}",
| 43| 0| vec_candidate, error
| 44| 0| );
| 45| 0| continue;
| 46| | }
| 47| | };
| 48| |
| 49| 87| if test_length != vec_candidate.len() {
| 50| 39| continue;
| 51| 48| }
| 52| 48|
| 53| 48| println!(
| 54| 48| "test_word: {}\ttest_candidate: {}",
| 55| 48| test_word, test_candidate
| 56| 48| );
| 57| 48|
| 58| 48| if test_word.eq(&test_candidate) {
| 59| 24| println!("{} == {} | adding {}", test_word, test_candidate, candidate);
| 60| 24| anagrams.insert(*candidate);
| 61| 24| }
| 62| |
| 63| 48| println!();
| 64| | }
| 65| |
| 66| 42| println!("found anagrams: {:?}", anagrams);
| 67| 42|
| 68| 42| anagrams.clone()
| 69| 42|}
------------------
10 changes: 10 additions & 0 deletions rust/anagram/coverage-missing-lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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/anagram/src/lib.rs 23 2 91.30% 1 0 100.00% 54 13 75.93% 0 0 -
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 23 2 91.30% 1 0 100.00% 54 13 75.93% 0 0 -
Uncovered Lines:
/home/vpayno/git_vpayno/exercism-workspace/rust/anagram/src/lib.rs: 13, 14, 15, 16, 17, 18, 19, 40, 41, 42, 43, 44, 45

Loading