fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • tuple unpacking

    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:

    Challenge

    Given a list of n numbers, you are asked to perform o operations 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 n and o - the number of elements in the list and the number of operations you need to perform. The next line contains n integers separated by a space. The next o lines 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.
    Input
    Output
    5 2 5 6 7 8 9 0 2 1 3
    7 8 5 6 9
    Explanation:
    1. Initially - 5 6 7 8 9
    1. After the first operation - 7 6 5 8 9
    1. After the second one - 7 8 5 6 9
     
    To check your solution you need to sign in
    Sign in to continue