-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayHopper.py
63 lines (44 loc) · 1.31 KB
/
ArrayHopper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
__author__ = 'spencertank'
#Function that finds the minimum number of hops to traverse the given array
def findMinHops(input_file):
try:
f = open(input_file, 'r')
except IOError:
return "The input file specified could not be found!", -2
s = f.read()
input = s.split('\n')
try:
array = map(int, input)
except ValueError:
return "The specified input is not formatted correctly!", -3
print "Input Array = " + str(array)
indexes = []
next_hop = 0
next_stop = array[0]
length = len(array)
current_index = 0
landing = 0
indexes.append(0)
while True:
for i in range(current_index, next_stop + 1):
landing = array[i] + i
if landing > next_hop:
next_hop = landing
next_index = i
if next_index == current_index:
return "failure", -4
indexes.append(next_index)
if next_hop >= length:
break
next_stop = next_index + array[next_index]
current_index = next_index
return indexes, 0
if __name__ == "__main__":
try:
input_file = str(sys.argv[1])
except IndexError:
print "Please specify an input file!"
quit()
output, value = findMinHops(input_file)
print output