Created
March 10, 2013 22:05
-
-
Save deluxe/5130709 to your computer and use it in GitHub Desktop.
Clojure code for http://thecodersbreakfast.net/index.php?post/2013/02/18/Coding-challenge-maman-les-petits-avions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; --- 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