1

I'm working on a code that takes a bowling score (in terms of the 10 frames), and evaluates it. An example of a string would be XXXXX6/XX7/XX5 where the score would be 248 or 62--43---2----X-- where the score would be 27.

My first idea was to try to split the string so that, it would group each frame together, and a strike separately as such; 62--43---2----X-- --> ((6 2) (--) (4 3) (--) (2 -)... (X) (--))

From there, it would be easier to evaluate the scores. But I can't find a way to do that using partition or partition-by. Can anyone help?

1
  • Yes I'm working in clojure like i mentioned in the prompt! Commented Mar 7, 2018 at 17:45

2 Answers 2

1

What you are probably looking for is re-seq, which will split a string into a seq of strings that match a regex. In your case try (re-seq #"X|\d\d|\d/|--|-\d|\d-" frames-str) which will match one of:

  • "X"
  • A pair of digits
  • A digit followed by a slash
  • "--"
  • A "-" and a number (in either order)

For your examples:

"XXXXX6/XX7/XX5"    -> ("X" "X" "X" "X" "X" "6/" "X" "X" "7/" "X" "X")
"62--43---2----X--" -> ("62" "--" "43" "--" "-2" "--" "--" "X" "--")
Sign up to request clarification or add additional context in comments.

2 Comments

your regex is an overkill. #"X|[\d\-][\d\-/]" would be enough.
That was the regex I started with actually, but I figured breaking it out into parts would be clearer in case the asker or another reader wasn't familiar with regex
1

How about an excuse to write a bowling score grammar/parser using instaparse:

(def bowl-score-parser
  (insta/parser
    "S = F F F F F F F F F 10TH
     F = OPEN | CLOSED
     OPEN = ROLL ROLL
     CLOSED = STRIKE | SPARE
     10TH = OPEN |
            SPARE (STRIKE | ROLL) |
            STRIKE (SPARE | ROLL ROLL) |
            STRIKE STRIKE (STRIKE | ROLL)
     STRIKE = 'X'
     SPARE = ROLL '/'
     ROLL = PINS | MISS
     PINS = #'[1-9]'
     MISS = '-'"))

This parses ten frames of a bowling score, with special rules for the tenth frame.

  1. Each of the first nine frames is either OPEN (pins left standing) or CLOSED (all pins down).
  2. OPEN is two ROLLs, which are either a number of pins down or a miss. CLOSED is either a STRIKE or SPARE.
  3. 10TH describes the possible states for the tenth frame, which is surprisingly confusing! I could've gotten something wrong here.

Here's the parser output for your test case:

(bowling-score-parser "XXXXX6/XX7/XX5")
=>
[:S
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:SPARE [:ROLL [:PINS "6"]] "/"]]]
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:STRIKE "X"]]]
 [:F [:CLOSED [:SPARE [:ROLL [:PINS "7"]] "/"]]]
 [:10TH [:STRIKE "X"] [:STRIKE "X"] [:ROLL [:PINS "5"]]]]

Note: this parser permits an invalid number of total pins in a frame, but you could make a more specific grammar/parser or handle that case post-parsing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.