diff options
| author | Federico I <git@federicoigne.com> | 2020-03-14 20:26:36 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:49:37 +0000 |
| commit | dede893978d55ec6ee0751808b44a0f647988110 (patch) | |
| tree | 68008ea3f2ce9493ee03dbb9110879bb3fcd1f59 | |
| parent | da0fec405edd592d9ae7c658cb80fdc652671c37 (diff) | |
| download | exercism-dede893978d55ec6ee0751808b44a0f647988110.tar.gz exercism-dede893978d55ec6ee0751808b44a0f647988110.zip | |
[rust] Reverse String
| -rw-r--r-- | reverse-string/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | reverse-string/.gitignore | 8 | ||||
| -rw-r--r-- | reverse-string/Cargo.toml | 10 | ||||
| -rw-r--r-- | reverse-string/README.md | 103 | ||||
| -rw-r--r-- | reverse-string/src/lib.rs | 5 | ||||
| -rw-r--r-- | reverse-string/tests/reverse-string.rs | 62 |
6 files changed, 189 insertions, 0 deletions
diff --git a/reverse-string/.exercism/metadata.json b/reverse-string/.exercism/metadata.json new file mode 100644 index 0000000..667d0ed --- /dev/null +++ b/reverse-string/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"reverse-string","id":"f735abfd7ff5444fb0f6c68c5fbef1b9","url":"https://exercism.io/my/solutions/f735abfd7ff5444fb0f6c68c5fbef1b9","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/reverse-string/.gitignore b/reverse-string/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/reverse-string/.gitignore | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # Generated by Cargo | ||
| 2 | # will have compiled files and executables | ||
| 3 | /target/ | ||
| 4 | **/*.rs.bk | ||
| 5 | |||
| 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
| 7 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| 8 | Cargo.lock | ||
diff --git a/reverse-string/Cargo.toml b/reverse-string/Cargo.toml new file mode 100644 index 0000000..bdf501c --- /dev/null +++ b/reverse-string/Cargo.toml | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [dependencies] | ||
| 2 | unicode-segmentation = "1.3.0" | ||
| 3 | |||
| 4 | [features] | ||
| 5 | grapheme = [] | ||
| 6 | |||
| 7 | [package] | ||
| 8 | edition = "2018" | ||
| 9 | name = "reverse_string" | ||
| 10 | version = "1.2.0" | ||
diff --git a/reverse-string/README.md b/reverse-string/README.md new file mode 100644 index 0000000..78e9753 --- /dev/null +++ b/reverse-string/README.md | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | # Reverse String | ||
| 2 | |||
| 3 | Reverse a string | ||
| 4 | |||
| 5 | For example: | ||
| 6 | input: "cool" | ||
| 7 | output: "looc" | ||
| 8 | |||
| 9 | ## Bonus | ||
| 10 | Test your function on this string: `uüu` and see what happens. Try to write a function that properly | ||
| 11 | reverses this string. Hint: grapheme clusters | ||
| 12 | |||
| 13 | To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the | ||
| 14 | last test, and execute the tests with: | ||
| 15 | |||
| 16 | ```bash | ||
| 17 | $ cargo test --features grapheme | ||
| 18 | ``` | ||
| 19 | |||
| 20 | You will need to use external libraries (a `crate` in rust lingo) for the bonus task. A good place to look for those is [crates.io](https://crates.io/), the official repository of crates. | ||
| 21 | |||
| 22 | [Check the documentation](https://doc.rust-lang.org/cargo/guide/dependencies.html) for instructions on how to use external crates in your projects. | ||
| 23 | |||
| 24 | |||
| 25 | ## Rust Installation | ||
| 26 | |||
| 27 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 28 | resources. | ||
| 29 | |||
| 30 | ## Writing the Code | ||
| 31 | |||
| 32 | Execute the tests with: | ||
| 33 | |||
| 34 | ```bash | ||
| 35 | $ cargo test | ||
| 36 | ``` | ||
| 37 | |||
| 38 | All but the first test have been ignored. After you get the first test to | ||
| 39 | pass, open the tests source file which is located in the `tests` directory | ||
| 40 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 41 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 42 | Continue, until you pass every test. | ||
| 43 | |||
| 44 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 45 | |||
| 46 | ```bash | ||
| 47 | $ cargo test -- --ignored | ||
| 48 | ``` | ||
| 49 | |||
| 50 | To run a specific test, for example `some_test`, you can use: | ||
| 51 | |||
| 52 | ```bash | ||
| 53 | $ cargo test some_test | ||
| 54 | ``` | ||
| 55 | |||
| 56 | If the specific test is ignored use: | ||
| 57 | |||
| 58 | ```bash | ||
| 59 | $ cargo test some_test -- --ignored | ||
| 60 | ``` | ||
| 61 | |||
| 62 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 63 | |||
| 64 | Make sure to read the [Modules][modules] chapter if you | ||
| 65 | haven't already, it will help you with organizing your files. | ||
| 66 | |||
| 67 | ## Further improvements | ||
| 68 | |||
| 69 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 70 | |||
| 71 | To format your solution, inside the solution directory use | ||
| 72 | |||
| 73 | ```bash | ||
| 74 | cargo fmt | ||
| 75 | ``` | ||
| 76 | |||
| 77 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 78 | |||
| 79 | ```bash | ||
| 80 | cargo clippy --all-targets | ||
| 81 | ``` | ||
| 82 | |||
| 83 | ## Submitting the solution | ||
| 84 | |||
| 85 | 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. | ||
| 86 | |||
| 87 | ## Feedback, Issues, Pull Requests | ||
| 88 | |||
| 89 | The [exercism/rust](https://github.com/exercism/rust) repository on 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! | ||
| 90 | |||
| 91 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 92 | |||
| 93 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 94 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 95 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 96 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 97 | |||
| 98 | ## Source | ||
| 99 | |||
| 100 | Introductory challenge to reverse an input string [https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb) | ||
| 101 | |||
| 102 | ## Submitting Incomplete Solutions | ||
| 103 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/reverse-string/src/lib.rs b/reverse-string/src/lib.rs new file mode 100644 index 0000000..f488a6e --- /dev/null +++ b/reverse-string/src/lib.rs | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | use unicode_segmentation::UnicodeSegmentation; | ||
| 2 | |||
| 3 | pub fn reverse(input: &str) -> String { | ||
| 4 | UnicodeSegmentation::graphemes(input, true).rev().collect() | ||
| 5 | } | ||
diff --git a/reverse-string/tests/reverse-string.rs b/reverse-string/tests/reverse-string.rs new file mode 100644 index 0000000..9b17bb0 --- /dev/null +++ b/reverse-string/tests/reverse-string.rs | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | //! Tests for reverse-string | ||
| 2 | //! | ||
| 3 | //! Generated by [script][script] using [canonical data][canonical-data] | ||
| 4 | //! | ||
| 5 | //! [script]: https://github.com/exercism/rust/blob/b829ce2/bin/init_exercise.py | ||
| 6 | //! [canonical-data]: https://raw.githubusercontent.com/exercism/problem-specifications/master/exercises/reverse-string/canonical_data.json | ||
| 7 | |||
| 8 | use reverse_string::*; | ||
| 9 | |||
| 10 | /// Process a single test case for the property `reverse` | ||
| 11 | fn process_reverse_case(input: &str, expected: &str) { | ||
| 12 | assert_eq!(&reverse(input), expected) | ||
| 13 | } | ||
| 14 | |||
| 15 | #[test] | ||
| 16 | /// empty string | ||
| 17 | fn test_an_empty_string() { | ||
| 18 | process_reverse_case("", ""); | ||
| 19 | } | ||
| 20 | |||
| 21 | #[test] | ||
| 22 | /// a word | ||
| 23 | fn test_a_word() { | ||
| 24 | process_reverse_case("robot", "tobor"); | ||
| 25 | } | ||
| 26 | |||
| 27 | #[test] | ||
| 28 | /// a capitalized word | ||
| 29 | fn test_a_capitalized_word() { | ||
| 30 | process_reverse_case("Ramen", "nemaR"); | ||
| 31 | } | ||
| 32 | |||
| 33 | #[test] | ||
| 34 | /// a sentence with punctuation | ||
| 35 | fn test_a_sentence_with_punctuation() { | ||
| 36 | process_reverse_case("I'm hungry!", "!yrgnuh m'I"); | ||
| 37 | } | ||
| 38 | |||
| 39 | #[test] | ||
| 40 | /// a palindrome | ||
| 41 | fn test_a_palindrome() { | ||
| 42 | process_reverse_case("racecar", "racecar"); | ||
| 43 | } | ||
| 44 | |||
| 45 | #[test] | ||
| 46 | /// an even-sized word | ||
| 47 | fn test_an_even_sized_word() { | ||
| 48 | process_reverse_case("drawer", "reward"); | ||
| 49 | } | ||
| 50 | |||
| 51 | #[test] | ||
| 52 | /// wide characters | ||
| 53 | fn test_wide_characters() { | ||
| 54 | process_reverse_case("子猫", "猫子"); | ||
| 55 | } | ||
| 56 | |||
| 57 | #[test] | ||
| 58 | #[cfg(feature = "grapheme")] | ||
| 59 | /// grapheme clusters | ||
| 60 | fn test_grapheme_clusters() { | ||
| 61 | process_reverse_case("uüu", "uüu"); | ||
| 62 | } | ||
