Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

difference-of-squares: add generator and regenerate tests #735

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions exercises/practice/difference-of-squares/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns difference-of-squares)

(defn- square [n] (* n n))
(defn- sum [xs] (reduce + xs))

(defn sum-of-squares [n]
(sum (map #(int (Math/pow % 2)) (range 0 (inc n)))))
(sum (map square (range 1 (inc n)))))

(defn square-of-sum [n]
(int (Math/pow (sum (range 0 (inc n))) 2)))
(square (sum (range 1 (inc n)))))

(defn difference [x]
(- (square-of-sum x) (sum-of-squares x)))
7 changes: 7 additions & 0 deletions exercises/practice/difference-of-squares/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns difference-of-squares-generator)

(defn- update-path [path]
(take-last 1 path))

(defn transform [test-cases]
(map #(update % :path update-path) test-cases))
18 changes: 18 additions & 0 deletions exercises/practice/difference-of-squares/.meta/generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns difference-of-squares-test
(:require [clojure.test :refer [deftest testing is]]
difference-of-squares))
{{#test_cases.squareOfSum}}
(deftest square-of-sum_test_{{idx}}
(testing "{{description}}"
(is (= {{expected}} (difference-of-squares/square-of-sum {{input.number}})))))
{{/test_cases.squareOfSum~}}
{{#test_cases.sumOfSquares}}
(deftest sum-of-squares_test_{{idx}}
(testing "{{description}}"
(is (= {{expected}} (difference-of-squares/sum-of-squares {{input.number}})))))
{{/test_cases.sumOfSquares~}}
{{#test_cases.differenceOfSquares}}
(deftest difference_test_{{idx}}
(testing "{{description}}"
(is (= {{expected}} (difference-of-squares/difference {{input.number}})))))
{{/test_cases.differenceOfSquares~}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
difference-of-squares))

(deftest square-of-sum_test_1
(testing "Square the sum of the numbers up to the given number -> square of sum 1"
tasxatzial marked this conversation as resolved.
Show resolved Hide resolved
(testing "square of sum 1"
(is (= 1 (difference-of-squares/square-of-sum 1)))))

(deftest square-of-sum_test_2
(testing "Square the sum of the numbers up to the given number -> square of sum 5"
(testing "square of sum 5"
(is (= 225 (difference-of-squares/square-of-sum 5)))))

(deftest square-of-sum_test_3
(testing "Square the sum of the numbers up to the given number -> square of sum 100"
(testing "square of sum 100"
(is (= 25502500 (difference-of-squares/square-of-sum 100)))))

(deftest sum-of-squares_test_1
(testing "Sum the squares of the numbers up to the given number -> sum of squares 1"
(testing "sum of squares 1"
(is (= 1 (difference-of-squares/sum-of-squares 1)))))

(deftest sum-of-squares_test_2
(testing "Sum the squares of the numbers up to the given number -> sum of squares 5"
(testing "sum of squares 5"
(is (= 55 (difference-of-squares/sum-of-squares 5)))))

(deftest sum-of-squares_test_3
(testing "Sum the squares of the numbers up to the given number -> sum of squares 100"
(testing "sum of squares 100"
(is (= 338350 (difference-of-squares/sum-of-squares 100)))))

(deftest difference_test_1
(testing "Subtract sum of squares from square of sums -> difference of squares 1"
(testing "difference of squares 1"
(is (= 0 (difference-of-squares/difference 1)))))

(deftest difference_test_2
(testing "Subtract sum of squares from square of sums -> difference of squares 5"
(testing "difference of squares 5"
(is (= 170 (difference-of-squares/difference 5)))))

(deftest difference_test_3
(testing "Subtract sum of squares from square of sums -> difference of squares 100"
(testing "difference of squares 100"
(is (= 25164150 (difference-of-squares/difference 100)))))