Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Solutions for 4Clojure problem 106
- http://www.4clojure.com/problem/106
- ;;;;;;;;;;;;;;;;;;;
- ;; 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 numbers goal]
- (if (not-any? #(= % goal) numbers)
- (find-path (inc step) (flatten (map transform numbers)) goal)
- step)))
- (println (find-path 3 347))
- ______________________________________________________________________
- ################
- # 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
Advertisement
Add Comment
Please, Sign In to add comment