From ed1191f15e96fb170f3383395cf2f88081472929 Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Fri, 10 May 2019 08:18:41 +0200 Subject: [PATCH 1/4] Revert "two-fer, hexadecimal, nucleotide-codons: Removed the deprecated exercises" This reverts commit 0cef40fc952323bb9aae3d2c3fdb1747df812462. --- exercises/hexadecimal/.gitignore | 8 ++ exercises/hexadecimal/Cargo.toml | 3 + exercises/hexadecimal/README.md | 68 ++++++++++++ exercises/hexadecimal/example.rs | 33 ++++++ exercises/hexadecimal/src/lib.rs | 1 + exercises/hexadecimal/tests/hexadecimal.rs | 60 ++++++++++ exercises/nucleotide-codons/.gitignore | 8 ++ exercises/nucleotide-codons/Cargo.toml | 3 + exercises/nucleotide-codons/README.md | 75 +++++++++++++ exercises/nucleotide-codons/example.rs | 46 ++++++++ exercises/nucleotide-codons/src/lib.rs | 1 + exercises/nucleotide-codons/tests/codons.rs | 117 ++++++++++++++++++++ exercises/two-fer/.gitignore | 8 ++ exercises/two-fer/Cargo.toml | 5 + exercises/two-fer/README.md | 86 ++++++++++++++ exercises/two-fer/example.rs | 6 + exercises/two-fer/src/lib.rs | 3 + exercises/two-fer/tests/two-fer.rs | 19 ++++ 18 files changed, 550 insertions(+) create mode 100644 exercises/hexadecimal/.gitignore create mode 100644 exercises/hexadecimal/Cargo.toml create mode 100644 exercises/hexadecimal/README.md create mode 100644 exercises/hexadecimal/example.rs create mode 100644 exercises/hexadecimal/src/lib.rs create mode 100644 exercises/hexadecimal/tests/hexadecimal.rs create mode 100644 exercises/nucleotide-codons/.gitignore create mode 100644 exercises/nucleotide-codons/Cargo.toml create mode 100644 exercises/nucleotide-codons/README.md create mode 100644 exercises/nucleotide-codons/example.rs create mode 100644 exercises/nucleotide-codons/src/lib.rs create mode 100644 exercises/nucleotide-codons/tests/codons.rs create mode 100644 exercises/two-fer/.gitignore create mode 100644 exercises/two-fer/Cargo.toml create mode 100644 exercises/two-fer/README.md create mode 100644 exercises/two-fer/example.rs create mode 100644 exercises/two-fer/src/lib.rs create mode 100644 exercises/two-fer/tests/two-fer.rs diff --git a/exercises/hexadecimal/.gitignore b/exercises/hexadecimal/.gitignore new file mode 100644 index 000000000..db7f315c0 --- /dev/null +++ b/exercises/hexadecimal/.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/exercises/hexadecimal/Cargo.toml b/exercises/hexadecimal/Cargo.toml new file mode 100644 index 000000000..313084bc0 --- /dev/null +++ b/exercises/hexadecimal/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "hexadecimal" +version = "0.0.0" diff --git a/exercises/hexadecimal/README.md b/exercises/hexadecimal/README.md new file mode 100644 index 000000000..69ef4cd9d --- /dev/null +++ b/exercises/hexadecimal/README.md @@ -0,0 +1,68 @@ +# Hexadecimal + +Convert a hexadecimal number, represented as a string (e.g. "10af8c"), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion). + +On the web we use hexadecimal to represent colors, e.g. green: 008000, +teal: 008080, navy: 000080). + +The program should handle invalid hexadecimal strings. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +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 all tests without editing the tests source file, use: + +```bash +$ cargo test -- --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] + +Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you +haven't already, it will help you with organizing your files. + +## Feedback, Issues, Pull Requests + +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! + +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). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html +[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html + +## Source + +All of Computer Science [http://www.wolframalpha.com/examples/NumberBases.html](http://www.wolframalpha.com/examples/NumberBases.html) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/hexadecimal/example.rs b/exercises/hexadecimal/example.rs new file mode 100644 index 000000000..f539b3c9c --- /dev/null +++ b/exercises/hexadecimal/example.rs @@ -0,0 +1,33 @@ +fn parse_hex_digit(c: char) -> Option { + match c { + '0' => Some(0), + '1' => Some(1), + '2' => Some(2), + '3' => Some(3), + '4' => Some(4), + '5' => Some(5), + '6' => Some(6), + '7' => Some(7), + '8' => Some(8), + '9' => Some(9), + 'a' => Some(10), + 'b' => Some(11), + 'c' => Some(12), + 'd' => Some(13), + 'e' => Some(14), + 'f' => Some(15), + _ => None, + } +} + +pub fn hex_to_int(string: &str) -> Option { + let base: i64 = 16; + + string + .chars() + .rev() + .enumerate() + .fold(Some(0), |acc, (pos, c)| { + parse_hex_digit(c).and_then(|n| acc.map(|acc| acc + n * base.pow(pos as u32))) + }) +} diff --git a/exercises/hexadecimal/src/lib.rs b/exercises/hexadecimal/src/lib.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/hexadecimal/src/lib.rs @@ -0,0 +1 @@ + diff --git a/exercises/hexadecimal/tests/hexadecimal.rs b/exercises/hexadecimal/tests/hexadecimal.rs new file mode 100644 index 000000000..757b6887f --- /dev/null +++ b/exercises/hexadecimal/tests/hexadecimal.rs @@ -0,0 +1,60 @@ +extern crate hexadecimal; + +#[test] +fn test_hex_1_is_decimal_1() { + assert_eq!(Some(1), hexadecimal::hex_to_int("1")); +} + +#[test] +#[ignore] +fn test_hex_c_is_decimal_12() { + assert_eq!(Some(12), hexadecimal::hex_to_int("c")); +} + +#[test] +#[ignore] +fn test_hex_10_is_decimal_16() { + assert_eq!(Some(16), hexadecimal::hex_to_int("10")); +} + +#[test] +#[ignore] +fn test_hex_af_is_decimal_175() { + assert_eq!(Some(175), hexadecimal::hex_to_int("af")); +} + +#[test] +#[ignore] +fn test_hex_100_is_decimal_256() { + assert_eq!(Some(256), hexadecimal::hex_to_int("100")); +} + +#[test] +#[ignore] +fn test_hex_19ace_is_decimal_105166() { + assert_eq!(Some(105166), hexadecimal::hex_to_int("19ace")); +} + +#[test] +#[ignore] +fn test_invalid_hex_is_none() { + assert_eq!(None, hexadecimal::hex_to_int("carrot")); +} + +#[test] +#[ignore] +fn test_black() { + assert_eq!(Some(0), hexadecimal::hex_to_int("0000000")); +} + +#[test] +#[ignore] +fn test_white() { + assert_eq!(Some(16777215), hexadecimal::hex_to_int("ffffff")); +} + +#[test] +#[ignore] +fn test_yellow() { + assert_eq!(Some(16776960), hexadecimal::hex_to_int("ffff00")); +} diff --git a/exercises/nucleotide-codons/.gitignore b/exercises/nucleotide-codons/.gitignore new file mode 100644 index 000000000..db7f315c0 --- /dev/null +++ b/exercises/nucleotide-codons/.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/exercises/nucleotide-codons/Cargo.toml b/exercises/nucleotide-codons/Cargo.toml new file mode 100644 index 000000000..ff77d8534 --- /dev/null +++ b/exercises/nucleotide-codons/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "nucleotide_codons" +version = "0.1.0" diff --git a/exercises/nucleotide-codons/README.md b/exercises/nucleotide-codons/README.md new file mode 100644 index 000000000..1c8e6b1e4 --- /dev/null +++ b/exercises/nucleotide-codons/README.md @@ -0,0 +1,75 @@ +# Nucleotide Codons + +Write a function that returns the name of an amino acid a particular codon, +possibly using shorthand, encodes for. + +In DNA sequences of 3 nucleotides, called codons, encode for amino acids. Often +several codons encode for the same amino acid. The International Union of Pure +and Applied Chemistry developed a shorthand system for designating groups of +codons that encode for the same amino acid. + +Simply put they've expanded the four letters A, C, G and T with a bunch of +letters that stand for different possibilities. For example R means A and G. +So TAR stands for TAA and TAG (think of "TAR" as "TA[AG]" in regex notation). + +Write some code that given a codon, which may use shorthand, returns the +name of the amino acid that that codon encodes for. You will be given +a list of non-shorthand-codon/name pairs to base your computation on. + +See: [wikipedia](https://en.wikipedia.org/wiki/DNA_codon_table). + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +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 all tests without editing the tests source file, use: + +```bash +$ cargo test -- --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] + +Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you +haven't already, it will help you with organizing your files. + +## Feedback, Issues, Pull Requests + +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! + +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). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html +[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/nucleotide-codons/example.rs b/exercises/nucleotide-codons/example.rs new file mode 100644 index 000000000..0577d8166 --- /dev/null +++ b/exercises/nucleotide-codons/example.rs @@ -0,0 +1,46 @@ +use std::collections::HashMap; + +pub struct CodonInfo<'a> { + actual_codons: HashMap<&'a str, &'a str>, +} + +pub fn parse<'a>(pairs: Vec<(&'a str, &'a str)>) -> CodonInfo<'a> { + CodonInfo { + actual_codons: pairs.into_iter().collect(), + } +} + +impl<'a> CodonInfo<'a> { + pub fn name_for(&self, codon: &str) -> Result<&'a str, &'static str> { + if codon.len() != 3 { + return Err("invalid length"); + } + + let mut valid = true; + let lookup: String = codon + .chars() + .map(|l| { + // Get an example of a "letter" represented by the possibly encoded letter. + // Since every codon represented by the compressed notation has to be of + // the desired amino acid just picking one at random will do. + match l { + 'A' | 'W' | 'M' | 'R' | 'D' | 'H' | 'V' | 'N' => 'A', + 'C' | 'S' | 'Y' | 'B' => 'C', + 'G' | 'K' => 'G', + 'T' => 'T', + _ => { + valid = false; + ' ' + } + } + }) + .collect(); + if !valid { + return Err("invalid char"); + } + + // If the input table is correct (which it is) every valid codon is in it + // so unwrap() shouldn't panic. + Ok(self.actual_codons.get(&lookup.as_ref()).unwrap()) + } +} diff --git a/exercises/nucleotide-codons/src/lib.rs b/exercises/nucleotide-codons/src/lib.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/exercises/nucleotide-codons/src/lib.rs @@ -0,0 +1 @@ + diff --git a/exercises/nucleotide-codons/tests/codons.rs b/exercises/nucleotide-codons/tests/codons.rs new file mode 100644 index 000000000..acd310f1c --- /dev/null +++ b/exercises/nucleotide-codons/tests/codons.rs @@ -0,0 +1,117 @@ +extern crate nucleotide_codons as codons; + +#[test] +fn test_methionine() { + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("ATG"), Ok("methionine")); +} + +#[test] +#[ignore] +fn test_cysteine_tgt() { + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("TGT"), Ok("cysteine")); +} + +#[test] +#[ignore] +fn test_cysteine_tgy() { + // "compressed" name for TGT and TGC + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("TGT"), info.name_for("TGY")); + assert_eq!(info.name_for("TGC"), info.name_for("TGY")); +} + +#[test] +#[ignore] +fn test_stop() { + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("TAA"), Ok("stop codon")); +} + +#[test] +#[ignore] +fn test_valine() { + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("GTN"), Ok("valine")); +} + +#[test] +#[ignore] +fn test_isoleucine() { + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("ATH"), Ok("isoleucine")); +} + +#[test] +#[ignore] +fn test_arginine_name() { + // In arginine CGA can be "compressed" both as CGN and as MGR + let info = codons::parse(make_pairs()); + assert_eq!(info.name_for("CGA"), Ok("arginine")); + assert_eq!(info.name_for("CGN"), Ok("arginine")); + assert_eq!(info.name_for("MGR"), Ok("arginine")); +} + +#[test] +#[ignore] +fn empty_is_invalid() { + let info = codons::parse(make_pairs()); + assert!(info.name_for("").is_err()); +} + +#[test] +#[ignore] +fn x_is_not_shorthand_so_is_invalid() { + let info = codons::parse(make_pairs()); + assert!(info.name_for("VWX").is_err()); +} + +#[test] +#[ignore] +fn too_short_is_invalid() { + let info = codons::parse(make_pairs()); + assert!(info.name_for("AT").is_err()); +} + +#[test] +#[ignore] +fn too_long_is_invalid() { + let info = codons::parse(make_pairs()); + assert!(info.name_for("ATTA").is_err()); +} + +// The input data constructor. Returns a list of codon, name pairs. +fn make_pairs() -> Vec<(&'static str, &'static str)> { + let grouped = vec![ + ("isoleucine", vec!["ATT", "ATC", "ATA"]), + ("leucine", vec!["CTT", "CTC", "CTA", "CTG", "TTA", "TTG"]), + ("valine", vec!["GTT", "GTC", "GTA", "GTG"]), + ("phenylalanine", vec!["TTT", "TTC"]), + ("methionine", vec!["ATG"]), + ("cysteine", vec!["TGT", "TGC"]), + ("alanine", vec!["GCT", "GCC", "GCA", "GCG"]), + ("glycine", vec!["GGT", "GGC", "GGA", "GGG"]), + ("proline", vec!["CCT", "CCC", "CCA", "CCG"]), + ("threonine", vec!["ACT", "ACC", "ACA", "ACG"]), + ("serine", vec!["TCT", "TCC", "TCA", "TCG", "AGT", "AGC"]), + ("tyrosine", vec!["TAT", "TAC"]), + ("tryptophan", vec!["TGG"]), + ("glutamine", vec!["CAA", "CAG"]), + ("asparagine", vec!["AAT", "AAC"]), + ("histidine", vec!["CAT", "CAC"]), + ("glutamic acid", vec!["GAA", "GAG"]), + ("aspartic acid", vec!["GAT", "GAC"]), + ("lysine", vec!["AAA", "AAG"]), + ("arginine", vec!["CGT", "CGC", "CGA", "CGG", "AGA", "AGG"]), + ("stop codon", vec!["TAA", "TAG", "TGA"]), + ]; + let mut pairs = Vec::<(&'static str, &'static str)>::new(); + for (name, codons) in grouped.into_iter() { + for codon in codons { + pairs.push((codon, name)); + } + } + pairs.sort_by(|&(_, a), &(_, b)| a.cmp(b)); + return pairs; +} diff --git a/exercises/two-fer/.gitignore b/exercises/two-fer/.gitignore new file mode 100644 index 000000000..db7f315c0 --- /dev/null +++ b/exercises/two-fer/.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/exercises/two-fer/Cargo.toml b/exercises/two-fer/Cargo.toml new file mode 100644 index 000000000..c80ccc6a8 --- /dev/null +++ b/exercises/two-fer/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "twofer" +version = "1.2.0" + +[dependencies] diff --git a/exercises/two-fer/README.md b/exercises/two-fer/README.md new file mode 100644 index 000000000..ff76e3197 --- /dev/null +++ b/exercises/two-fer/README.md @@ -0,0 +1,86 @@ +# Two Fer + +`Two-fer` or `2-fer` is short for two for one. One for you and one for me. + +Given a name, return a string with the message: + +```text +One for X, one for me. +``` + +Where X is the given name. + +However, if the name is missing, return the string: + +```text +One for you, one for me. +``` + +Here are some examples: + +|Name | String to return +|:------:|:-----------------: +|Alice | One for Alice, one for me. +|Bob | One for Bob, one for me. +| | One for you, one for me. +|Zaphod | One for Zaphod, one for me. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +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 all tests without editing the tests source file, use: + +```bash +$ cargo test -- --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] + +Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you +haven't already, it will help you with organizing your files. + +## Feedback, Issues, Pull Requests + +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! + +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). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html +[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html + +## Source + +[https://github.com/exercism/problem-specifications/issues/757](https://github.com/exercism/problem-specifications/issues/757) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/two-fer/example.rs b/exercises/two-fer/example.rs new file mode 100644 index 000000000..7b4221dd6 --- /dev/null +++ b/exercises/two-fer/example.rs @@ -0,0 +1,6 @@ +pub fn twofer(name: &str) -> String { + match name { + "" => "One for you, one for me.".to_string(), + _ => format!("One for {}, one for me.", name), + } +} diff --git a/exercises/two-fer/src/lib.rs b/exercises/two-fer/src/lib.rs new file mode 100644 index 000000000..62086679a --- /dev/null +++ b/exercises/two-fer/src/lib.rs @@ -0,0 +1,3 @@ +pub fn twofer(name: &str) -> String { + unimplemented!("One for {}, one for me.", name); +} diff --git a/exercises/two-fer/tests/two-fer.rs b/exercises/two-fer/tests/two-fer.rs new file mode 100644 index 000000000..3946d7c11 --- /dev/null +++ b/exercises/two-fer/tests/two-fer.rs @@ -0,0 +1,19 @@ +extern crate twofer; +use twofer::twofer; + +#[test] +fn empty_string() { + assert_eq!(twofer(""), "One for you, one for me."); +} + +#[test] +#[ignore] +fn alice() { + assert_eq!(twofer("Alice"), "One for Alice, one for me."); +} + +#[test] +#[ignore] +fn bob() { + assert_eq!(twofer("Bob"), "One for Bob, one for me."); +} From 0f94c916748ebde53ecab8bd09bd68bdbc27b5fe Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Fri, 10 May 2019 08:19:13 +0200 Subject: [PATCH 2/4] Revert "Forego formerly deprecated exercises" This reverts commit 78f94b0655dc700b5f94450f6a7cdd23bcab91b7. --- config.json | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/config.json b/config.json index 72a06a46c..a3e984e38 100644 --- a/config.json +++ b/config.json @@ -4,11 +4,8 @@ "blurb": "Rust is a compiled programming language designed for speed, concurrency, and memory safety. Rust programs can run almost anywhere, from low-power embedded devices to web servers.", "foregone": [ "binary", - "hexadecimal", - "nucleotide-codons", "octal", - "trinary", - "two-fer" + "trinary" ], "exercises": [ { @@ -1069,6 +1066,36 @@ "topics": [ "parser_reimplementation" ] + }, + { + "slug": "two-fer", + "uuid": "c6631a2c-4632-11e8-842f-0ed5f89f718b", + "core": false, + "unlocked_by": null, + "difficulty": 1, + "topics": [ + "match", + "strings" + ], + "deprecated": true + }, + { + "slug": "nucleotide-codons", + "uuid": "8dae8f4d-368d-477d-907e-bf746921bfbf", + "core": false, + "unlocked_by": null, + "difficulty": 0, + "topics": null, + "deprecated": true + }, + { + "slug": "hexadecimal", + "uuid": "496fd79f-1678-4aa2-8110-c32c6aaf545e", + "core": false, + "unlocked_by": null, + "difficulty": 0, + "topics": null, + "deprecated": true } ] } From 8a68502893967dc706d67c593ea34aa2e61d7ee1 Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Fri, 10 May 2019 16:33:16 +0200 Subject: [PATCH 3/4] Fix CI complaints about deprecated exercises - regenerate readmes - copy examples to lib.rs so they compile --- exercises/hexadecimal/README.md | 28 ++++++++++++--- exercises/hexadecimal/src/lib.rs | 32 +++++++++++++++++ exercises/nucleotide-codons/README.md | 28 ++++++++++++--- exercises/nucleotide-codons/src/lib.rs | 45 +++++++++++++++++++++++ exercises/two-fer/README.md | 49 +++++++++++++++----------- exercises/two-fer/src/lib.rs | 5 ++- 6 files changed, 157 insertions(+), 30 deletions(-) diff --git a/exercises/hexadecimal/README.md b/exercises/hexadecimal/README.md index 69ef4cd9d..1651fe77a 100644 --- a/exercises/hexadecimal/README.md +++ b/exercises/hexadecimal/README.md @@ -46,9 +46,29 @@ $ cargo test some_test -- --ignored To learn more about Rust tests refer to the [online test documentation][rust-tests] -Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you +Make sure to read the [Modules](https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html) chapter if you haven't already, it will help you with organizing your files. +## Further improvements + +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. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## 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 [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! @@ -56,9 +76,9 @@ The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is th 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). [help-page]: https://exercism.io/tracks/rust/learning -[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html -[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html -[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html +[modules]: https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html ## Source diff --git a/exercises/hexadecimal/src/lib.rs b/exercises/hexadecimal/src/lib.rs index 8b1378917..f539b3c9c 100644 --- a/exercises/hexadecimal/src/lib.rs +++ b/exercises/hexadecimal/src/lib.rs @@ -1 +1,33 @@ +fn parse_hex_digit(c: char) -> Option { + match c { + '0' => Some(0), + '1' => Some(1), + '2' => Some(2), + '3' => Some(3), + '4' => Some(4), + '5' => Some(5), + '6' => Some(6), + '7' => Some(7), + '8' => Some(8), + '9' => Some(9), + 'a' => Some(10), + 'b' => Some(11), + 'c' => Some(12), + 'd' => Some(13), + 'e' => Some(14), + 'f' => Some(15), + _ => None, + } +} +pub fn hex_to_int(string: &str) -> Option { + let base: i64 = 16; + + string + .chars() + .rev() + .enumerate() + .fold(Some(0), |acc, (pos, c)| { + parse_hex_digit(c).and_then(|n| acc.map(|acc| acc + n * base.pow(pos as u32))) + }) +} diff --git a/exercises/nucleotide-codons/README.md b/exercises/nucleotide-codons/README.md index 1c8e6b1e4..10b5b4d6c 100644 --- a/exercises/nucleotide-codons/README.md +++ b/exercises/nucleotide-codons/README.md @@ -57,9 +57,29 @@ $ cargo test some_test -- --ignored To learn more about Rust tests refer to the [online test documentation][rust-tests] -Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you +Make sure to read the [Modules](https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html) chapter if you haven't already, it will help you with organizing your files. +## Further improvements + +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. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## 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 [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! @@ -67,9 +87,9 @@ The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is th 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). [help-page]: https://exercism.io/tracks/rust/learning -[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html -[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html -[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html +[modules]: https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/nucleotide-codons/src/lib.rs b/exercises/nucleotide-codons/src/lib.rs index 8b1378917..0577d8166 100644 --- a/exercises/nucleotide-codons/src/lib.rs +++ b/exercises/nucleotide-codons/src/lib.rs @@ -1 +1,46 @@ +use std::collections::HashMap; +pub struct CodonInfo<'a> { + actual_codons: HashMap<&'a str, &'a str>, +} + +pub fn parse<'a>(pairs: Vec<(&'a str, &'a str)>) -> CodonInfo<'a> { + CodonInfo { + actual_codons: pairs.into_iter().collect(), + } +} + +impl<'a> CodonInfo<'a> { + pub fn name_for(&self, codon: &str) -> Result<&'a str, &'static str> { + if codon.len() != 3 { + return Err("invalid length"); + } + + let mut valid = true; + let lookup: String = codon + .chars() + .map(|l| { + // Get an example of a "letter" represented by the possibly encoded letter. + // Since every codon represented by the compressed notation has to be of + // the desired amino acid just picking one at random will do. + match l { + 'A' | 'W' | 'M' | 'R' | 'D' | 'H' | 'V' | 'N' => 'A', + 'C' | 'S' | 'Y' | 'B' => 'C', + 'G' | 'K' => 'G', + 'T' => 'T', + _ => { + valid = false; + ' ' + } + } + }) + .collect(); + if !valid { + return Err("invalid char"); + } + + // If the input table is correct (which it is) every valid codon is in it + // so unwrap() shouldn't panic. + Ok(self.actual_codons.get(&lookup.as_ref()).unwrap()) + } +} diff --git a/exercises/two-fer/README.md b/exercises/two-fer/README.md index ff76e3197..31aecaccf 100644 --- a/exercises/two-fer/README.md +++ b/exercises/two-fer/README.md @@ -2,28 +2,15 @@ `Two-fer` or `2-fer` is short for two for one. One for you and one for me. -Given a name, return a string with the message: - ```text -One for X, one for me. +"One for X, one for me." ``` -Where X is the given name. - -However, if the name is missing, return the string: - -```text -One for you, one for me. -``` +When X is a name or "you". -Here are some examples: +If the given name is "Alice", the result should be "One for Alice, one for me." +If no name is given, the result should be "One for you, one for me." -|Name | String to return -|:------:|:-----------------: -|Alice | One for Alice, one for me. -|Bob | One for Bob, one for me. -| | One for you, one for me. -|Zaphod | One for Zaphod, one for me. ## Rust Installation @@ -64,9 +51,29 @@ $ cargo test some_test -- --ignored To learn more about Rust tests refer to the [online test documentation][rust-tests] -Make sure to read the [Modules](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you +Make sure to read the [Modules](https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html) chapter if you haven't already, it will help you with organizing your files. +## Further improvements + +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. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## 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 [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! @@ -74,9 +81,9 @@ The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is th 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). [help-page]: https://exercism.io/tracks/rust/learning -[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html -[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html -[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html +[modules]: https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html ## Source diff --git a/exercises/two-fer/src/lib.rs b/exercises/two-fer/src/lib.rs index 62086679a..7b4221dd6 100644 --- a/exercises/two-fer/src/lib.rs +++ b/exercises/two-fer/src/lib.rs @@ -1,3 +1,6 @@ pub fn twofer(name: &str) -> String { - unimplemented!("One for {}, one for me.", name); + match name { + "" => "One for you, one for me.".to_string(), + _ => format!("One for {}, one for me.", name), + } } From 92fe95f281ea461cb8123f6dd4e121b974a3467b Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Sat, 11 May 2019 20:14:17 +0200 Subject: [PATCH 4/4] regeneate twofer readme (again?) --- exercises/two-fer/README.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/exercises/two-fer/README.md b/exercises/two-fer/README.md index 31aecaccf..d8cda081c 100644 --- a/exercises/two-fer/README.md +++ b/exercises/two-fer/README.md @@ -2,15 +2,28 @@ `Two-fer` or `2-fer` is short for two for one. One for you and one for me. +Given a name, return a string with the message: + ```text -"One for X, one for me." +One for X, one for me. ``` -When X is a name or "you". +Where X is the given name. + +However, if the name is missing, return the string: + +```text +One for you, one for me. +``` -If the given name is "Alice", the result should be "One for Alice, one for me." -If no name is given, the result should be "One for you, one for me." +Here are some examples: +|Name |String to return +|:-------|:------------------ +|Alice |One for Alice, one for me. +|Bob |One for Bob, one for me. +| |One for you, one for me. +|Zaphod |One for Zaphod, one for me. ## Rust Installation