Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns coinflip.core)
- (defn flip []
- (if (> (rand) 0.5)
- 'heads
- 'tails))
- (defn flip-coins [num-coins]
- (repeatedly num-coins flip))
- (defn count-heads-and-tails [coin-list]
- {:heads (count (filter #(= % 'heads) coin-list))
- :tails (count (filter #(= % 'tails) coin-list))})
- (defn split-list [coin-flips]
- (list (conj coin-flips 'heads) (conj coin-flips 'tails)))
- (defn list-possible-sequences [num-flips]
- (loop [result '((heads) (tails))
- count num-flips]
- (if (= count 1) result
- (recur (apply concat (map split-list result)) (- count 1)))))
- (defn ratio [coin-map]
- (let [heads (:heads coin-map)
- tails (:tails coin-map)]
- (if (= heads 0) 0
- (double (/ heads (+ heads tails))))))
- (defn count-num-half [num-flips]
- "return the number of flips that had an equal number of heads and tails."
- (count (filter #(= 0.5 %) (map ratio (map count-heads-and-tails (list-possible-sequences num-flips))))))
- (defn keep-on-flippin [num-attempts num-flips]
- (repeatedly num-attempts #(flip-coins num-flips)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement