diff --git a/config.json b/config.json index 92ffe5629..b5677c31d 100644 --- a/config.json +++ b/config.json @@ -767,6 +767,18 @@ "difficulty": 4, "topics": [] }, + { + "uuid": "8cdc3424-51da-4cae-a065-9982859d5b55", + "slug": "palindrome-products", + "core": false, + "unlocked_by": null, + "difficulty": 4, + "topics": [ + "string comparison", + "calculation", + "structs" + ] + }, { "uuid": "0a33f3ac-cedd-4a40-a132-9d044b0e9977", "slug": "poker", diff --git a/exercises/palindrome-products/.meta/test-in-release-mode b/exercises/palindrome-products/.meta/test-in-release-mode new file mode 100644 index 000000000..1371d9d0a --- /dev/null +++ b/exercises/palindrome-products/.meta/test-in-release-mode @@ -0,0 +1 @@ +Takes very long time in debug mode because of the nested loops which compute the palindrome products. diff --git a/exercises/palindrome-products/Cargo.toml b/exercises/palindrome-products/Cargo.toml new file mode 100644 index 000000000..0fc8ac2a9 --- /dev/null +++ b/exercises/palindrome-products/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "palindrome-products" +version = "1.1.0" +authors = ["Kirill Meng "] + +[dependencies] diff --git a/exercises/palindrome-products/README.md b/exercises/palindrome-products/README.md new file mode 100644 index 000000000..0b8520188 --- /dev/null +++ b/exercises/palindrome-products/README.md @@ -0,0 +1,72 @@ +# Palindrome Products + +Detect palindrome products in a given range. + +A palindromic number is a number that remains the same when its digits are +reversed. For example, `121` is a palindromic number but `112` is not. + +Given a range of numbers, find the largest and smallest palindromes which +are products of numbers within that range. + +Your solution should return the largest and smallest palindromes, along with the +factors of each within the range. If the largest or smallest palindrome has more +than one pair of factors within the range, then return all the pairs. + +## Example 1 + +Given the range `[1, 9]` (both inclusive)... + +And given the list of all possible products within this range: +`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]` + +The palindrome products are all single digit numbers (in this case): +`[1, 2, 3, 4, 5, 6, 7, 8, 9]` + +The smallest palindrome product is `1`. Its factors are `(1, 1)`. +The largest palindrome product is `9`. Its factors are `(1, 9)` and `(3, 3)`. + +## Example 2 + +Given the range `[10, 99]` (both inclusive)... + +The smallest palindrome product is `121`. Its factors are `(11, 11)`. +The largest palindrome product is `9009`. Its factors are `(91, 99)`. + +## 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, remove the ignore flag (`#[ignore]`) from the next test and get the tests +to pass again. The test file is located in the `tests` directory. You can +also remove the ignore flag from all the tests to get them to run all at once +if you wish. + +Make sure to read the [Modules](https://doc.rust-lang.org/book/second-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]: http://exercism.io/languages/rust +[modules]: https://doc.rust-lang.org/book/second-edition/ch07-00-modules.html +[cargo]: https://doc.rust-lang.org/book/second-edition/ch14-00-more-about-cargo.html + +## Source + +Problem 4 at Project Euler [http://projecteuler.net/problem=4](http://projecteuler.net/problem=4) + +## 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/palindrome-products/example.rs b/exercises/palindrome-products/example.rs new file mode 100644 index 000000000..067c3305e --- /dev/null +++ b/exercises/palindrome-products/example.rs @@ -0,0 +1,25 @@ +pub type Palindrome = u64; +pub fn get_palindrome_products(min: u64, max: u64) -> Vec { + let mut palindromes: Vec = Vec::new(); + for i in min..max + 1 { + for j in min..max + 1 { + if is_palindrome(i * j) { + palindromes.push(i * j); + } + } + } + palindromes +} + +pub fn min(palindromes: &[Palindrome]) -> Option { + palindromes.iter().min().map(|n| n.clone()) +} + +pub fn max(palindromes: &[Palindrome]) -> Option { + palindromes.iter().max().map(|n| n.clone()) +} + +fn is_palindrome(n: u64) -> bool { + let s = n.to_string().into_bytes(); + s.iter().zip(s.iter().rev()).all(|(c1, c2)| c1 == c2) +} diff --git a/exercises/palindrome-products/src/lib.rs b/exercises/palindrome-products/src/lib.rs new file mode 100644 index 000000000..a18110222 --- /dev/null +++ b/exercises/palindrome-products/src/lib.rs @@ -0,0 +1,22 @@ +pub type Palindrome = u64; +pub fn get_palindrome_products(min: u64, max: u64) -> Vec { + unimplemented!( + "Find all palindromic numbers which are products of numbers in the inclusive range ({}..{})", + min, + max + ) +} + +pub fn min(palindromes: &[Palindrome]) -> Option { + unimplemented!( + "Return the palindrome of minimal value from the supplied list: {:?}", + palindromes + ) +} + +pub fn max(palindromes: &[Palindrome]) -> Option { + unimplemented!( + "Return the palindrome of maximal value from the supplied list: {:?}", + palindromes + ) +} diff --git a/exercises/palindrome-products/tests/palindrome-products.rs b/exercises/palindrome-products/tests/palindrome-products.rs new file mode 100644 index 000000000..87975aef9 --- /dev/null +++ b/exercises/palindrome-products/tests/palindrome-products.rs @@ -0,0 +1,57 @@ +extern crate palindrome_products; +use palindrome_products::*; + +#[test] +fn single_digits() { + let palindromes = get_palindrome_products(1, 9); + assert_eq!(min(&palindromes), Some(1)); + assert_eq!(max(&palindromes), Some(9)); +} + +#[test] +#[ignore] +fn double_digits() { + let palindromes = get_palindrome_products(10, 99); + assert_eq!(min(&palindromes), Some(121)); + assert_eq!(max(&palindromes), Some(9009)); +} + +#[test] +#[ignore] +fn triple_digits() { + let palindromes = get_palindrome_products(100, 999); + assert_eq!(min(&palindromes), Some(10201)); + assert_eq!(max(&palindromes), Some(906609)); +} + +#[test] +#[ignore] +fn four_digits() { + let palindromes = get_palindrome_products(1000, 9999); + assert_eq!(min(&palindromes), Some(1002001)); + assert_eq!(max(&palindromes), Some(99000099)); +} + +#[test] +#[ignore] +fn empty_result_for_smallest_palindrome() { + assert_eq!(min(&get_palindrome_products(1002, 1003)), None); +} + +#[test] +#[ignore] +fn empty_result_for_largest_palindrome() { + assert_eq!(max(&get_palindrome_products(15, 15)), None); +} + +#[test] +#[ignore] +fn error_smallest_palindrome_when_min_gt_max() { + assert_eq!(min(&get_palindrome_products(1000, 1)), None); +} + +#[test] +#[ignore] +fn error_largest_palindrome_when_min_st_max() { + assert_eq!(max(&get_palindrome_products(2, 1)), None); +}