From 3e3603c9bcfa55ff3bd3100a4d69aa4144a467aa Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Sun, 19 Nov 2023 22:04:30 +0100 Subject: [PATCH] Sync pythagorean-triplet with problem-specifications --- .../pythagorean-triplet/.docs/instructions.md | 7 +- .../pythagorean-triplet/.meta/config.json | 2 +- .../.meta/test_template.tera | 14 +++ .../pythagorean-triplet/.meta/tests.toml | 34 ++++++- .../tests/pythagorean-triplet.rs | 91 ++++++++++--------- 5 files changed, 99 insertions(+), 49 deletions(-) create mode 100644 exercises/practice/pythagorean-triplet/.meta/test_template.tera diff --git a/exercises/practice/pythagorean-triplet/.docs/instructions.md b/exercises/practice/pythagorean-triplet/.docs/instructions.md index 395ff6a55..1c1a8aea6 100644 --- a/exercises/practice/pythagorean-triplet/.docs/instructions.md +++ b/exercises/practice/pythagorean-triplet/.docs/instructions.md @@ -1,10 +1,9 @@ # Instructions -A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for -which, +A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which, ```text -a**2 + b**2 = c**2 +a² + b² = c² ``` and such that, @@ -16,7 +15,7 @@ a < b < c For example, ```text -3**2 + 4**2 = 9 + 16 = 25 = 5**2. +3² + 4² = 5². ``` Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`. diff --git a/exercises/practice/pythagorean-triplet/.meta/config.json b/exercises/practice/pythagorean-triplet/.meta/config.json index 6adbaf21a..8519cc3db 100644 --- a/exercises/practice/pythagorean-triplet/.meta/config.json +++ b/exercises/practice/pythagorean-triplet/.meta/config.json @@ -35,5 +35,5 @@ }, "blurb": "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product a * b * c.", "source": "Problem 9 at Project Euler", - "source_url": "http://projecteuler.net/problem=9" + "source_url": "https://projecteuler.net/problem=9" } diff --git a/exercises/practice/pythagorean-triplet/.meta/test_template.tera b/exercises/practice/pythagorean-triplet/.meta/test_template.tera new file mode 100644 index 000000000..642b1a760 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/test_template.tera @@ -0,0 +1,14 @@ +use std::collections::HashSet; +{% for test in cases %} +#[test] +{% if loop.index != 1 -%} +#[ignore] +{% endif -%} +fn {{ test.description | slugify | replace(from="-", to="_") }}() { + let input = {{ test.input.n | json_encode() }}; + let output = {{ crate_name }}::{{ fn_names[0] }}(input); + let expected = {{ test.expected | json_encode() }}; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); +} +{% endfor -%} diff --git a/exercises/practice/pythagorean-triplet/.meta/tests.toml b/exercises/practice/pythagorean-triplet/.meta/tests.toml index be690e975..719620a97 100644 --- a/exercises/practice/pythagorean-triplet/.meta/tests.toml +++ b/exercises/practice/pythagorean-triplet/.meta/tests.toml @@ -1,3 +1,31 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[a19de65d-35b8-4480-b1af-371d9541e706] +description = "triplets whose sum is 12" + +[48b21332-0a3d-43b2-9a52-90b2a6e5c9f5] +description = "triplets whose sum is 108" + +[dffc1266-418e-4daa-81af-54c3e95c3bb5] +description = "triplets whose sum is 1000" + +[5f86a2d4-6383-4cce-93a5-e4489e79b186] +description = "no matching triplets for 1001" + +[bf17ba80-1596-409a-bb13-343bdb3b2904] +description = "returns all matching triplets" + +[9d8fb5d5-6c6f-42df-9f95-d3165963ac57] +description = "several matching triplets" + +[f5be5734-8aa0-4bd1-99a2-02adcc4402b4] +description = "triplets for large number" diff --git a/exercises/practice/pythagorean-triplet/tests/pythagorean-triplet.rs b/exercises/practice/pythagorean-triplet/tests/pythagorean-triplet.rs index 45c255094..67722e3b9 100644 --- a/exercises/practice/pythagorean-triplet/tests/pythagorean-triplet.rs +++ b/exercises/practice/pythagorean-triplet/tests/pythagorean-triplet.rs @@ -1,76 +1,85 @@ -use 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]]); + let input = 12; + let output = pythagorean_triplet::find(input); + let expected = [[3, 4, 5]]; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); } #[test] #[ignore] fn triplets_whose_sum_is_108() { - process_tripletswithsum_case(108, &[[27, 36, 45]]); + let input = 108; + let output = pythagorean_triplet::find(input); + let expected = [[27, 36, 45]]; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); } #[test] #[ignore] fn triplets_whose_sum_is_1000() { - process_tripletswithsum_case(1000, &[[200, 375, 425]]); + let input = 1000; + let output = pythagorean_triplet::find(input); + let expected = [[200, 375, 425]]; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); } #[test] #[ignore] fn no_matching_triplets_for_1001() { - process_tripletswithsum_case(1001, &[]); + let input = 1001; + let output = pythagorean_triplet::find(input); + let expected = []; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); } #[test] #[ignore] fn returns_all_matching_triplets() { - process_tripletswithsum_case(90, &[[9, 40, 41], [15, 36, 39]]); + let input = 90; + let output = pythagorean_triplet::find(input); + let expected = [[9, 40, 41], [15, 36, 39]]; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); } #[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], - ], - ); + let input = 840; + let output = pythagorean_triplet::find(input); + let expected = [ + [40, 399, 401], + [56, 390, 394], + [105, 360, 375], + [120, 350, 370], + [140, 336, 364], + [168, 315, 357], + [210, 280, 350], + [240, 252, 348], + ]; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); } #[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], - ], - ); + let input = 30000; + let output = pythagorean_triplet::find(input); + let expected = [ + [1200, 14375, 14425], + [1875, 14000, 14125], + [5000, 12000, 13000], + [6000, 11250, 12750], + [7500, 10000, 12500], + ]; + let expected: HashSet<_> = expected.iter().cloned().collect(); + assert_eq!(output, expected); }