Skip to content

Instantly share code, notes, and snippets.

@eternalruler
Created April 29, 2015 18:47
Show Gist options
  • Save eternalruler/ed803a9366ee39f3eb12 to your computer and use it in GitHub Desktop.
Save eternalruler/ed803a9366ee39f3eb12 to your computer and use it in GitHub Desktop.
arr = [1,2,3,1,1,2,1,1]
class jump_path():
def __init__(self, target_array = None, spots = []):
#print('new path!')
self.target = target_array
self.spots = spots
def hops(self):
return len(self.spots)-1
def next_hops(self):
paths = []
#print('next_hops')
j = self.spots[-1]
e = self.target[j]
#print('e : {}'.format(e))
for x in range(e+1):
#print(' x : {}'.format(x))
path = self.copy()
path.spots.append(j+x)
paths.append(path)
return paths
def copy(self):
clone = jump_path()
clone.target = self.target
clone.spots = self.spots[:]
return clone
def __str__(self):
return 'Path: {}, Distance: {}'.format(self.spots, self.hops())
def get_shortest_path_through_arr(arr):
path = jump_path(arr, [0])
paths = [path]
if len(paths) == 0:
return None
def loop(paths):
next_paths = []
for path in paths:
#print path
#print path.spots
#print (len(path.target)-1)
if path.spots[-1] == (len(path.target)-1):
#print 'FOUND'
return path
next_paths = next_paths + path.next_hops()
if len(next_paths) > 0:
return loop(next_paths)
return loop(paths)
shortest_path = get_shortest_path_through_arr(arr)
print 'Shortest Path: {}'.format(shortest_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment