Even though I have completed this assignment, the direction()
function is just irking me. I feel like there is a better way to do it, ideally gaining on execution time or at least aesthetically (cleaner way).
(The stub for my assignment contained code from my professor, but it's generic display_grid
and some part of temp()
.)
Challenge:
Traverse a 10 X 10 grid, to accumulate a cost starting from a location on the grid. The cost and location will be user input. The grid can be traversed in 4 directions(Up, Down, Left, Right) and diagonal movement is not allowed. No location should be added to cost more than once. The direction of traversal should be maintained till the end of the grid or a loop is encountered. In case the direction needs to be changed it must be done clockwise (UP>RIGHT>DOWN>LEFT). If none of the directions are possible from that point, you back track to the previous point and try another direction. For example you are heading RIGHT and at a point it is no longer possible to go right then you try to go DOWN, if that is not possible you try to go LEFT and so on and so forth. If none of these are possible you backtrack you the previous location and try heading DOWN because previously you went RIGHT from this point and the next clockwise option is DOWN. You continue doing this till you accumulate the cost provided by user. Additional Information: Use stack for backtracking.
direction(): Given the list of nodes already seen and the current position, it returns the children of the current position in all direction and the directions they are found ensuring none of them have been previously traversed.
I want to know if I can improve this implementation of direction()
which gives me the children of (x,y) and the direction in which they are found.
import sys from random import seed, randrange def display_grid(grid): for i in range(len(grid)): print(' ', ' '.join(str(grid[i][j]) for j in range(len(grid[0])))) def temp(): chain=[] value=0 direction_flag=0 #professor's code starts try: for_seed, bound, x, y, target = [int(x) for x in input('Enter five integers: ').split()] if bound < 1 or x not in range(10) or y not in range(10) or target < 0: raise ValueError except ValueError: print('Incorrect input, giving up.') sys.exit() seed(for_seed) #Creates a grid grid = [[randrange(bound) for _ in range(10)] for _ in range(10)] print('Here is the grid that has been generated:') display_grid(grid) #professor's code ends children = direction((x, y),grid,set(chain),(value,direction_flag)) print(f'Children of ({x},{y}): {children}') def direction(element, grid, chain, support_element): ''' This is a code skeleton, I have removed parts of the code, I thought were not necessary to this code Review In case you feel the need to see the whole thing, please let me know. ''' ''' 1=North 2=South 3=East 4=West ''' #{element} is a location on the grid #{chain} is all the locations previously seen [removed the iteration] #{value} gives the sum of the grid locations seen before arriving at #that point #{direction_flag} gives the direction in which the node was discovered. children = [] direction_flag=support_element[1] if direction_flag == 1: val=element[0] - 1 if element[0] > 0 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 1)] val=element[1] + 1 if element[1] < 9 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 3)] val=element[1] - 1 if element[1] > 0 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 4)] return children elif direction_flag == 2: val=element[0] + 1 if element[0] < 9 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 2)] val=element[1] - 1 if element[1] > 0 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 4)] val= element[1] + 1 if element[1] < 9 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 3)] return children elif direction_flag == 3: val=element[1] + 1 if element[1] < 9 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 3)] val=element[0] + 1 if element[0] < 9 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 2)] val=element[0] - 1 if element[0] > 0 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 1)] return children elif direction_flag == 4: val=element[1] - 1 if element[1] > 0 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 4)] val=element[0] - 1 if element[0] > 0 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 1)] val=element[0] + 1 if element[0] < 9 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 2)] return children else: val = element[0] - 1 if element[0] > 0 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 1)] val = element[1] + 1 if element[1] < 9 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 3)] val = element[1] - 1 if element[1] > 0 and (element[0], val) not in chain: children += [(element[0], val, support_element[0], 4)] val = element[0] + 1 if element[0] < 9 and (val, element[1]) not in chain: children += [(val, element[1], support_element[0], 2)] return children temp()