Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add grains exercise (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent eac4c15 commit 347e96c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@
],
"difficulty": 2
},
{
"slug": "grains",
"name": "Grains",
"uuid": "3a96556b-4ec8-431d-a5ee-99355065baec",
"practices": [],
"prerequisites": [
"numbers"
],
"difficulty": 3
},
{
"slug": "proverb",
"name": "Proverb",
Expand Down
15 changes: 15 additions & 0 deletions exercises/practice/grains/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Instructions

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince.
The king promised to pay whatever the servant could dream up.
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
One grain on the first square of a chess board, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

- how many grains were on a given square, and
- the total number of grains on the chessboard
19 changes: 19 additions & 0 deletions exercises/practice/grains/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/grains.cljs"
],
"test": [
"test/grains_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
"source": "The CodeRanch Cattle Drive, Assignment 6",
"source_url": "https://coderanch.com/wiki/718824/Grains"
}
16 changes: 16 additions & 0 deletions exercises/practice/grains/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns grains)

(defn- pow [x n]
(loop [x x n n r 1]
(cond
(= n 0) r
(even? n) (recur (* x x) (/ n 2) r)
:else (recur x (dec n) (* r x)))))

(defn square [number]
(pow 2 (dec number)))

(def ^:private square-numbers (rest (range 65)))

(defn total []
(apply + (map square square-numbers)))
10 changes: 10 additions & 0 deletions exercises/practice/grains/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
9 changes: 9 additions & 0 deletions exercises/practice/grains/src/grains.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns grains)

(defn square [] ;; <- arglist goes here
;; your code goes here
)

(defn total [] ;; <- arglist goes here
;; your code goes here
)
27 changes: 27 additions & 0 deletions exercises/practice/grains/test/grains_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns grains-test
(:require [clojure.test :refer [deftest is]]
grains))

(deftest square-1
(is (= 1 (grains/square 1))))

(deftest square-2
(is (= 2 (grains/square 2))))

(deftest square-3
(is (= 4 (grains/square 3))))

(deftest square-4
(is (= 8 (grains/square 4))))

(deftest square-16
(is (= 32768 (grains/square 16))))

(deftest square-32
(is (= 2147483648 (grains/square 32))))

(deftest square-64
(is (= 9223372036854775808 (grains/square 64))))

(deftest total-grains
(is (= 18446744073709551615 (grains/total))))

0 comments on commit 347e96c

Please sign in to comment.