Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write a program that swaps the values of two variables a and b in one line, without a third variable. Then write a function order(x, y) that gives back the two numbers as a tuple, the smaller first.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The right side of an assignment makes a tuple, and the left side unpacks it.
Check. After the swap, a is 9 and b is 5. The call order(8, 3) returns (3, 8), and the unpacking gives small = 3 and big = 8.
Answer: a, b = b, a, and a function with return x, y or return y, x.
Log in to join the discussion.
Loading…