Advertisement
SimeonTs

SUPyF Lists - 07. Remove Negatives and Reverse

Jun 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. """
  2. Lists
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/924#6
  4.  
  5. 07. Remove Negatives and Reverse
  6.  
  7. Условие:
  8. Read a list of integers, remove all negative numbers from it and print the remaining items in reversed order.
  9. In case of no items left in the list, print “empty”.
  10. Examples
  11. Input               Output
  12. 10 -5 7 9 -33 50    50 9 7 10
  13. 7 -2 -10 1          1 7
  14. -1 -2 -3            empty
  15. Hints
  16. -Read the list
  17. -Create a new empty list for the results.
  18. -Scan the input list from the end to the beginning. Check each item and append all non-negative items to the result list.
  19. -Finally, print the results list (at a single line holding space-separated numbers).
  20. """
  21.  
  22. """
  23. How I solved it:
  24.  
  25. First we will take all the added integers on one line divided by space and it will add them in a new list one by one:
  26. We will create a if_empty variable to check if the list is empty later.
  27. Then using a  reversed() For Loop, we will make a if, else construction where if will check for negative numbers
  28. and if there are some. It will remove them. Else it will print as they are positive and it will set the if empty
  29. checker to False (since the List will still have numbers). And finally We will check if_empty is True and if so
  30. it will print "empty"  
  31. """
  32.  
  33. nums = [int(item) for item in input().split(" ")]
  34.  
  35. if_empty = True
  36.  
  37. for num in reversed(nums):
  38.     if num < 0:
  39.         nums.remove(num)
  40.     else:
  41.         print(num, end=" ")
  42.         if_empty = False
  43.  
  44. if if_empty:
  45.     print("empty")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement