-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_arr_kgroup.py
53 lines (39 loc) · 973 Bytes
/
reverse_arr_kgroup.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
#!/usr/bin/env python3
def inser_value(len_l):
while True:
k = input("Insert an integer smaller than " + str(len_l) + "\n")
k = int(k)
if (k <= len_l):
break
else:
print("Invalid value, insert a new integer\n")
return k
def arr_sub_rev(l,k):
sub_l = []
new_l = []
indx = len(l)
d = len(l)/k
d = int(d)
if (k == 1) :
new_l = sorted(l, reverse = True)
else:
for j in range(0,d):
sub_l = [l[i] for i in range((j*k),((j+1))*k)]
#print(sub_l)
sub_l = sorted(sub_l, reverse = True)
for i in sub_l:
new_l.append(i)
sub_l = []
indx = (((j+1))*k)
#fill the rest of the array, if necessary
if indx != len(l):
for i in range(indx,len(l)):
new_l.append(l[i])
return new_l
l = [1,2,3,4,5,6,7,8,9,10,11]
l1 = [4]
l2 = [1,2,3,4,5,6,7,8,9,10,11,12]
k = inser_value(len(l))
nrl = arr_sub_rev(l,k) # l list, k group
print(l)
print(nrl)