Advertisement
ganiyuisholaafeez

Untitled

Feb 22nd, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. """ This function accepts a list of numbers
  2. builds up and returns a new list
  3. if a number is > 5 adds it to the end of the new  list
  4. otherwise, deletes the last number """
  5.  
  6. def push_and_pop(list_of_numbers):
  7.     push_pop = []               # Empty to create a new list
  8.     for list_of_number in list_of_numbers:
  9.         if list_of_number > 5:              # Integer > 5
  10.             push_pop.append(list_of_number) # Append it to the new list
  11.         else:
  12.             push_pop.pop()                  # Integer < = 5
  13.     return push_pop
  14.  
  15. print(push_and_pop([10]))   # [10]
  16. print(push_and_pop([10, 4]))    # []
  17. print(push_and_pop([10, 20, 30]))   # [10, 20, 30]
  18. print(push_and_pop([10, 20, 2, 30]))    # [10, 30]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement