-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2.py
More file actions
32 lines (21 loc) · 812 Bytes
/
Q2.py
File metadata and controls
32 lines (21 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def find_and_replace(lst, find_val, replace_val):
"""
Task 1
- Create a function that searches for all occurrences of a value (find_val) in a given list (lst) and replaces them with another value (replace_val).
- lst must be a list.
- Return the modified list.
"""
# Validate that lst is a list
if not isinstance(lst, list):
return -1
# Replace all occurrences
for i in range(len(lst)):
if lst[i] == find_val:
lst[i] = replace_val
return lst
# Task 2
# Invoke the function "find_and_replace" using the following scenarios:
# - [1, 2, 3, 4, 2, 2], 2, 5
# - ["apple", "banana", "apple"], "apple", "orange"
print(find_and_replace([1, 2, 3, 4, 2, 2], 2, 5))
print(find_and_replace(["apple", "banana", "apple"], "apple", "orange"))