-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path!9. Nearly sorted - v1.py
More file actions
59 lines (50 loc) · 1.58 KB
/
!9. Nearly sorted - v1.py
File metadata and controls
59 lines (50 loc) · 1.58 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
#User function Template for python3
class Solution:
#Function to return the sorted array.
def nearlySorted(self,a,n,k):
# code here
sorted_list = []
for elm in a:
if len(sorted_list) == 0:
sorted_list.append(elm)
else:
print(f'sorted list : {sorted_list}')
index = 0
for idx, sorted_elm in enumerate(sorted_list):
index = idx
print(f'len liste: {len(sorted_list)}')
if sorted_elm > elm:
print(index)
#sorted_list[:] = list(elm)
sorted_list.insert(index, elm)
break
elif len(sorted_list) == index+1:
print(index + 1)
sorted_list.insert(index + 1, elm)
break
print(f'end sorted : {sorted_list}')
#sorted_list[index:index] = list(elm)
return sorted_list
#{
# Driver Code Starts
#Initial Template for Python 3
import atexit
import io
import sys
import heapq
from collections import defaultdict
# Contributed by : Nagendra Jha
if __name__ == '__main__':
test_cases = int(input())
for cases in range(test_cases) :
n,k = map(int,input().strip().split())
a = list(map(int,input().strip().split()))
ob=Solution()
print(*ob.nearlySorted(a,n,k))
# } Driver Code Ends
# Tested inputs :
'''
1
6 4
1 3 5 6 4 2
'''