-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday72.py
52 lines (41 loc) · 1.36 KB
/
day72.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
#!/bin/python3
import sys
def is_sorted(arr):
return all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
def almostSorted(arr):
swap_l = -1
swap_r = -1
for ind in range(1, len(arr)):
if arr[ind - 1] > arr[ind]:
swap_l = ind - 1
break
for ind in range(swap_l + 1, len(arr)):
if ind == len(arr) - 1 or arr[ind + 1] > arr[swap_l]:
swap_r = ind
arr[swap_l], arr[swap_r] = arr[swap_r], arr[swap_l]
break
if is_sorted(arr):
print("yes")
print("swap {} {}".format(swap_l + 1, swap_r + 1))
return True
arr[swap_l], arr[swap_r] = arr[swap_r], arr[swap_l]
rev_l = -1
rev_r = -1
for ind in range(len(arr) - 1):
if rev_l == -1 and arr[ind] > arr[ind + 1]:
rev_l = ind
elif rev_l != -1 and arr[ind] < arr[ind + 1]:
rev_r = ind
break
to_rev = arr[rev_l:rev_r+1]
arr = arr[:rev_l] + to_rev[::-1] + arr[rev_r+1:]
if is_sorted(arr):
print("yes")
print("reverse {} {}".format(rev_l + 1, rev_r + 1))
return True
print("no")
return False
if __name__ == "__main__":
n = int(input().strip())
arr = list(map(int, input().strip().split(' ')))
almostSorted(arr)