Question

In: Computer Science

The signature of each function is provided below, do not make any changes to them otherwise...

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]]

Solutions

Expert Solution

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.


Related Solutions

Match them please Changes its signature with each copy made during replication completely rewrites itself each...
Match them please Changes its signature with each copy made during replication completely rewrites itself each time it is replicated, sometimes changing its behavior exploits browser and plugin vulnerabilities to download and install malware without the user's knowledge, with the user having been specifically targeted a website with multiple transparent or opaque layers, tricking the user into clicking on a different button than they intended a seemingly useful program that contains hidden harmful code A. watering hole B. trojan horse...
// The Song class that represents a song // Do not make any changes to this...
// The Song class that represents a song // Do not make any changes to this file! public class Song { // instance variables private String m_artist; private String m_title; private Song m_link; // constructor public Song(String artist, String title) { m_artist = artist; m_title = title; m_link = null; } // getters and setters public void setArtist(String artist) { m_artist = artist; } public String getArtist() { return m_artist; } public void setTitle(String title) { m_title = title; }...
Complete the isScrambled() function to return True if stringA can be reordered to make stringB. Otherwise,...
Complete the isScrambled() function to return True if stringA can be reordered to make stringB. Otherwise, return False. Ignore spaces and capitalization. You must use a list for this assignment. isScrambled( 'Easy', 'Yase' ) returns True isScrambled( 'Easy', 'EasyEasy' ) returns False isScrambled( 'eye', 'eyes' ) returns False isScrambled( 'abcdefghijklmnopqrstuvwxyz', 'zyxwvutsrqponmlkjihgfedcba' ) returns True isScrambled( 'Game Test', 'tamegest' ) returns True.
There are several scenarios described below. For each of them, do the following (note: R.V. means...
There are several scenarios described below. For each of them, do the following (note: R.V. means random variable) (1) Define the R.V.--- that means something like, “Let X be the number of people who…..” (2) Define the distribution and parameter(s) of the R.V. (3) Give the support of the R.V. (4) Write the probability statement related to the information being sought. Do not calculate the probability. a) Joe is debugging a coding project. From prior experience, he expects to find...
For each statement below circle TRUE if the statement is true, otherwise circle FALSE.
For each statement below circle TRUE if the statement is true, otherwise circle FALSE. A hypothesis test uses data from a sample to assess a claim about a population. A randomization distribution will be centered at the value used in the null hypothesis. “Failing to reject the null hypothesis” and “accepting the null hypothesis” mean the same thing in a hypothesis test’s conclusion. The p-value is the probability, assuming the alternative hypothesis is true, of obtaining a sample statistic as...
Recommend changes to the company's business processes that will make them more effective and efficient. You...
Recommend changes to the company's business processes that will make them more effective and efficient. You are to analyze five key business processes: selling meat, purchasing animals, paying employees, purchasing miscellaneous supplies and services, and providing refunds to customers. The following interviews with the CEO and key employees describe those business processes. Where information is incomplete, you are to make appropriate assumptions.The company uses accrual accounting and a calendar fiscal year. Summary of Interview with CEO A few years ago,...
Ageing changes function of the human body. Discuss two changes in each section of the digestive...
Ageing changes function of the human body. Discuss two changes in each section of the digestive tract and the associated implications on major function of the digestive system. Provide at least five examples with detail explanation.
Choose any 3 types of regression and make a short comparison of them in a form...
Choose any 3 types of regression and make a short comparison of them in a form of table.
Write C functions to do each of the following. Make sure your function has exactly the...
Write C functions to do each of the following. Make sure your function has exactly the same name, parameters, and return type as specified here. Please place all of the functions in a single .c file and write a main() to test them. int process(const char *input_filename, const char *output_filename) — reads the file named input_filename and processes it, putting results in output_filename. The input file consists of lines of the form "number operation number," for example: 15 - 4...
Rewrite the sentences below with the adjective of the underlined words. Make changes to the sentence...
Rewrite the sentences below with the adjective of the underlined words. Make changes to the sentence structure if necessary Example: She speaks quietly. She speaks in a quiet voice. 1. My teacher explained the lesson clearly. _____________________________________________________________________________. 2. The agenda of the meeting is mainly about the budget. _____________________________________________________________________________. 3. Children have many ways to spend their vacations usefully. _____________________________________________________________________________. 4. We should use social media carefully. _____________________________________________________________________________. 5. He spoke to his students angrily as they performed poorly in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT