How many move i need to make two list same in python

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)

How do you manually figure out the solution? What is the logic you use to know that the example is 2? If you can figure out the steps you mentally take to figure out the solution, then you have an algorithm for which you can write code.

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.

I understand there are a minimum of two moves. You need to figure out the generic steps for any two lists. For example, what other numbers of swaps could you come up with in this example. How do you know when to stop making swaps?

  1. You do not need to take into account “a == b” because there is no “move or count or swap” necessary in that case!
  2. 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])”.
  3. swap(b(i), b(j))
  4. Increment move (move += 1).
  5. return move (I prefer count_swaps).

I hope it helps!

swap of 2 values m, n in Python => m, n = n, m

1 Like