Skip to content

Commit

Permalink
Add solution for greatest common divisor issue (#446)
Browse files Browse the repository at this point in the history
* Add solution for greatest common divisor issue

* Fix typo

Co-authored-by: Vitaly <vtmilyakov@yandex.ru>
  • Loading branch information
solar05 and vtm9 committed Aug 12, 2020
1 parent 8671219 commit 3ebf0bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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))

0 comments on commit 3ebf0bf

Please sign in to comment.