Skip to content

Commit

Permalink
knapsack: fix generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 22, 2024
1 parent e6e4abe commit 1c0971a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
98 changes: 48 additions & 50 deletions exercises/practice/knapsack/KnapsackTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,74 @@ open Xunit
open Knapsack

[<Fact>]
let ``No items`` () = maximumValue [] 100 |> should equal 0
let ``No items`` () =
let items = []
maximumValue items 100 |> should equal 0

[<Fact(Skip = "Remove this Skip property to run this test")>]
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

[<Fact(Skip = "Remove this Skip property to run this test")>]
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

[<Fact(Skip = "Remove this Skip property to run this test")>]
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

[<Fact(Skip = "Remove this Skip property to run this test")>]
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

[<Fact(Skip = "Remove this Skip property to run this test")>]
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

[<Fact(Skip = "Remove this Skip property to run this test")>]
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

12 changes: 12 additions & 0 deletions generators/Generators.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,18 @@ type EliudsEggs() =

type Knapsack() =
inherit ExerciseGenerator()

let renderItem (item: JToken) =
let weight = item["weight"].ToObject<int>()
let value = item["value"].ToObject<int>()
$"{{ 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()
Expand Down

0 comments on commit 1c0971a

Please sign in to comment.