Whoops, I guess I don't understand bowling scoring as well as I
thought.  Now that I've read up a bit more on bowling scoring, I see
that if you get down to three rolls, (say 10, 7, 2) it must be scored
differently depending on whether it is a strike in the 9th frame
followed by 2 balls in the 10th frame or just three balls from the
10th frame (in the first scenario, the second and third ball get
counted twice).  So it looks like you really do need to assemble it
into a list of frame-by-frame scores.

Unfortunately, I misused the "is" macro (forgot to put =), which
prevented me from catching my incorrect tests.

The result is similar in spirit to Stuart's code, but arguably a bit
more compact.

(use 'clojure.contrib.test-is)

; A game is a sequence of numbers, representing how many pins were
knocked down for that roll

(defn sum [s] (reduce + s))
(defn frame-scores [game]
  (cond
    (< (count game) 3)      [(sum game)],
    (= (first game) 10)     (cons (sum (take 3 game)) (frame-scores
(drop 1 game))),
    (= (sum (take 2 game)) 10)  (cons (sum (take 3 game))
(frame-scores (drop 2 game))),
    :else (cons (sum (take 2 game)) (frame-scores (drop 2 game)))))
(defn score [games] (sum (take 10 (frame-scores games))))
(deftest sample-games
  (is (= (score [6 1 9 0 8 2 5 5 8 0 6 2 9 1 7 2 8 2 9 1 7]) 127))
  (is (= (score [10 10 7 3 8 2 10 9 1 10 10 10 10 7 3]) 232))
  (is (= (score [10 10 7 3 8 2 10 9 1 10 10 10 7 2]) 210))
  (is (= (score [5 5 8 2 9 1 7 3 8 2 6 4 9 1 7 3 6 4 4 5]) 163))
  (is (= (score [10 10 10 10 10 10 10 10 10 10 10 10]) 300)))

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to