-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnew_year_chaos.py
More file actions
46 lines (36 loc) · 985 Bytes
/
Copy pathnew_year_chaos.py
File metadata and controls
46 lines (36 loc) · 985 Bytes
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
#!/bin/python
"""
https://www.hackerrank.com/challenges/new-year-chaos
"""
T = int(raw_input().strip())
for a0 in xrange(T):
n = int(raw_input().strip())
q = map(int,raw_input().strip().split(' '))
# print 'xxx xxx xxx xxx xxx'
# your code goes here
i = 0
ttl_transitions = 0
while i < n:
# print i, ':', q
transitions = 0
save_i = i
while (save_i + 1 < n and q[save_i] > q[save_i+1]):
tmp = q[save_i]
q[save_i] = q[save_i+1]
q[save_i+1] = tmp
transitions += 1
save_i += 1
if transitions > 2:
break
if transitions == 0:
i += 1
elif transitions > 2:
print 'Too chaotic'
ttl_transitions = -1
break
else:
if i - 1 >= 0:
i -= 1
ttl_transitions += transitions
if ttl_transitions != -1:
print ttl_transitions