Advertisement
SimeonTs

SUPyF Exam 24.03.2019 - 02. Command Center

Aug 13th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1590#1
  4.  
  5. SUPyF Exam 24.03.2019 - 02. Command Center
  6.  
  7. Problem:
  8. Input / Constraints
  9. We are going to receive a list of integers from console.
  10. After that we will start receive some of the following commands in format:
  11. • swap {index1} {index2}
  12. • enumerate_list
  13. • max
  14. • min
  15. • get_divisible by {number}
  16.  
  17. *If you receive command 'swap' you should check if the indexes are valid. A valid index is index which is 0 or higher
  18. and is less than list length.
  19.    -   If one of the indexes is not valid just print the list without changing it
  20.    -   If both indexes are valid swap the two elements on these indexes
  21.  
  22. *If you receive ‘enumerate_list’ you should enumerate the list and print it in the following format:
  23.    [(0, {list[0]}), (1, list[1]), (2, list[2]), (3, list[3])]
  24. Where {list[n]} is the element corresponding to the given index (starting from zero)
  25.  
  26. *If you receive 'max', print the max number in the list
  27. *If you receive 'min', print the min number in the list
  28.  
  29. *If you receive ‘get_divisible by’ you must print every element in the list which residue after division with {number}
  30. is 0 in format:
  31. [el1, el2, ….]
  32. It is guaranteed -  the {number} never will be 0, so you do not need to check it.
  33.  
  34. Output
  35. When you receive a command which says 'end', you should print the count of commands you have performed.
  36. Note that invalid commands may appear. In this case do not print anything and do not count these commands as performed.
  37.  
  38. Examples:
  39.    Input:
  40.        1 3 2 4 5
  41.        swap 1 15
  42.        enumerate_list
  43.        max
  44.        get_divisible by 13
  45.        get_divisible by 2
  46.        swap 1 4
  47.        enumerate_listtt
  48.        end
  49.    Output:
  50.        [1, 3, 2, 4, 5]
  51.        [(0, 1), (1, 3), (2, 2), (3, 4), (4, 5)]
  52.        5
  53.        []
  54.        [2, 4]
  55.        [1, 5, 2, 4, 3]
  56.        6
  57.    Input:
  58.        15 -1 3 0 19 -15 24
  59.        swap 0 1
  60.        swap 4 6
  61.        enumerate_list
  62.        swap 6 1
  63.        swap 7 -1
  64.        get divisible by -15
  65.        get_divisible by 15
  66.        get_divisibleee by 15
  67.        end
  68.    Output:
  69.        [-1, 15, 3, 0, 19, -15, 24]
  70.        [-1, 15, 3, 0, 24, -15, 19]
  71.        [(0, -1), (1, 15), (2, 3), (3, 0), (4, 24), (5, -15), (6, 19)]
  72.        [-1, 19, 3, 0, 24, -15, 15]
  73.        [-1, 19, 3, 0, 24, -15, 15]
  74.        [0, -15, 15]
  75.        6
  76. """
  77.  
  78. nums = [int(item) for item in input().split(" ")]
  79.  
  80. valid_operations = 0
  81.  
  82. while True:
  83.     command = input()
  84.     if command == "end":
  85.         break
  86.     a = [item for item in command.split(" ")]
  87.     if a[0] == "swap" and len(a) == 3:
  88.         if 0 <= int(a[1]) <= len(nums) and 0 <= int(a[2]) < len(nums):
  89.             nums[int(a[1])], nums[int(a[2])] = nums[int(a[2])], nums[int(a[1])]
  90.             print(nums)
  91.             valid_operations += 1
  92.         else:
  93.             print(nums)
  94.             valid_operations += 1
  95.     elif command == "enumerate_list":
  96.         print(list(enumerate(nums, 0)))
  97.         valid_operations += 1
  98.     elif command == "max":
  99.         print(max(nums))
  100.         valid_operations += 1
  101.     elif command == "min":
  102.         print(min(nums))
  103.         valid_operations += 1
  104.     elif a[0] == "get_divisible" and a[1] == "by" and len(a) == 3:
  105.         divisible_list = []
  106.         for num in nums:
  107.             if num % int(a[2]) == 0:
  108.                 divisible_list += [num]
  109.         print(divisible_list)
  110.         valid_operations += 1
  111.  
  112. print(valid_operations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement