forked from codepath/compsci_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Increment by 1
Jessica Sang edited this page Sep 14, 2024
·
1 revision
Unit 1 Session 2 (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 always contain only numbers?
- Yes.
- Can the list be empty?
- Yes.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Loop through a list, and generate a new list containing each element + 1.
1) Create a function with parameters for the list
2) Make an empty list to store the results
3) Loop through the input list
a) For each element, add the element + 1 to the results list
4) Return the results list
def increment_values(lst):
incremented = []
for num in lst:
incremented.append(num + 1)
return incremented