-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
60 lines (53 loc) · 1.93 KB
/
tests.py
File metadata and controls
60 lines (53 loc) · 1.93 KB
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
# 1d
def reverse_sublist(lst,start,end):
if (len(lst) != 0): #empty check
for index in range(0,(end - start) // 2): #for half the sublist, reverse using a temp variable
temp = lst[start + index]
lst[start + index] = lst[end - 1]
lst[end - 1] = temp
index = index + 1
end = end - 1
def rotate1(lst):
if (len(lst) != 0): #if the list isn't empty, move each item to the next index, and put the last one in lst[0]
last = lst[len(lst) - 1]
for i in range(1,len(lst)):
lst[len(lst) - i] = lst[len(lst) - i - 1]
lst[0] = last
def rotatek_v1(lst,k):
if (len(lst) != 0):
k = k % len(lst) #in case that k>len(lst), minimize it
if k != len(lst): #no need to rotate
if(k < 0):
k = len(lst) + k
if ((len(lst) - k) >= (len(lst) / 2)):
while k > 0:
rotate1(lst)
k = k - 1
else:
lst.reverse()
k = len(lst)-k
while k!=0:
rotate1(lst)
k=k-1
lst.reverse()
def rotatek_v2(lst,k):
if (len(lst)!=0): #empty check
k = k % len(lst) #in case that k>len(lst), minimize it
if k != len(lst): #need to rotate
if(k < 0): #same as the method above
k = len(lst) + k
if ((len(lst) - k) >= (len(lst) / 2)):
while k > 0:
rotate1(lst)
k = k - 1
else:
reverse_sublist(lst,0,len(lst))
k = len(lst)-k
while k!=0:
rotate1(lst)
k = k-1
reverse_sublist(lst,0,len(lst))
lst1=[2,6,7,8]
print(id(lst1))
rotatek_v1(lst1,-1)
print(lst1,id(lst1))