Created
August 16, 2012 23:11
-
-
Save anonymous/3374386 to your computer and use it in GitHub Desktop.
Solutions for 4Clojure problem 106 - http://www.4clojure.com/problem/106
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
;;;;;;;;;;;;;;;;;;; | |
;; CLOJURE CODE: ;; | |
;;;;;;;;;;;;;;;;;;; | |
(defn transform [x] | |
(if (even? x) [(+ x 2) (* x 2) (/ x 2)] | |
[(+ x 2) (* x 2)])) | |
(defn find-path | |
([number goal] | |
(find-path 1 [number] goal)) | |
([step branches goal] | |
(if (not-any? #(= % goal) branches) | |
(find-path (inc step) (flatten (map transform branches)) goal) | |
step))) | |
(println (find-path 3 347)) |
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
################ | |
# PYTHON CODE: # | |
################ | |
goal = 347 | |
start = 3 | |
steps = 1 | |
branches = [start] | |
while not goal in branches: | |
steps += 1 | |
newBranches = [] | |
for branch in branches: | |
newBranches.append(branch+2) | |
newBranches.append(branch*2) | |
if branch % 2 == 0: | |
newBranches.append(branch/2) | |
branches = newBranches | |
print steps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment