-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.py
75 lines (61 loc) · 2 KB
/
q1.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# MAIN SOLUTION -----------------------------------------------------------------
canjump=0
def solution(A):
# write your code in Python 3.6
# ways you can reach the end of the array
# can either do even or odd jumps
jumpcount=1
for i in range(len(A)):
jump(findSmallestOdd(i, A), A, jumpcount)
return canjump
# RECURSIVE FUNCTION ------------------------------------------------------------------
def jump(pos, A, jumpcount):
# base case
if pos == len(A)-1: # at end! hooray, increment canjump
canjump+=1
# keep jumping
if jumpcount%2==1:
# odd jump, odd rules
for i in range(pos, len(A)):
if validOdd(pos, i, A):
jump(i, A, jumpcount+1) # break into smaller problems
else:
break
else:
# even jump, even rules
for i in range(pos, len(A)):
if validEven(pos, i, A):
jump(i, A, jumpcount+1)
else:
break
# HELPER FUNCTIONS ------------------------------------------------------------------------------
def validOdd(pos, i, A):
# is jumping from pos to i valid in A, given odd jump?
if i==findSmallestOdd(pos, A):
return True
else:
return False
def validEven(pos, i, A):
# is jumping from pos to i valid in A, given even jump?
if i==findSmallestEven(pos, A):
return True
else:
return False
def findSmallestOdd(pos, A):
smallest=101
smallpos=0
for i in range(pos, len(A)):
if A[i] > A[pos] and A[i]-A[pos] < smallest:
smallest=A[i]
smallpos=i
return smallpos
def findSmallestEven(pos, A):
smallestDiff=101
smallpos=0
for i in range(pos, len(A)):
if A[i] < A[pos] and A[pos]-A[i] < smallestDiff:
smallestDiff=A[i]
smallpos=i
return smallpos