Question

In: Computer Science

Write the following Python script: Imagine you live in a world without modules in Python! No...

Write the following Python script:

Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it is actually tricky to make a useful list of zeros. For instance, if you need to start with a 5 row, 6 column double list of 0, you might be tempted to try:

'''

thing = [ [ 0 ] ∗ 6 ] ∗ 5

'''

and if you look at it in the console window, it would appear to be a 5 by 6 list of lists containing all zeros! However - try the following and see what happens:

'''

thing [ 2 ] [ 5 ]

thing

'''

Notice that the item in index 5 of every row has changed, not just the row with index 2! Given the difficulty, I will give you the line of code that will create a list of lists that is num_rows by num_cols:

'''

ans = [ [ 0 for col in range ( num_cols ) ] for row in range ( num_rows ) ]

'''

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

def mat_mult(m1, m2):
    if len(m1) == 0 or len(m2) == 0:
        print('Invalid sizes')
        return None
    if len(m1[0]) != len(m2):
        print('Invalid sizes')
        return None

    num_rows, num_cols = len(m1), len(m2[0])
    result = [[0 for col in range(num_cols)] for row in range(num_rows)]
    # iterate through rows of m1
    for i in range(len(m1)):
        # iterate through columns of m2
        for j in range(len(m2[0])):
            # iterate through rows of m2
            for k in range(len(m2)):
                result[i][j] += m1[i][k] * m2[k][j]

    return result


X = [[1,2,1],[1,2,3],[4,5,6]]
Y = [[1,1,1],[2,2,2]]

print(mat_mult(X,Y))

==========================================================================


Related Solutions

How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
Write a Python script that: Ask the customer for their current bank balance. One thing you...
Write a Python script that: Ask the customer for their current bank balance. One thing you dont have to worry about is whether or not the user enters a negative or positive amount.. Ask the customer if they wish to deposit ('d') or withdraw ('w') from that amount. Ask them to enter the amount of money they are either depositing or withdrawing. This has to be a positive numeric value. If it is negative, you should print an error message...
Write the following Python script: Problem Statement A string X is an anagram of string Y...
Write the following Python script: Problem Statement A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which...
Write the following Python script: Pikachu is a well-known character in the Pokemon anime series. Pikachu...
Write the following Python script: Pikachu is a well-known character in the Pokemon anime series. Pikachu can speak, but only 3 syllables: "pi", "ka", and "chu". Therefore Pikachu can only pronounce strings that can be created as a concatenation of one or more syllables he can pronounce. For example, he can pronounce the words "pikapi" and "pikachu". You are given a String word. Your task is to check whether Pikachu can pronounce the string. If the string can be produced...
Write a Python script that takes an input image and output's the name of the dominant...
Write a Python script that takes an input image and output's the name of the dominant color in that image(i.e. red, green, blue).  
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
Python, filing and modules Modify the driver to write the statistics to a file named employee-stats.txt...
Python, filing and modules Modify the driver to write the statistics to a file named employee-stats.txt rather than to the console. driver.py code: from employee import Employee employees = [] # Load an employee object for each employee specified in 'employees.txt' into the employees list. employee.txt Jescie Craig VP 95947 Marny Mckenzie Staff 52429 Justin Fuller Manager 72895 Herman Love VP 108204 Hasad Wilkins Staff 96153 Aubrey Eaton Staff 76785 Yvonne Wilkinson Staff 35823 Dane Carlson Manager 91524 Emma Tate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT