As we’ve already seen, tuples are very useful when unpacking them into several variables:
This is very useful when swapping the values of variables:
Given a list of
nnumbers, you are asked to perform
ooperations on those. Each operation represents a swapping of two numbers. Given 2 indices, the values situated at those locations need to be replaced by one another. You are asked to print the resulting list at the end.
The first line of the input contains two integers
o- the number of elements in the list and the number of operations you need to perform. The next line contains
nintegers separated by a space. The next
olines contain two integers on each line separated by a space - the indices which need to be swapped.
The program should print the resulting list of numbers separated by a space.
5 2 5 6 7 8 9 0 2 1 3
7 8 5 6 9
- Initially -
5 6 7 8 9
- After the first operation -
7 6 5 8 9
- After the second one -
7 8 5 6 9