Skip to content

Instantly share code, notes, and snippets.

@arcrose
Created January 7, 2018 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcrose/64e509d9836377edc2a2c4eaac025d31 to your computer and use it in GitHub Desktop.
Save arcrose/64e509d9836377edc2a2c4eaac025d31 to your computer and use it in GitHub Desktop.
(ns spacemacs-test.core)
(require '[clojure.string :as str])
; Want to parse strings like the following into some data structure that describes each game.
; E.g. Game 1, team 1 = Los Angeles Clippers, score 1 = 104, team 2 = Dallas Mavericks, score 2 = 88, winner = team 1
(def *example-input* "Los Angeles Clippers 104 Dallas Mavericks 88,New York Knicks 101 Atlanta Hawks 112,Indiana Pacers 103 Memphis Grizzlies 112, Los Angeles Clippers 100 Boston Celtics 12")
(defn- tokenize-game-reports [report-sheet]
(str/split report-sheet #","))
(defn- try-parse-int [string]
(try (read-string string)
(catch NumberFormatException _ nil)))
(defn- lex [[original-string parsed-value]]
(if (int? parsed-value)
{:kind :score, :value parsed-value}
{:kind :name, :value original-string}))
(defn- tokenize-game-details [game-sheet]
(let [ tokens (str/split game-sheet #" ")
, pairs (map vector tokens (map try-parse-int tokens))
]
(map lex pairs)))
(defn- reconstruct-game [game-tokens]
(let [ scores-first (reverse game-tokens)
, team2-score (:value (first scores-first))
, rest (drop 1 scores-first)
, team2-name-parts (map :value (take-while (fn [{:keys [kind]}] (= kind :name)) rest))
, team2-name (str/join " " (reverse team2-name-parts))
, team1-parts (drop (+ 1 (count team2-name-parts)) scores-first)
, team1-score (:value (first team1-parts))
, team1-name-parts (map :value (drop 1 team1-parts))
, team1-name (str/join " " (reverse team1-name-parts))
]
{ :team1-name team1-name
, :team1-score team1-score
, :team2-name team2-name
, :team2-score team2-score
}))
(let [ game-sheet "Los Angeles Clippers 104 Dallas Mavericks 88"
, tokens (tokenize-game-details game-sheet)
]
(reconstruct-game tokens))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment