-
Notifications
You must be signed in to change notification settings - Fork 0
/
q9_1.py
50 lines (41 loc) · 859 Bytes
/
q9_1.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
def sortInto(A,B) :
"""Requires: A, B aleady sorted arrays and A has large end buffer to hold B
also A, B need at least one element
Modifies: A by sorting B into it
"""
a = -1
while(A[a+1] != None) : #find last element of A
a += 1
b = len(B) - 1
f = a + len(B)
while(a != -1) :
if b == -1 : #B is completely sorted into A, nothing else needed
return
if A[a] >= B[b] :
A[f] = A[a]
a -= 1
else :
A[f] = B[b]
b -= 1
f -= 1
while(b != -1) :
A[f] = B[b]
b -= 1
f -= 1
def test_sortInto() :
A = [4, 5, 6, None, None, None]
B = [1,2,3]
sortInto(A,B)
print A
assert A == range(1,7)
A = [1,None,None ]
B = [2,3]
sortInto(A,B)
assert A == range(1,4)
print "passes tests"
if __name__ == '__main__' :
test_sortInto()
#incorporate
#############################
#Divide
#############################