Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. (ns coinflip.core)
  2.  
  3. (defn flip []
  4. (if (> (rand) 0.5)
  5. 'heads
  6. 'tails))
  7.  
  8. (defn flip-coins [num-coins]
  9. (repeatedly num-coins flip))
  10.  
  11. (defn count-heads-and-tails [coin-list]
  12. {:heads (count (filter #(= % 'heads) coin-list))
  13. :tails (count (filter #(= % 'tails) coin-list))})
  14.  
  15. (defn split-list [coin-flips]
  16. (list (conj coin-flips 'heads) (conj coin-flips 'tails)))
  17.  
  18. (defn list-possible-sequences [num-flips]
  19. (loop [result '((heads) (tails))
  20. count num-flips]
  21. (if (= count 1) result
  22. (recur (apply concat (map split-list result)) (- count 1)))))
  23.  
  24. (defn ratio [coin-map]
  25. (let [heads (:heads coin-map)
  26. tails (:tails coin-map)]
  27. (if (= heads 0) 0
  28. (double (/ heads (+ heads tails))))))
  29.  
  30. (defn count-num-half [num-flips]
  31. "return the number of flips that had an equal number of heads and tails."
  32. (count (filter #(= 0.5 %) (map ratio (map count-heads-and-tails (list-possible-sequences num-flips))))))
  33.  
  34. (defn keep-on-flippin [num-attempts num-flips]
  35. (repeatedly num-attempts #(flip-coins num-flips)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement