Friday, May 6, 2011

Open Computer Science Problems

I found this new problems site. The first problem is simple:

(apply + 
    (fn [i]
      (Math/pow 3 (dec i)))
    (range 1 16)))

The second problem isn't so simple. I implemented the procedure the site described, however, it doesn't work because the range is way too large:

(let [n (fn [i]
          (count (filter #(= \1 %) (Integer/toString i 2))))]
  (map n (range 1 (inc (Math/pow 2 50)))))

Instead I solved this problem using mathematical simplification which lead me to:

(inc (* 50 (Math/pow 2 49)))

Then I got to the third problem. Apparently they want me parse some expression! I am a Lisp programmer for a reason: I don't like to parse. So instead I made a macro based upon this problem:

(defmacro ternary-expression 
  [if-true expr if-false]
  `(if ~expr

No comments:

Post a Comment