Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """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."""
- #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).
- #As an intermediate step I create a function tuples(n)
- def tuples(n):
- ans=[]
- for i in range(-n,n+1):
- for j in range(-n,n+1):
- ans.append((i,j))
- return ans
- def oddGrid(m):
- if m%2==0:
- return "That is not an odd integer"
- else: return tuples(m//2)
- #Now I want to define a generator that will move through coordinates in the desired 'spiral' pattern
- def position():
- xCoord=0
- yCoord=0
- yield (xCoord,yCoord)
- n=1
- while True:
- for i in range(0,n):
- xCoord+=1
- yield (xCoord,yCoord)
- for i in range(0,n):
- yCoord-=1
- yield (xCoord,yCoord)
- n+=1
- for i in range(0,n):
- xCoord-=1
- yield (xCoord,yCoord)
- for i in range(0,n):
- yCoord+=1
- yield (xCoord,yCoord)
- n+=1
- x=position()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement