I have two list,
a = [0,1,0,1]
b = [1,0,1,0]
Note: The the elements of both list will just 0 and 1.
if i want to make a == b then, i need 2 move.
1st move will be, a[0] swap to a[1]
2nd move will be,a[2] swap to a[3]
Then a look like same as b .
So, My input is,
a = [0,1,0,1]
b =[1,0,1,0]
output will,
2
How can i fix this in python?
What have you tried so far? Do you have any code or notes?
1 Like
def xx(a,b):
move = 0
if a == b:
print(move)
else:
# i don't' know what should i do here and
# when i will add 1 to move
a = list(map(int,input().split()))
b = list(map(int,input().split()))
xx(a,b)
Sorry,i don’t get it.But the logic is,
a = [0,1,0,1]
b =[1,0,1,0]
if i swap between first and second element of list a then it will be ,
a = [1,0,0,1]
Now if i swap between third and fourth element of list a then it will be,
a = [1,0,1,0]
So, New list a and list b is equal now.I needed two minimum move to make them same.
- You do not need to take into account “a == b” because there is no “move or count or swap” necessary in that case!
- Try a “for-Loop” in “range(len(a) - 1)” and if a[i] != b[i] set a new index as “j = b.index(a[i])”.
- swap(b(i), b(j))
- Increment move (move += 1).
- return move (I prefer count_swaps).
I hope it helps!
swap of 2 values m, n in Python => m, n = n, m
1 Like