In: Computer Science
The signature of each function is provided below, do not make any changes to them otherwise the tester will not work properly. The following are the functions you must implement:
mashup(lst) [20pts]
Description: Creates a new string that is based on the provided list. Each number in the list specifies how many characters from the string that follows belong in the resulting string. Parameters: lst is a list of variable size, that contains alternating ints and strings Assumptions: When a pair of values doesn’t make sense, throw out that pair. When you have an empty string in the list, move on to the next pair. When you have a number that is larger than the length of the string, move on to the next pair, etc. Return value: the new string that is generated from the replacements Examples: mashup([2, 'abc', 1, 'def']) → 'abd' mashup([3, 'rate', 2, 'inside', 1, 'goat']) → 'rating'
expand(numbers, amount) [20pts]
Description: Given a list of numbers it returns that same list that has been expanded with a certain amount of zeroes around all of the numbers, including at the beginning and end of the list. Parameters: numbers is a list of mixed int and float Assumptions: You will always have at least one element in numbers. amount will be >= 0 Return value: Nothing is returned. The swapping occurs in-place, i.e. you modify numbers itself Examples: ls = [1,2,3] expand(ls, 1) # nothing is returned! print(ls) # prints [0,1,0,2,0,3,0] ls = [1.5, -6, 4, 0] expand(ls, 2) # nothing is returned! print(ls) # prints [0, 0, 1.5, 0, 0, -6, 0, 0, 4, 0, 0, 0, 0, 0]
Assumptions for the following two problems: There will be at least one row and one column in the matrix. All rows will have the same number of elements.
squarify(matrix) [25pts]
Description: Determine the size of the largest square that can be made with the given matrix. Construct a new square matrix of this size using the elements from the original matrix in their original order. Parameters: matrix (list of lists of int) Return value: A new matrix (list of lists of int) Examples: ls = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] new_ls = squarify(ls) print(new_ls) # prints [[1, 2, 3], [5, 6, 7], [9, 10, 11]]
apply(mask, matrix) [25pts]
Description: Given a matrix, apply the mask. The matrix is some MxN list of list of ints, and the mask is exactly a 2x2 list of lists of ints. Imagine you overlay the mask on top of the matrix, starting from the top left corner. There will be 4 places that overlap. Add each pair of numbers that are overlapped, and update the original matrix with this new value. Shift the mask down the row of the matrix to the next 2x2 that hasn't been updated already, and continue this process. Keep doing this down the columns as well. If you are on an edge and only a piece of the mask overlaps, you can ignore the other numbers and only update the overlapping portion. Parameters: matrix (MxN list of list of ints) and mask (2x2 list of list of ints) Return value: Nothing is returned. The updating occurs in-place, i.e. you modify matrix itself Examples: ls = [[1,2],[3,4]] apply([[1,1],[1,1]], ls) # nothing is returned! print(ls) # prints [[2, 3], [4, 5]] ls = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] apply([[1,1],[1,1]], ls) # nothing is returned! print(ls) # prints [[2, 3, 4], [5, 6, 7], [8, 9, 10]] ls = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] apply([[1,0],[0,1]], ls) # nothing is returned! print(ls) # prints [[2, 2, 4], [4, 6, 6], [8, 8, 10], [10, 12, 12]]
The python code for the first question is:
def mashup(lst):
new_str = '' # empty string is initialised
i = 0
while i < len(lst):
k = 0
while k < lst[i] and len(lst[i+1]) >= lst[i]:
new_str += lst[i+1][k]
k += 1
i += 2
return new_str
print(mashup([3, 'rate', 2, 'inside', 1, 'goat']))
The sample of the code and the output is:
The python code for the second question is:
def expand(numbers, amount):
i = 0
while i < len(numbers):
j = 0
while j < amount: # number of times 0's are added to the list
numbers.insert(i, 0)
j += 1
i += amount + 1
i = len(numbers)
j = 0
while j < amount: # the zeros are added after the last element
numbers.insert(i, 0)
j += 1
ls = [1.5,-6,4,0]
expand(ls,2)
print(ls)
The sample of the code and output is:
The python code for the third question is:
def squarify(matrix):
rows = len(matrix) # no. of elements in the list matrix
columns = len(matrix[0]) # no. of elements in the first list of list matrix
if rows > columns: # minimum of rows and columns is found
min_rc = columns
else:
min_rc = rows
new_matrix = []
for i in range(min_rc):
n = []
for j in range(min_rc):
n.append(matrix[i][j]) # columns of each row are filled
new_matrix.append(n) # the row is now appended to the list
return new_matrix
print(squarify([[1,2,3,4],[5,6,7,8],[9,10,11,12]]))
The sample and output of the code is:
The python code for the fourth question is:
def apply(mask, matrix):
val_1 = mask[0][0] # storing first value in val_1
val_2 = mask[0][1] # storing second value in val_2
val_3 = mask[1][0] # storing third value in val_3
val_4 = mask[1][1] # storing fourth value in val_4
for i in range(len(matrix)):
if i % 2 == 0: # even index values of matrix overlap val_1 or val_2
for j in range(len(matrix[0])):
if j % 2 == 0: # even index values of matrix[i] overlap val_1
matrix[i][j] += val_1
else: # else val_2 overlap
matrix[i][j] += val_2
else: # odd index values of matrix overlap val_3 or val_4
for j in range(len(matrix[0])):
if j % 2 == 0: # even index values of matrix[i] overlap val_3
matrix[i][j] += val_3
else: # else val_4 overlap
matrix[i][j] += val_4
ls = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
apply([[1,0],[0,1]],ls)
print(ls)
The sample of the code and output is:
If you have any queries regarding the code of any question comment down. I will help you out as soon as possible.