Skip to content

Instantly share code, notes, and snippets.

@wonja
Last active February 5, 2016 19:33
Show Gist options
  • Save wonja/184d51849c3543d1e38d to your computer and use it in GitHub Desktop.
Save wonja/184d51849c3543d1e38d to your computer and use it in GitHub Desktop.
;; inspiration: http://mike.place/2016/extranapkins/
;; number conversion code taken from http://codewars.com/kumite/53e6a24ec4d201e407000473?sel=53ee1b32f611ce423b000cf6
;; implementation below
(declare convert)
(defn iter [num]
(reduce #(format %2 (convert (count (clojure.string/split %1 #"\s"))) %1) "Fuck off" (repeat num "I've got %s words for you: %s")))
(iter 100)
;;"I've got Seven Hundred Fifty-Nine words for you: I've got Seven Hundred Fifty-One words for you: I've got Seven Hundred Fourty-Three words for you: I've got Seven Hundred Thirty-Five words for you: I've got Seven Hundred Twenty-Seven words for you: I've got Seven Hundred Nineteen words for you: I've got Seven Hundred Eleven words for you: I've got Seven Hundred Three words for you: I've got Six Hundred Ninety-Five words for you: I've got Six Hundred Eighty-Seven words for you: I've got Six Hundred Seventy-Nine words for you: I've got Six Hundred Seventy-One words for you: I've got Six Hundred Sixty-Three words for you: I've got Six Hundred Fifty-Five words for you: I've got Six Hundred Fourty-Seven words for you: I've got Six Hundred Thirty-Nine words for you: I've got Six Hundred Thirty-One words for you: I've got Six Hundred Twenty-Three words for you: I've got Six Hundred Fifteen words for you: I've got Six Hundred Seven words for you: I've got Five Hundred Ninety-Nine words for you: I've got Five Hundred Ninety-One words for you: I've got Five Hundred Eighty-Three words for you: I've got Five Hundred Seventy-Five words for you: I've got Five Hundred Sixty-Seven words for you: I've got Five Hundred Fifty-Nine words for you: I've got Five Hundred Fifty-One words for you: I've got Five Hundred Fourty-Three words for you: I've got Five Hundred Thirty-Five words for you: I've got Five Hundred Twenty-Seven words for you: I've got Five Hundred Nineteen words for you: I've got Five Hundred Eleven words for you: I've got Five Hundred Three words for you: I've got Four Hundred Ninety-Five words for you: I've got Four Hundred Eighty-Seven words for you: I've got Four Hundred Seventy-Nine words for you: I've got Four Hundred Seventy-One words for you: I've got Four Hundred Sixty-Three words for you: I've got Four Hundred Fifty-Five words for you: I've got Four Hundred Fourty-Seven words for you: I've got Four Hundred Thirty-Nine words for you: I've got Four Hundred Thirty-One words for you: I've got Four Hundred Twenty-Three words for you: I've got Four Hundred Fifteen words for you: I've got Four Hundred Seven words for you: I've got Three Hundred Ninety-Nine words for you: I've got Three Hundred Ninety-One words for you: I've got Three Hundred Eighty-Three words for you: I've got Three Hundred Seventy-Five words for you: I've got Three Hundred Sixty-Seven words for you: I've got Three Hundred Fifty-Nine words for you: I've got Three Hundred Fifty-One words for you: I've got Three Hundred Fourty-Three words for you: I've got Three Hundred Thirty-Five words for you: I've got Three Hundred Twenty-Seven words for you: I've got Three Hundred Nineteen words for you: I've got Three Hundred Eleven words for you: I've got Three Hundred Three words for you: I've got Two Hundred Ninety-Five words for you: I've got Two Hundred Eighty-Seven words for you: I've got Two Hundred Seventy-Nine words for you: I've got Two Hundred Seventy-One words for you: I've got Two Hundred Sixty-Three words for you: I've got Two Hundred Fifty-Five words for you: I've got Two Hundred Fourty-Seven words for you: I've got Two Hundred Thirty-Nine words for you: I've got Two Hundred Thirty-One words for you: I've got Two Hundred Twenty-Three words for you: I've got Two Hundred Fifteen words for you: I've got Two Hundred Seven words for you: I've got Two Hundred words for you: I've got One Hundred Ninety-Two words for you: I've got One Hundred Eighty-Four words for you: I've got One Hundred Seventy-Six words for you: I've got One Hundred Sixty-Eight words for you: I've got One Hundred Sixty words for you: I've got One Hundred Fifty-Two words for you: I've got One Hundred Fourty-Four words for you: I've got One Hundred Thirty-Six words for you: I've got One Hundred Twenty-Eight words for you: I've got One Hundred Twenty words for you: I've got One Hundred Twelve words for you: I've got One Hundred Four words for you: I've got Ninety-Eight words for you: I've got Ninety-Two words for you: I've got Eighty-Six words for you: I've got Eighty words for you: I've got Seventy-Four words for you: I've got Sixty-Eight words for you: I've got Sixty-Two words for you: I've got Fifty-Six words for you: I've got Fifty words for you: I've got Fourty-Four words for you: I've got Thirty-Eight words for you: I've got Thirty-Two words for you: I've got Twenty-Six words for you: I've got Twenty words for you: I've got Fourteen words for you: I've got Eight words for you: I've got Two words for you: Fuck off"
(def digit-to-word
{1 "One" 2 "Two" 3 "Three" 4 "Four"
5 "Five" 6 "Six" 7 "Seven" 8 "Eight"
9 "Nine"})
(def tens-to-word
{20 "Twenty" 30 "Thirty" 40 "Fourty"
50 "Fifty" 60 "Sixty" 70 "Seventy"
80 "Eighty" 90 "Ninety"})
(def teen-to-word
{10 "Ten" 11 "Eleven" 12 "Twelve"
13 "Thirteen" 14 "Fourteen" 15 "Fifteen"
16 "Sixteen" 17 "Seventeen" 18 "Eighteen"
19 "Nineteen"})
(defn divmod [n d] [(quot n d) (mod n d)])
(defn tens [n & {:keys [zero] :or {zero ""}}]
(let [[tens ones] (divmod n 10)
digit (digit-to-word ones)]
(condp = tens
0 (or digit zero)
1 (teen-to-word n)
(str (tens-to-word (* 10 tens)) (when digit (str "-" digit))))))
(defn order [d name f n]
(let [[hs ts] (divmod n d)]
(str ""
(when (> hs 0) (str (f hs) " " name))
(when (> ts 0) (str (when (> hs 0) " ") (f ts))))))
(def hundreds (partial order 100 "Hundred" tens))
(def orders ["" " Thousand" " Million" " Billion" " Trillion"])
(defn addp [lst n o]
(if (> n 0) (conj lst (str (hundreds n) (orders o))) lst))
(defn convert- [x]
(clojure.string/join ", "
(loop [i 0, n x, out '()]
(let [[a b] (divmod n 1000)]
(cond
(>= a 1000) (recur (inc i) a (addp out b i))
(> a 0) (-> out (addp b i) (addp a (inc i)))
:else (addp out b i)
)))))
(defn convert [x]
(case (compare x 0)
-1 (str "Negative " (convert- (- (int x))))
0 "Zero"
1 (convert- (int x))
nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment