Skip to content

pythagorean-triplet: Updated the test suite to the version 1.0.0 #713

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 7 commits into from
Nov 16, 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
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@
"unlocked_by": null,
"difficulty": 1,
"topics": [
"math",
"option"
"math"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions exercises/pythagorean-triplet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[dependencies]

[package]
name = "pythagorean_triplet"
version = "0.0.0"

[dependencies]
version = "1.0.0"
18 changes: 12 additions & 6 deletions exercises/pythagorean-triplet/example.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
pub fn find() -> Option<u32> {
for a in 1..1000 {
for b in (a + 1)..(1000 - a) {
let c = 1000 - (a + b);
use std::collections::HashSet;

pub fn find(sum: u32) -> HashSet<[u32; 3]> {
let mut triplets = HashSet::new();

for a in 1..sum {
for b in (a + 1)..(sum - a) {
let c = sum - (a + b);

if a * a + b * b == c * c {
return Some(a * b * c);
triplets.insert([a, b, c]);
}
}
}
None

triplets
}
6 changes: 4 additions & 2 deletions exercises/pythagorean-triplet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub fn find() -> Option<u32> {
unimplemented!();
use std::collections::HashSet;

pub fn find(sum: u32) -> HashSet<[u32; 3]> {
unimplemented!("Given the sum {}, return all possible Pythagorean triplets, which produce the said sum, or an empty HashSet if there are no such triplets. Note that you are expected to return triplets in [a, b, c] order, where a < b < c", sum);
}
76 changes: 74 additions & 2 deletions exercises/pythagorean-triplet/tests/pythagorean-triplet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
extern crate pythagorean_triplet;

use pythagorean_triplet::find;
use std::{collections::HashSet, iter::FromIterator};

fn process_tripletswithsum_case(sum: u32, expected: Vec<[u32; 3]>) {
let triplets = find(sum);

if !expected.is_empty() {
let expected = HashSet::from_iter(expected.iter().cloned());

assert_eq!(expected, triplets);
} else {
assert!(triplets.is_empty());
}
}

#[test]
fn test_triplets_whose_sum_is_12() {
process_tripletswithsum_case(12, vec![[3, 4, 5]]);
}

#[test]
#[ignore]
fn test_triplets_whose_sum_is_108() {
process_tripletswithsum_case(108, vec![[27, 36, 45]]);
}

#[test]
#[ignore]
fn test_triplets_whose_sum_is_1000() {
process_tripletswithsum_case(1000, vec![[200, 375, 425]]);
}

#[test]
#[ignore]
fn test_no_matching_triplets_for_1001() {
process_tripletswithsum_case(1001, vec![]);
}

#[test]
#[ignore]
fn test_returns_all_matching_triplets() {
process_tripletswithsum_case(90, vec![[9, 40, 41], [15, 36, 39]]);
}

#[test]
#[ignore]
fn test_several_matching_triplets() {
process_tripletswithsum_case(
840,
vec![
[40, 399, 401],
[56, 390, 394],
[105, 360, 375],
[120, 350, 370],
[140, 336, 364],
[168, 315, 357],
[210, 280, 350],
[240, 252, 348],
],
);
}

#[test]
fn test_answer() {
assert_eq!(pythagorean_triplet::find(), Some(31875000));
#[ignore]
fn test_triplets_for_large_number() {
process_tripletswithsum_case(
30000,
vec![
[1200, 14375, 14425],
[1875, 14000, 14125],
[5000, 12000, 13000],
[6000, 11250, 12750],
[7500, 10000, 12500],
],
);
}