Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ This function accepts a list of numbers
- builds up and returns a new list
- if a number is > 5 adds it to the end of the new list
- otherwise, deletes the last number """
- def push_and_pop(list_of_numbers):
- push_pop = [] # Empty to create a new list
- for list_of_number in list_of_numbers:
- if list_of_number > 5: # Integer > 5
- push_pop.append(list_of_number) # Append it to the new list
- else:
- push_pop.pop() # Integer < = 5
- return push_pop
- print(push_and_pop([10])) # [10]
- print(push_and_pop([10, 4])) # []
- print(push_and_pop([10, 20, 30])) # [10, 20, 30]
- print(push_and_pop([10, 20, 2, 30])) # [10, 30]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement