kakel

Untitled

Aug 16th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Solutions for 4Clojure problem 106
  2. http://www.4clojure.com/problem/106
  3.  
  4. ;;;;;;;;;;;;;;;;;;;
  5. ;; CLOJURE CODE: ;;
  6. ;;;;;;;;;;;;;;;;;;;
  7.  
  8. (defn transform [x]
  9.   (if (even? x) [(+ x 2) (* x 2) (/ x 2)]
  10.                 [(+ x 2) (* x 2)]))
  11.  
  12. (defn find-path
  13.   ([number goal]
  14.     (find-path 1 [number] goal))
  15.   ([step numbers goal]
  16.     (if (not-any? #(= % goal) numbers)
  17.       (find-path (inc step) (flatten (map transform numbers)) goal)
  18.       step)))
  19.  
  20. (println (find-path 3 347))
  21.  
  22. ______________________________________________________________________
  23.  
  24. ################
  25. # PYTHON CODE: #
  26. ################
  27.  
  28. goal = 347
  29. start = 3
  30. steps = 1
  31. branches = [start]
  32.  
  33. while not goal in branches:
  34.     steps += 1
  35.     newBranches = []
  36.     for branch in branches:
  37.         newBranches.append(branch+2)
  38.         newBranches.append(branch*2)
  39.         if branch % 2 == 0:
  40.             newBranches.append(branch/2)
  41.     branches = newBranches
  42.  
  43. print steps
Advertisement
Add Comment
Please, Sign In to add comment