Skip to content

Instantly share code, notes, and snippets.

@weejulius
Created May 2, 2013 11:21
Show Gist options
  • Save weejulius/5501587 to your computer and use it in GitHub Desktop.
Save weejulius/5501587 to your computer and use it in GitHub Desktop.
;;assert(succ("") == "") ;;assert(succ("R2D3") == "R2D4") ;;assert(succ("R2D9") == "R2E0") ;;assert(succ("A99") == "B00") ;;assert(succ("Z99") == "AA00") ;;assert(succ("Zz99") == "AAa00") ;;assert(succ("9Z") == "10A")
(defn increment
[str-chars]
(if ((complement empty?) str-chars)
(let [last-char (last str-chars)
ahead-chars (drop-last str-chars)]
(cond
(= last-char \9) (if (empty? ahead-chars) "10" (str (increment ahead-chars) \0))
(= last-char \z) (if (empty? ahead-chars) "aa" (str (increment ahead-chars) \a))
(= last-char \Z) (if (empty? ahead-chars) "AA" (str (increment ahead-chars) \A))
:else (apply str (conj (vec ahead-chars) (char (inc (int last-char))))))) ""))
;;assert(succ("") == "")
;;assert(succ("R2D3") == "R2D4")
;;assert(succ("R2D9") == "R2E0")
;;assert(succ("A99") == "B00")
;;assert(succ("Z99") == "AA00")
;;assert(succ("Zz99") == "AAa00")
;;assert(succ("9Z") == "10A")
(increment (char-array "9"))
(increment (char-array "Z"))
(increment (char-array ""))
(increment (char-array "R2D3"))
(increment (char-array "R2D9"))
(increment (char-array "A99"))
(increment (char-array "Z99"))
(increment (char-array "Zz99"))
(increment (char-array "9Z"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment