Summary - Arrays
- Algorithms course
- Beginner
- Lesson 12/80
- 4 min
Combine access and looping skills in a mini task.
Summary - Arrays
In this group you learned what arrays are, how to access, iterate and update them. Array access and update are O(1).
Iterating all elements with a loop takes O(n) time. Linear search is also O(n) because every element is checked.
Practice Print reverse
Write print_reverse(arr). Print elements from last to first with for. Call print_reverse([1, 2, 3]) to print 3, 2, 1.
Starter code
def print_reverse(arr):
# Print in reverseSample solution
def print_reverse(arr):
# Print in reverse
for i in range(len(arr) - 1, -1, -1):
print(arr[i])
print_reverse([1, 2, 3])Expected output
3 2 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Summary - Arrays: match the terms
| Term | Meaning |
|---|---|
arr[0] |
First element |
len(arr) |
Length |
O(1) |
Access |
for x in arr |
Iteration |
2 Array index
In an array, elements are accessed by ____.
In an array, elements are accessed by index.
Explanation: index is right.
3 What is the access complexity in an array?
- O(n)
- O(n^2)
- O(log n)
- O(1) Correct answer
Explanation: Array access is O(1).