July 23, 2016

Quora homework question, Smile and Clojure

Looking at my twitter feed I saw the following Quora question: How do I write a program that produces the following output?.

People were all trying to create some cyphered program that would produce the desired output, the perl one was funny.

At the time I didn’t found a Clojure take on this hard problem.

Code!

So, here’s a Clojure one with magic randomness immutability.

(defn foo [a b]
  "Do some char foo."
  (char (+ a b)))

(defn bar [a]
  "Keep only some foo from foos."
  (take-while #(not= % 0) a))

(defn bazz [a b]
  "Transform some foo."
  (map #(foo (second a) %) b))

(defn bar-bazz [a]
  "Iterate on some random foo."
  (let [r (new Random (first a))]
    (apply str (bazz a (bar (repeatedly #(.nextInt r 27)))))))

(defn foo-bar [a z]
  "Construct some magic foo from a lot of foo."
  (let [r (range z 0 -1)]
    (map #(conj ["\n"] (take % (repeatedly (fn [] (apply str (map bar-bazz a)))))) r)))

(defn please-do-my-homework [number-of-times]
  "This is your homework :)"
  (let [magic [[-9223372036854775771 66] [-9223372036854209285 95] [-9223372036854775771 16]]]
    (print (apply str (flatten (foo-bar magic number-of-times))))))

Execution

(please-do-my-homework 5)
; Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!
; Smile!Smile!
; Smile!

(please-do-my-homework 10)
; Smile!Smile!Smile!Smile!Smile!Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!Smile!
; Smile!Smile!Smile!
; Smile!Smile!
; Smile!

Until next time!

Alexandre Grison - //grison.me - @algrison