-
Notifications
You must be signed in to change notification settings - Fork 243
Odd Indices
Sar Champagne Bielert edited this page Apr 8, 2024
·
5 revisions
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.
- Can the list be empty?
- Yes. If the list is empty, nothing should print.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Loop through a list, and if the index is odd, print out that element.
- A number is odd when it is NOT evenly divisible by two. How can we check that in python?
def print_indices(lst):
for index in range(len(lst)):
if index % 2 == 1:
print(lst[index])
- Q: What if I used a counter variable?
- A: While this solution will also work, the extra variable takes up unnecessary space in memory. Using
range(len(LIST))
is the more efficient way to solve the problem in Python.