-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use exercism::isbn_verifier; | ||
use exercism::pythagorean_triplet; | ||
fn main() { | ||
isbn_verifier::is_valid_isbn("3-598-21507-X"); | ||
pythagorean_triplet::find(1001); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::collections::HashSet; | ||
|
||
pub fn find(sum: u32) -> HashSet<[u32; 3]> { | ||
let mut result: HashSet<[u32; 3]> = HashSet::new(); | ||
for a in 1..=sum / 2 { | ||
for b in a..sum / 2 { | ||
if a + b > sum { | ||
break; | ||
} | ||
|
||
let c = sum - b - a; | ||
if c < a || c < b { | ||
continue; | ||
} | ||
|
||
if a * a + b * b == c * c { | ||
result.insert([a, b, c]); | ||
} | ||
} | ||
} | ||
|
||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ mod allergies; | |
mod nucleotide_count; | ||
mod hamming; | ||
mod isbn_verifier; | ||
mod pythagorean_triplet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use exercism::pythagorean_triplet::find; | ||
use std::collections::HashSet; | ||
fn process_tripletswithsum_case(sum: u32, expected: &[[u32; 3]]) { | ||
let triplets = find(sum); | ||
if !expected.is_empty() { | ||
let expected: HashSet<_> = expected.iter().cloned().collect(); | ||
assert_eq!(expected, triplets); | ||
} else { | ||
assert!(triplets.is_empty()); | ||
} | ||
} | ||
#[test] | ||
fn triplets_whose_sum_is_12() { | ||
process_tripletswithsum_case(12, &[[3, 4, 5]]); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn triplets_whose_sum_is_108() { | ||
process_tripletswithsum_case(108, &[[27, 36, 45]]); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn triplets_whose_sum_is_1000() { | ||
process_tripletswithsum_case(1000, &[[200, 375, 425]]); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn no_matching_triplets_for_1001() { | ||
process_tripletswithsum_case(1001, &[]); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn returns_all_matching_triplets() { | ||
process_tripletswithsum_case(90, &[[9, 40, 41], [15, 36, 39]]); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn several_matching_triplets() { | ||
process_tripletswithsum_case( | ||
840, | ||
&[ | ||
[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] | ||
#[ignore] | ||
fn triplets_for_large_number() { | ||
process_tripletswithsum_case( | ||
30_000, | ||
&[ | ||
[1200, 14_375, 14_425], | ||
[1875, 14_000, 14_125], | ||
[5000, 12_000, 13_000], | ||
[6000, 11_250, 12_750], | ||
[7500, 10_000, 12_500], | ||
], | ||
); | ||
} |