Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. """This is an attempt to solve the 28th Project Euler problem using object oriented python. This seems like a bit of an overkill, since I think the answer to this could be computed prodecurally in a relatively straightforward manner, but it is a nice exercise."""
  2.  
  3. #First I create a function oddGrid(m). This gives me a list of coordinates of an mxm grid for odd m, centred on (0,0).
  4.  
  5. #As an intermediate step I create a function tuples(n)
  6.  
  7. def tuples(n):
  8. ans=[]
  9. for i in range(-n,n+1):
  10. for j in range(-n,n+1):
  11. ans.append((i,j))
  12. return ans
  13. def oddGrid(m):
  14. if m%2==0:
  15. return "That is not an odd integer"
  16. else: return tuples(m//2)
  17.  
  18. #Now I want to define a generator that will move through coordinates in the desired 'spiral' pattern
  19.  
  20. def position():
  21. xCoord=0
  22. yCoord=0
  23. yield (xCoord,yCoord)
  24. n=1
  25. while True:
  26. for i in range(0,n):
  27. xCoord+=1
  28. yield (xCoord,yCoord)
  29. for i in range(0,n):
  30. yCoord-=1
  31. yield (xCoord,yCoord)
  32. n+=1
  33. for i in range(0,n):
  34. xCoord-=1
  35. yield (xCoord,yCoord)
  36. for i in range(0,n):
  37. yCoord+=1
  38. yield (xCoord,yCoord)
  39. n+=1
  40.  
  41. x=position()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement