You may come across this swapping of 2 numbers frequently during interview rooms.
The interviewer always try to dig you to know how many possible ways it can be solved.
Depending upon your move he/she may decide what is level of knowledge you have
and how much logically you are able to think.
Always have an knowledge on multiple ways of solving the same question.
Below are the 3 ways it can be solve
Method 1
a=10
b=20
b,a=a,b
print('A : ',a)
print('B : ',b)
a=10
b=20
tmp=a
a=b
b=tmp
print('A : ',a)
print('B : ',b)
a=10
b=20
a = a + b # (30)
b = a - b # (30 - 20)
a = a - b # (30 - 10)
print('A : ',a)
print('B : ',b)
0 Comments