forked from codepath/compsci_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Negative Numbers
Jessica Sang edited this page Sep 14, 2024
·
1 revision
Unit 1 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Will the list only contain numbers?
- Yes.
- Is there always a negative number in the list?
- No. If there are no negative numbers, the function should not print anything.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a function that prints all negative numbers in the list.
1) Loop through each number in the list
2) If the current number is less than zero, print it out
def print_negatives(lst):
for number in lst:
if number < 0:
print(number)
print_negatives([1,-2, 4, 3, -5])
# Output:
# -2
# -5
print_negatives([1,2,3,4,5])
# No output