SimeonTs

SUPyF2 P.-Mid-Exam/30 June 2019/1 - Number Array

Oct 28th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. """
  2. Programming Fundamentals Mid Exam - 30 June 2019 Group 1
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1682#1
  4.  
  5. SUPyF2 P.-Mid-Exam/30 June 2019/1 - Number Array
  6.  
  7. Problem:
  8. Create a program that helps you keep track of a number array.
  9. First, you are going to receive the numbers оn a single line, separated by space, in the following format:
  10. "{number1} {number2} {number3}… {numbern}"
  11. Then you will start receiving commands until you read the "End" message. There are five possible commands:
  12. • "Switch {index}"
  13. o   Find the number on this index in your collection, if the index exists, and switch its sign (negative <-> positive).
  14. • "Change {index} {value}"
  15. o   Replace the number on the given index with the number given, if the index exists.
  16. • "Sum Negative"
  17. o   Print the sum of all negative numbers.
  18. • "Sum Positive"
  19. o   Print the sum of all positive numbers.
  20. • "Sum All"
  21. o   Print the sum of all numbers.
  22. In the end, print the positive numbers on a single line, keeping in mind that 0 is positive,
  23. separated by a single space in the following format:
  24. "{number1} {number2} {number3}… {numbern}"
  25. Input
  26. • On the 1st line you are going to receive the numbers of the array (always integers), separated by a single space.
  27. • On the next lines, until the "End" command is received, you will be receiving commands.
  28. Output
  29. • Print the tasks in the format described above.
  30.  
  31. Examples:
  32. Input:              Output:
  33. 1 2 3 4 5           -8
  34. Switch 4            2 3 4
  35. Change 0 -3
  36. Sum Negative
  37. End
  38.  
  39. Comments:
  40. First, we receive the command "Switch 4" and we make the number on index 4 negative
  41. (because it is positive before the command). After this command, the task collection looks like this:
  42. 1 2 3 4 -5
  43. Afterwards, we receive the "Change 0 -3" command and we need to change the number on index 0 with the number -3.
  44. The collection looks like this now:
  45. -3 2 3 4 -5
  46. After that, we receive the "Sum Negative" command,
  47. which means we need to print the sum of all negative numbers and it is -8.
  48. In the end, we print all of the positive numbers. This is the result collection:
  49. 2 3 4
  50.  
  51. Input:                      Output:
  52. 1 2 3 4 5 4 3 2 1 0         23
  53. Switch -4                   2 3 4 5 4 3 2 1 0
  54. Change 13 0
  55. Switch 0
  56. Sum All
  57. End
  58. """
  59. nums = [int(num) for num in input().split()]
  60.  
  61. while True:
  62.     command = input().split()
  63.     if command[0] == "End":
  64.         print(' '.join([str(num) for num in nums if num >= 0]))
  65.         break
  66.  
  67.     elif command[0] == "Switch":
  68.         index = int(command[1])
  69.         if 0 <= index < len(nums):
  70.             nums[index] = nums[index].__invert__() + 1
  71.  
  72.     elif command[0] == "Change":
  73.         index, value = int(command[1]), int(command[2])
  74.         if 0 <= index < len(nums):
  75.             nums[index] = value
  76.  
  77.     elif command[0] == "Sum":
  78.         if command[1] == "Negative":
  79.             print(sum([num for num in nums if num < 0]))
  80.  
  81.         elif command[1] == "Positive":
  82.             print(sum([num for num in nums if num >= 0]))
  83.  
  84.         elif command[1] == "All":
  85.             print(sum(nums))
Add Comment
Please, Sign In to add comment