Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the index of the target in the list or -1 if not found.
DSA Python
def linear_search(arr, target): for i in range(len(arr)): if arr[i] == [1]: return i return -1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the index 'i' instead of the target value in the comparison.
Comparing arr[i] to the whole list 'arr' instead of the target.
✗ Incorrect
We compare each element arr[i] with the target value to find a match.
2fill in blank
mediumComplete the code to return True if the target is found, otherwise False.
DSA Python
def contains(arr, target): for element in arr: if element == [1]: return True return False
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing element to itself instead of the target.
Returning False too early inside the loop.
✗ Incorrect
We check if each element equals the target to decide if it exists in the list.
3fill in blank
hardFix the error in the code to correctly perform linear search and return the index or -1.
DSA Python
def search(arr, target): i = 0 while i < len(arr): if arr[i] == [1]: return i i += 1 return -1
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[i] to the index variable 'i' instead of the target.
Comparing arr[i] to the whole list 'arr'.
✗ Incorrect
The comparison must be between arr[i] and the target value to find a match.
4fill in blank
hardFill both blanks to create a dictionary of elements and their indices for elements greater than 5.
DSA Python
result = { [1]: [2] for [2], [1] in enumerate(arr) if [1] > 5 }Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values in the dictionary comprehension.
Using undefined variables in the condition.
✗ Incorrect
We use 'x' as the element variable and 'i' as the index variable to create a dictionary of elements (keys) and their indices (values) for elements greater than 5.
5fill in blank
hardFill all three blanks to create a dictionary of elements and their indices for elements less than 10.
DSA Python
result = { [1]: [2] for [2], [1] in enumerate(arr) if [3] < 10 }Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values in the dictionary comprehension.
Using undefined variables in the condition.
✗ Incorrect
We use 'val' as the element variable and 'idx' as the index variable. The condition checks if 'val' is less than 10.
