Guest User

Untitled

a guest
Oct 17th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. # Classic functional approach to iteration:
  2. #
  3. # "for" and "while" keywords are superfluous when
  4. # recursion is supported. With the proviso that the
  5. # interpreter/compiler must be able to internally
  6. # convert tail-recursive calls to iteration to
  7. # prevent stack overflow.
  8. #
  9. # Python interpretor does not, so this is mostly
  10. # a syntactic exploration rather than a practical one.
  11. #
  12. # It does have one other benefit in that thinking about
  13. # writing functions without the use of iterator keywords
  14. # provides good practice for thinking "recursively."
  15. #
  16. # Use Case: The Robot Simulation
  17. #
  18. # The robot is trying to get home - OK, I should have
  19. # maybe called this "E.T. Simulation" but I'm avoiding
  20. # the potential copyright infringment...
  21. #
  22. # The robot starts at the upper left of an n*n grid
  23. # containing obstacles. It's objective is to get
  24. # home by a combination of walk right/down moves.
  25. #
  26. def iterate(a):
  27. i = 0
  28. n = len(a)
  29. def iterator():
  30. nonlocal i
  31. if i == n:
  32. return False
  33. v = a[i]
  34. i += 1
  35. return v
  36. return iterator
  37.  
  38. def dowhile(proc,iter):
  39. v = iter()
  40. if v:
  41. proc(v)
  42. dowhile(proc,iter)
  43.  
  44. def apply(f,iter):
  45. r = []
  46. dowhile(lambda v: r.append(f(v)),iter)
  47. return r
  48.  
  49. a = [1,2,3,4,5,6,7,8,9]
  50. squarev = lambda v: v*v
  51. squares = apply(squarev,iterate(a))
  52. print(squares)
  53.  
  54.  
  55. # Robot simulation:
  56. # Robot finds path through obstacles to home
  57. # walking only right or down on the grid.
  58. #
  59. # For fun: extend this to include moves in
  60. # any direction.
  61. #
  62. # 0 1 2 3 4
  63. # 0 R _ X _ _
  64. # 1 _ X _ _ _
  65. # 2 _ _ _ _ _
  66. # 3 _ _ _ X H
  67. # 4 _ X _ _ _
  68. #
  69. def robot(gridsize,obstacles,homespot):
  70. path = []
  71. def outofbounds(coord):
  72. return coord > gridsize - 1
  73.  
  74. def ishome(pos):
  75. return pos == homespot
  76.  
  77. def isblocked(pos):
  78. return True in apply(lambda x: x == pos,iterate(obstacles))
  79.  
  80. def walk(pos,athome,lvl):
  81. print(f"{' '*lvl}{pos} {home}")
  82. path.append(pos) # add possible pos to path
  83.  
  84. if ishome(pos):
  85. return True
  86.  
  87. r,c = pos[0],pos[1]
  88.  
  89. if not athome and not outofbounds(r+1) and not isblocked((r+1,c)):
  90. athome = walk((r+1,c),athome,lvl+1)
  91.  
  92. if not athome and not outofbounds(c+1) and not isblocked((r,c+1)):
  93. athome = walk((r, c+1),athome,lvl+1)
  94.  
  95. if not athome:
  96. path.pop() # remove attempted pos
  97.  
  98. return athome
  99.  
  100. pos = (0,0)
  101. if not isblocked(pos):
  102. walk(pos,False,0)
  103. return path
  104.  
  105. print("Path Home:")
  106. obstacles = [(0, 2),(1, 1), (3, 3), (4, 1)]
  107. gridsize = 5
  108. home = (3,4)
  109. path = robot(gridsize=gridsize,obstacles=obstacles,homespot=home)
  110. print(path)
Add Comment
Please, Sign In to add comment