CSC108H Winter 2024 Worksheet 18 : Parallel Strings and Lists
For each function, complete the examples in the docstring and then complete the function body.
1. def stretch_string(s: str, stretch_factors: list[int]) -> str:
"""Return a string consisting of the characters in s in the same order as in s,
repeated the number of times indicated by the item at the corresponding
position of stretch_factors.
Precondition: len(s) == len(stretch_factors) and the values in
stretch_factors are non-negative
>>> stretch_string('Hello', [2, 0, 3, 1, 1])
'HHllllo'
>>> stretch_string('echo', [0, 0, 1, 5])
"""
2. def greatest_difference(nums1: list[int], nums2: list[int]) -> int:
"""Return the greatest absolute difference between numbers at corresponding
positions in nums1 and nums2.
Precondition: len(nums1) == len(nums2) and nums1 != []
>>> greatest_difference([1, 2, 3], [6, 8, 10])
7
>>> greatest_difference([1, -2, 3], [-6, 8, 10])
"""