Skip to content

Instantly share code, notes, and snippets.

@mfikes

mfikes/core.clj Secret

Created November 4, 2014 21:15
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 mfikes/17ec1cfcdddfdba2b2f4 to your computer and use it in GitHub Desktop.
Save mfikes/17ec1cfcdddfdba2b2f4 to your computer and use it in GitHub Desktop.
Bank OCR User Story 2
(ns bank-ocr2.core)
(def ^:private rendered-digit-lines
"A sequence of 3 lines representing all of the digits in order."
[" _ _ _ _ _ _ _ _ "
"| | | _| _||_||_ |_ ||_||_|"
"|_| ||_ _| | _||_| ||_| _| "])
(defn- lines->digit-representations
"Converts a sequence of 3 lines into a sequence of representations
of the digits on those lines, where each digit representation is
a sequence of character triplets."
[lines]
(apply map list (map #(partition 3 %) lines)))
(def ^:private digit-representation->digit
"A map from digit representation to digit."
(zipmap (lines->digit-representations rendered-digit-lines)
(range 10)))
(defn- lines->account-number
"Takes a sequence of 3 lines and converts them into the sequence of digits
for a bank account number."
[lines]
(map digit-representation->digit (lines->digit-representations lines)))
(defn- lines->account-numbers
"Takes a sequence of lines (representing multiple account numbers with
blank lines in between) and produces a sequence of account numbers (each
represented as a sequence of digits)."
[lines]
(map lines->account-number
(partition 3 4 lines)))
(defn- account-number->string
"Converts an account number (a sequence of digits) to a string (for display)."
[account-number]
(apply str account-number))
(defn- account-number-valid?
"Determines whether an account number is valid"
[account-number]
(zero? (mod (apply +
(map * account-number
(range 9 0 -1)))
11)))
(defn print-account-numbers
"Prints the account numbers in a file."
[file]
(with-open [r (clojure.java.io/reader file)]
(doseq [account-number (lines->account-numbers (line-seq r))]
(println (account-number->string account-number)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment