-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path!9. Nearly sorted - v3.py
More file actions
61 lines (51 loc) · 1.48 KB
/
!9. Nearly sorted - v3.py
File metadata and controls
61 lines (51 loc) · 1.48 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
60
61
#User function Template for python3
class Solution:
#Function to return the sorted array.
def nearlySorted(self,a,n,k):
# code here
for idx1, elm in enumerate(a):
#print(idx1)
curent_nbr = 0
try:
if elm > a[idx1+1]:
curent_nbr = a[idx1+1]
del a[idx1+1]
print(f'curent_nbr: {curent_nbr}')
for idx2, sorted_elm in enumerate(a[:idx1]):
if sorted_elm > curent_nbr:
a[idx2:idx2] = [curent_nbr]
break
print(f'step: {a}')
except:
break
print(a)
for index in range(len(a)):
try:
if a[index] > a[index+1]:
self.nearlySorted(a,n,k)
except:
break
return a
#{
# 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
'''