Skip to content

Commit

Permalink
Add solution for farm problem issue (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
solar05 committed Sep 2, 2020
1 parent 0cbf8ec commit 181597c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/battle_asserts/issues/farm_problem.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns battle-asserts.issues.farm-problem
(:require [clojure.test.check.generators :as gen]))

(def level :elementary)

(def description "A farmer is asking you to tell him how many legs can be counted among all his animals.
The farmer breeds three species:
`chickens = 2 legs`
`cows = 4 legs`
`pigs = 4 legs`
The farmer has counted his animals and he gives you a subtotal for each species.
You have to implement a function that returns the total number of legs of all the animals.")

(def signature
{:input [{:argument-name "chickens" :type {:name "integer"}}
{:argument-name "cows" :type {:name "integer"}}
{:argument-name "pigs" :type {:name "integer"}}]
:output {:type {:name "integer"}}})

(defn arguments-generator
[]
(gen/tuple gen/small-integer
gen/small-integer
gen/small-integer))

(def test-data
[{:expected 36 :arguments [2 3 5]}
{:expected 22 :arguments [1 2 3]}
{:expected 50 :arguments [5 2 8]}])

(defn solution
[chickens cows pigs]
(+ (* chickens 2) (* cows 4) (* pigs 4)))
15 changes: 15 additions & 0 deletions test/battle_asserts/issues/farm_problem_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns battle-asserts.issues.farm-problem-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.farm-problem :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 181597c

Please sign in to comment.