To work on these problems, open the Parsons editor:
1
python3 parsons
Q2: Replace Elements
Complete the function replace_elements, a function which takes in source_list and dest_list and mutates the elements of dest_list to be the elements at the corresponding index in source_list.
dest_list always has a length greater than or equal to the length of source_list.
defreplace_elements(source_list, dest_list): """ Complete the function replace_elements, a function which takes in source_list and dest_list and mutates the elements of dest_list to be the elements at the corresponding index in source_list. dest_list always has a length greater than or equal to the length of source_list. >>> s1 = [1, 2, 3] >>> s2 = [5, 4] >>> replace_elements(s2, s1) >>> s1 [5, 4, 3] >>> s3 = [0, 0, 0, 0, 0] >>> replace_elements(s1, s3) >>> s3 [5, 4, 3, 0, 0] """ "*** YOUR CODE HERE ***" for i inrange(len(source_list)): dest_list[i] = source_list[i]
Code Writing Questions
Q3: Flatten
Write a function flatten that takes a list and “flattens” it. The list could be a deep list, meaning that there could be a multiple layers of nesting within the list.
For example, one use case of flatten could be the following:
defflatten(s): """Returns a flattened version of list s. >>> flatten([1, 2, 3]) # normal list [1, 2, 3] >>> x = [1, [2, 3], 4] # deep list >>> flatten(x) [1, 2, 3, 4] >>> x # Ensure x is not mutated [1, [2, 3], 4] >>> x = [[1, [1, 1]], 1, [1, 1]] # deep list >>> flatten(x) [1, 1, 1, 1, 1, 1] >>> x [[1, [1, 1]], 1, [1, 1]] """ "*** YOUR CODE HERE ***" res = [] for item in s: iftype(item) == list: res.extend(flatten(item)) else: res.append(item) return res
Use Ok to test your code:
1
python3 ok -q flatten✂️
Q4: Couple
Implement the function couple, which takes in two lists and returns a list that contains lists with i-th elements of two sequences coupled together. You can assume the lengths of two sequences are the same. Try using a list comprehension.
Hint: You may find the built in range function helpful.
defcouple(s, t): """Return a list of two-element lists in which the i-th element is [s[i], t[i]]. >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> couple(a, b) [[1, 4], [2, 5], [3, 6]] >>> c = ['c', 6] >>> d = ['s', '1'] >>> couple(c, d) [['c', 's'], [6, '1']] """ assertlen(s) == len(t) "*** YOUR CODE HERE ***" defmake_pair(a, b): return [a, b] res = [] for index inrange(len(s)): pair = make_pair(s[index], t[index]) res.append(pair) return res
Use Ok to test your code:
1
python3 ok -q couple✂️
Q5: Insert Items
Write a function which takes in a list lst, an argument entry, and another argument elem. This function will check through each item in lst to see if it is equal to entry. Upon finding an item equal to entry, the function should modify the list by placing elem into lst right after the item. At the end of the function, the modified list should be returned.
See the doctests for examples on how this function is utilized.
Important: Use list mutation to modify the original list. No new lists should be created or returned.
Note: If the values passed into entry and elem are equivalent, make sure you’re not creating an infinitely long list while iterating through it. If you find that your code is taking more than a few seconds to run, the function may be in a loop of inserting new values.