Skip to content

Palindrome Product Exercise #501

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

Merged
merged 24 commits into from
Apr 27, 2018
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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions exercises/palindrome-products/.meta/test-in-release-mode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Takes very long time in debug mode because of the nested loops which compute the palindrome products.
6 changes: 6 additions & 0 deletions exercises/palindrome-products/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "palindrome-products"
version = "1.1.0"
authors = ["Kirill Meng <[email protected]>"]

[dependencies]
72 changes: 72 additions & 0 deletions exercises/palindrome-products/README.md
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions exercises/palindrome-products/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub type Palindrome = u64;
pub fn get_palindrome_products(min: u64, max: u64) -> Vec<Palindrome> {
let mut palindromes: Vec<u64> = 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<Palindrome> {
palindromes.iter().min().map(|n| n.clone())
}

pub fn max(palindromes: &[Palindrome]) -> Option<Palindrome> {
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)
}
22 changes: 22 additions & 0 deletions exercises/palindrome-products/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub type Palindrome = u64;
pub fn get_palindrome_products(min: u64, max: u64) -> Vec<Palindrome> {
unimplemented!(
"Find all palindromic numbers which are products of numbers in the inclusive range ({}..{})",
min,
max
)
}

pub fn min(palindromes: &[Palindrome]) -> Option<Palindrome> {
unimplemented!(
"Return the palindrome of minimal value from the supplied list: {:?}",
palindromes
)
}

pub fn max(palindromes: &[Palindrome]) -> Option<Palindrome> {
unimplemented!(
"Return the palindrome of maximal value from the supplied list: {:?}",
palindromes
)
}
57 changes: 57 additions & 0 deletions exercises/palindrome-products/tests/palindrome-products.rs
Original file line number Diff line number Diff line change
@@ -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);
}