From 1c0971a92d171c927b3db1a20acfbb352bb75f49 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Tue, 22 Oct 2024 15:34:49 +0200 Subject: [PATCH] knapsack: fix generator --- exercises/practice/knapsack/KnapsackTests.fs | 98 ++++++++++---------- generators/Generators.fs | 12 +++ 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/exercises/practice/knapsack/KnapsackTests.fs b/exercises/practice/knapsack/KnapsackTests.fs index 1da45f628..1b0c6e6c4 100644 --- a/exercises/practice/knapsack/KnapsackTests.fs +++ b/exercises/practice/knapsack/KnapsackTests.fs @@ -6,76 +6,74 @@ open Xunit open Knapsack [] -let ``No items`` () = maximumValue [] 100 |> should equal 0 +let ``No items`` () = + let items = [] + maximumValue items 100 |> should equal 0 [] let ``One item, too heavy`` () = - maximumValue [ { weight = 100; value = 1 } ] 10 - |> should equal 0 + let items = [{ weight = 100; value = 1 }] + maximumValue items 10 |> should equal 0 [] let ``Five items (cannot be greedy by weight)`` () = - maximumValue - [ { weight = 2; value = 5 } - { weight = 2; value = 5 } - { weight = 2; value = 5 } - { weight = 2; value = 5 } + let items = + [ { weight = 2; value = 5 }; + { weight = 2; value = 5 }; + { weight = 2; value = 5 }; + { weight = 2; value = 5 }; { weight = 10; value = 21 } ] - 10 - |> should equal 21 + maximumValue items 10 |> should equal 21 [] let ``Five items (cannot be greedy by value)`` () = - maximumValue - [ { weight = 2; value = 20 } - { weight = 2; value = 20 } - { weight = 2; value = 20 } - { weight = 2; value = 20 } + let items = + [ { weight = 2; value = 20 }; + { weight = 2; value = 20 }; + { weight = 2; value = 20 }; + { weight = 2; value = 20 }; { weight = 10; value = 50 } ] - 10 - |> should equal 80 + maximumValue items 10 |> should equal 80 [] let ``Example knapsack`` () = - maximumValue - [ { weight = 5; value = 10 } - { weight = 4; value = 40 } - { weight = 6; value = 30 } + let items = + [ { weight = 5; value = 10 }; + { weight = 4; value = 40 }; + { weight = 6; value = 30 }; { weight = 4; value = 50 } ] - 10 - |> should equal 90 + maximumValue items 10 |> should equal 90 [] let ``8 items`` () = - maximumValue - [ { weight = 25; value = 350 } - { weight = 35; value = 400 } - { weight = 45; value = 450 } - { weight = 5; value = 20 } - { weight = 25; value = 70 } - { weight = 3; value = 8 } - { weight = 2; value = 5 } + let items = + [ { weight = 25; value = 350 }; + { weight = 35; value = 400 }; + { weight = 45; value = 450 }; + { weight = 5; value = 20 }; + { weight = 25; value = 70 }; + { weight = 3; value = 8 }; + { weight = 2; value = 5 }; { weight = 2; value = 5 } ] - 104 - |> should equal 900 + maximumValue items 104 |> should equal 900 [] let ``15 items`` () = - maximumValue - [ { weight = 70; value = 135 } - { weight = 73; value = 139 } - { weight = 77; value = 149 } - { weight = 80; value = 150 } - { weight = 82; value = 156 } - { weight = 87; value = 163 } - { weight = 90; value = 173 } - { weight = 94; value = 184 } - { weight = 98; value = 192 } - { weight = 106; value = 201 } - { weight = 110; value = 210 } - { weight = 113; value = 214 } - { weight = 115; value = 221 } - { weight = 118; value = 229 } + let items = + [ { weight = 70; value = 135 }; + { weight = 73; value = 139 }; + { weight = 77; value = 149 }; + { weight = 80; value = 150 }; + { weight = 82; value = 156 }; + { weight = 87; value = 163 }; + { weight = 90; value = 173 }; + { weight = 94; value = 184 }; + { weight = 98; value = 192 }; + { weight = 106; value = 201 }; + { weight = 110; value = 210 }; + { weight = 113; value = 214 }; + { weight = 115; value = 221 }; + { weight = 118; value = 229 }; { weight = 120; value = 240 } ] - 750 - |> should equal 1458 + maximumValue items 750 |> should equal 1458 + diff --git a/generators/Generators.fs b/generators/Generators.fs index 7c02e6a9d..df181d71a 100644 --- a/generators/Generators.fs +++ b/generators/Generators.fs @@ -2044,6 +2044,18 @@ type EliudsEggs() = type Knapsack() = inherit ExerciseGenerator() + + let renderItem (item: JToken) = + let weight = item["weight"].ToObject() + let value = item["value"].ToObject() + $"{{ weight = {weight}; value = {value} }}" + + override _.PropertiesWithIdentifier _ = [ "items" ] + + override _.RenderInput(testCase, key, value) = + match key with + | "items" -> List.mapRenderMultiLine renderItem value + | _ -> base.RenderInput(testCase, key, value) type BottleSong() = inherit ExerciseGenerator()