We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2867645 commit bc0e1d9Copy full SHA for bc0e1d9
Almost_Sorted.py
@@ -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
29
30
+ print "reverse {} {}".format(diff1+1, diff2+1)
31
32
33
+elif l == sl:
34
0 commit comments