- Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathswapthemall.py
executable file
·30 lines (22 loc) · 841 Bytes
/
swapthemall.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
#!/usr/bin/env python
# Given an array of all letters a-z in order. what would be the final
# state of the array after running the following algorithm.
# You need to swap (x,x+i) for all values of x and i such that x+i<27,
# So
# i = 1: You would swap first element with second element, etc.
# i.e swap(1,2), swap (2,3), swap (3,4) etc.
# i=2: you would swap first element with third element, etc.
# i.e swap(1,3),swap(2,4), swap(3,5) etc.
# continue until
# i=25 : you'd swap just the first and 26th elements.
# Starting state = abcdefghijklmnopqrstuvwxyz
# Flag = The final state of the array
s=list("abcdefghijklmnopqrstuvwxyz")
defswap(x):
foriinrange(0, len(s) -x):
tmp=s[i]
s[i] =s[i+x]
s[i+x] =tmp
foriinrange(0,26):
swap(i)
print''.join(s) # zldmpnqeaiojkbgvrwshtcxfyu