0
0
DSA Pythonprogramming~10 mins

Array Traversal Patterns in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print all elements in the array using a for loop.

DSA Python
arr = [10, 20, 30, 40]
for [1] in arr:
    print([1])
Drag options to blanks, or click blank then click option'
Ax
Bi
Citem
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using an index variable instead of the element variable.
Using different variable names in the loop and print statement.
2fill in blank
medium

Complete the code to print elements at even indices of the array.

DSA Python
arr = [5, 10, 15, 20, 25]
for i in range(0, len(arr), [1]):
    print(arr[i])
Drag options to blanks, or click blank then click option'
A1
B2
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using step size 1 which prints all elements.
Using step size 3 or 4 which skips too many elements.
3fill in blank
hard

Fix the error in the code to print elements in reverse order.

DSA Python
arr = [1, 2, 3, 4, 5]
for i in range(len(arr)-1, [1], -1):
    print(arr[i])
Drag options to blanks, or click blank then click option'
A1
B0
C-1
Dlen(arr)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as stop which excludes index 0.
Using len(arr) which is out of range.
4fill in blank
hard

Fill both blanks to create a dictionary with elements as keys and their indices as values.

DSA Python
arr = ['a', 'b', 'c']
index_map = [1] for [2] in range(len(arr))}
Drag options to blanks, or click blank then click option'
A{arr[i]: i
Barr[i]
Ci
Drange(len(arr))
Attempts:
3 left
💡 Hint
Common Mistakes
Using the element as the loop variable instead of the index.
Swapping key and value in the dictionary.
5fill in blank
hard

Fill all three blanks to create a list of squares of even numbers from the array.

DSA Python
arr = [1, 2, 3, 4, 5, 6]
squares = [[1] for [2] in arr if [3] % 2 == 0]
Drag options to blanks, or click blank then click option'
Ax**2
Bx
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and condition.
Not squaring the variable in the output expression.