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

Add solution for greatest common divisor issue #446

Merged
merged 2 commits into from
Aug 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/battle_asserts/issues/greatest_common_divisor.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns battle-asserts.issues.greatest-common-divisor
(:require [clojure.test.check.generators :as gen]))

(def level :easy)

(def description "Create a function that calculates GCD (Greatest Common Divisor).")

(def signature
{:input [{:argument-name "x" :type {:name "integer"}}
{:argument-name "y" :type {:name "integer"}}]
:output {:type {:name "integer"}}})

(defn arguments-generator []
(gen/tuple (gen/choose 1 50) (gen/choose 1 50)))

(def test-data
[{:arguments [8 24]
:expected 8}
{:arguments [8 26]
:expected 2}
{:arguments [42 56]
:expected 14}
{:arguments [15 50]
:expected 5}])

(defn solution [x y]
(if (zero? y)
x
(recur y (mod x y))))
14 changes: 14 additions & 0 deletions test/battle_asserts/issues/greatest_common_divisor_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns battle-asserts.issues.greatest-common-divisor-test
(:require [clojure.test :refer :all]
[clojure.test.check.properties :as prop]
[clojure.test.check.clojure-test :as ct]
[test-helper :as h]
[battle-asserts.issues.greatest-common-divisor :as issue]))

(ct/defspec spec-solution
20
(prop/for-all [v (issue/arguments-generator)]
(instance? Number (apply issue/solution v))))

(deftest test-solution
(h/generate-tests issue/test-data issue/solution))