Skip to content

Commit a5dfd47

Browse files
DifferenceOfSquares Knapsack Generator (exercism#128)
1 parent e35c654 commit a5dfd47

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
lines changed

exercises/practice/difference-of-squares/DifferenceOfSquaresTest.lean

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ open LeanTest
55

66
def differenceOfSquaresTests : TestSuite :=
77
(TestSuite.empty "DifferenceOfSquares")
8-
|>.addTest "Square the sum of the numbers up to the given number - square of sum 1" (do
8+
|>.addTest "Square the sum of the numbers up to the given number -> square of sum 1" (do
99
return assertEqual 1 (DifferenceOfSquares.squareOfSum 1))
10-
|>.addTest "Square the sum of the numbers up to the given number - square of sum 5" (do
10+
|>.addTest "Square the sum of the numbers up to the given number -> square of sum 5" (do
1111
return assertEqual 225 (DifferenceOfSquares.squareOfSum 5))
12-
|>.addTest "Square the sum of the numbers up to the given number - square of sum 100" (do
12+
|>.addTest "Square the sum of the numbers up to the given number -> square of sum 100" (do
1313
return assertEqual 25502500 (DifferenceOfSquares.squareOfSum 100))
14-
|>.addTest "Sum the squares of the numbers up to the given number - sum of squares 1" (do
14+
|>.addTest "Sum the squares of the numbers up to the given number -> sum of squares 1" (do
1515
return assertEqual 1 (DifferenceOfSquares.sumOfSquares 1))
16-
|>.addTest "Sum the squares of the numbers up to the given number - sum of squares 5" (do
16+
|>.addTest "Sum the squares of the numbers up to the given number -> sum of squares 5" (do
1717
return assertEqual 55 (DifferenceOfSquares.sumOfSquares 5))
18-
|>.addTest "Sum the squares of the numbers up to the given number - sum of squares 100" (do
18+
|>.addTest "Sum the squares of the numbers up to the given number -> sum of squares 100" (do
1919
return assertEqual 338350 (DifferenceOfSquares.sumOfSquares 100))
20-
|>.addTest "Subtract sum of squares from square of sums - difference of squares 1" (do
20+
|>.addTest "Subtract sum of squares from square of sums -> difference of squares 1" (do
2121
return assertEqual 0 (DifferenceOfSquares.differenceOfSquares 1))
22-
|>.addTest "Subtract sum of squares from square of sums - difference of squares 5" (do
22+
|>.addTest "Subtract sum of squares from square of sums -> difference of squares 5" (do
2323
return assertEqual 170 (DifferenceOfSquares.differenceOfSquares 5))
24-
|>.addTest "Subtract sum of squares from square of sums - difference of squares 100" (do
24+
|>.addTest "Subtract sum of squares from square of sums -> difference of squares 100" (do
2525
return assertEqual 25164150 (DifferenceOfSquares.differenceOfSquares 100))
2626

2727
def main : IO UInt32 := do

generators/Generator/Generator.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import Generator.HammingGenerator
2626
import Generator.LineUpGenerator
2727
import Generator.TwoFerGenerator
2828
import Generator.YachtGenerator
29+
import Generator.DifferenceOfSquaresGenerator
30+
import Generator.KnapsackGenerator
2931
import Generator.PalindromeProductsGenerator
3032
import Generator.EliudsEggsGenerator
3133
import Generator.EtlGenerator
@@ -113,6 +115,8 @@ def dispatch : Std.HashMap String (introGenerator × testCaseGenerator × endBod
113115
("LineUp", (LineUpGenerator.genIntro, LineUpGenerator.genTestCase, LineUpGenerator.genEnd)),
114116
("TwoFer", (TwoFerGenerator.genIntro, TwoFerGenerator.genTestCase, TwoFerGenerator.genEnd)),
115117
("Yacht", (YachtGenerator.genIntro, YachtGenerator.genTestCase, YachtGenerator.genEnd)),
118+
("DifferenceOfSquares", (DifferenceOfSquaresGenerator.genIntro, DifferenceOfSquaresGenerator.genTestCase, DifferenceOfSquaresGenerator.genEnd)),
119+
("Knapsack", (KnapsackGenerator.genIntro, KnapsackGenerator.genTestCase, KnapsackGenerator.genEnd)),
116120
("PalindromeProducts", (PalindromeProductsGenerator.genIntro, PalindromeProductsGenerator.genTestCase, PalindromeProductsGenerator.genEnd)),
117121
("EliudsEggs", (EliudsEggsGenerator.genIntro, EliudsEggsGenerator.genTestCase, EliudsEggsGenerator.genEnd)),
118122
("Etl", (EtlGenerator.genIntro, EtlGenerator.genTestCase, EtlGenerator.genEnd)),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Lean.Data.Json
2+
import Std
3+
import Helper
4+
5+
open Lean
6+
open Std
7+
open Helper
8+
9+
namespace DifferenceOfSquaresGenerator
10+
11+
def genIntro (exercise : String) : String := s!"import LeanTest
12+
import {exercise}
13+
14+
open LeanTest
15+
16+
def {exercise.decapitalize}Tests : TestSuite :=
17+
(TestSuite.empty \"{exercise}\")"
18+
19+
def genTestCase (exercise : String) (case : TreeMap.Raw String Json) : String :=
20+
let input := case.get! "input"
21+
let expected := case.get! "expected"
22+
let description := case.get! "description"
23+
|> (·.compress)
24+
let funName := getFunName (case.get! "property")
25+
let call := s!"({exercise}.{funName} {insertAllInputs input})"
26+
s!"
27+
|>.addTest {description} (do
28+
return assertEqual {expected} {call})"
29+
30+
def genEnd (exercise : String) : String :=
31+
s!"
32+
33+
def main : IO UInt32 := do
34+
runTestSuitesWithExitCode [{exercise.decapitalize}Tests]
35+
"
36+
37+
end DifferenceOfSquaresGenerator
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Lean.Data.Json
2+
import Std
3+
import Helper
4+
5+
open Lean
6+
open Std
7+
open Helper
8+
9+
namespace KnapsackGenerator
10+
11+
def genIntro (exercise : String) : String := s!"import LeanTest
12+
import {exercise}
13+
14+
open LeanTest
15+
16+
def {exercise.decapitalize}Tests : TestSuite :=
17+
(TestSuite.empty \"{exercise}\")"
18+
19+
def serializeItem (item : Json) : String :=
20+
let weight := item.getObjValD "weight" |>.getInt? |> getOk |> intLiteral
21+
let value := item.getObjValD "value" |>.getInt? |> getOk |> intLiteral
22+
"{ weight := " ++ weight ++ ", value := " ++ value ++ " }"
23+
24+
def genTestCase (exercise : String) (case : TreeMap.Raw String Json) : String :=
25+
let input := case.get! "input"
26+
let maximumWeight := input.getObjValD "maximumWeight"
27+
let items := serializeList (input.getObjValD "items") serializeItem
28+
let expected := case.get! "expected"
29+
let description := case.get! "description"
30+
|> (·.compress)
31+
let funName := getFunName (case.get! "property")
32+
let call := s!"({exercise}.{funName} {maximumWeight} #{items})"
33+
s!"
34+
|>.addTest {description} (do
35+
return assertEqual {expected} {call})"
36+
37+
def genEnd (exercise : String) : String :=
38+
s!"
39+
40+
def main : IO UInt32 := do
41+
runTestSuitesWithExitCode [{exercise.decapitalize}Tests]
42+
"
43+
44+
end KnapsackGenerator

0 commit comments

Comments
 (0)