Skip to content

Instantly share code, notes, and snippets.

Forked from alandipert/gist:585336
Created September 19, 2010 22:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save artlung/587176 to your computer and use it in GitHub Desktop.
challenge in clojure
(defn challenge1
"Given file name f reads it and prints statistics."
(let [{numbers false letters true} (group-by #(nil? (re-matches #"\d*" %))
( f))
letter-freqs (frequencies letters)
sum (reduce + (map #(Integer/parseInt %)
(println sum)
(doseq [k (keys letter-freqs)]
(println k " = " (get letter-freqs k)))))
;;; Ruby:
;;; numbers, characters ="test.txt"){|c|c=~/\d/}
;;; puts
;;; freqs = lambda{|arr| arr.reduce({}.tap{|h| h.default=0}){|xs,y| xs.tap{|h| h[y] += 1}}}
;;;{|k,v| puts "#{k} = #{v}"}
(defn str-reverse
"Reverse a string."
(apply str (reverse (str s))))
(defn reverse-sums
"Lazy sequence of n added to its reverse.
ie. (take 5 (reverse-sums 56)) ;=> (56 121 242 484 968)"
(letfn [(rev-add [a] (+ a (BigInteger. (str-reverse a))))]
(iterate rev-add n)))
(defn palindrome?
"True if n is a palindrome."
(= (str n)
(str-reverse n)))
(defn candidate?
"True if none the first lim reverse-sums of n are palindromes."
[lim n]
(every? #(not (palindrome? %))
(take lim (reverse-sums n))))
(defn challenge2
"Prints the Lychrel candidates for a given lim, below n."
[lim n]
(println "Candidates: "
(filter (partial candidate? lim)
(range 1 (inc n)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment