Skip to content

Instantly share code, notes, and snippets.

@deluxe
Created March 10, 2013 22:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deluxe/5130709 to your computer and use it in GitHub Desktop.
Save deluxe/5130709 to your computer and use it in GitHub Desktop.
; --- http://thecodersbreakfast.net/index.php?post/2013/02/18/Coding-challenge-maman-les-petits-avions ---
; --- Function returning the sum of the characters in the string s ---
(defn chars-to-int [s]
"Returns the sum of the characters in the string s."
(apply + (map #(int %) s)))
; --- Function returning a syracuse suite:
; if n1 even, n2 = (n1 / 2),
; if n1 odd, n2 = (n1 * 3 + 1).
; ---
(defn syracuse-suite [n]
"Return the syracuse suite starting with n."
(cons n
(if (= n 1)
[]
(syracuse-suite
(if (even? n)
(/ n 2)
(inc (* n 3)))))))
; --- Function printing the fly path of a prototype from his name ---
(defn fly [name]
"Print the fly path of a prototype from his name."
(let [fly-seq (syracuse-suite (chars-to-int name))]
(println "Sequence:" fly-seq)
(println "Total fly:" (count fly-seq))
(println "Altitude fly:" (count (take-while #(>= % (first fly-seq)) fly-seq)))
(println "Max altitude:" (apply max fly-seq))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment