Skip to content

Commit bc0e1d9

Browse files
authored
Create Almost_Sorted.py
1 parent 2867645 commit bc0e1d9

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Almost_Sorted.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
raw_input()
3+
l = map(int,raw_input().split())
4+
sl = sorted(l)
5+
6+
diffcount = 0
7+
diff1 = -1
8+
diff2 = -1
9+
10+
for i in range(len(l)):
11+
if sl[i] != l[i]:
12+
diffcount += 1
13+
if diff1 == -1:
14+
diff1 = i
15+
elif diffcount > 1:
16+
diff2 = i
17+
lastdiff = i
18+
19+
if diffcount == 2:
20+
l[diff1], l[diff2] = l[diff2], l[diff1]
21+
if l == sl:
22+
print "yes"
23+
print "swap {} {}".format(diff1+1, diff2+1)
24+
else:
25+
print "no"
26+
elif diffcount > 2:
27+
l = l[:diff1] + l[diff1:diff2+1][::-1] + l[diff2+1:]
28+
if l == sl:
29+
print "yes"
30+
print "reverse {} {}".format(diff1+1, diff2+1)
31+
else:
32+
print "no"
33+
elif l == sl:
34+
print "yes"

0 commit comments

Comments
 (0)