Question: Write the code for a Python function called dotProduct that accepts a dictionary containing 'n' equal length lists such that each list is stored using
Write the code for a Python function called dotProduct that accepts a dictionary containing 'n' equal length lists such that each list is stored using a unique key. You may assume there are a minimum of 2 keys in the dictionary, but there may be 3, 4, 10, 1000, etc, or more keys. This function performs a dot product multiplication of all lists in the dictionary. In mathematics, the dot product or scalar product is an algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors), and returns a single number.
The function prototype: def dotProduct(myDictParam) (in the fill-in section)
An example of a dot product of 2 lists [1, 2, 3] and [6, 7, 8] would be: [1, 2, 3] [6, 7, 8] 1 * 6 + 2 * 7 + 3 * 8 = 44
An example of a dot product of 3 lists [-1, 6, 4, 22], [5, 2, 8, -1], and [7, 3, 1, 5], would be: [-1, 6, 4, 22] [ 5, 2, 8, -1] [ 7, 3, 1, 5] -1 * 5 * 7 + 6 * 2 * 3 + 4 * 8 * 1 + 22 * -1 * 5 = -77 NOTE: Your solution MUST use both the LAMBDA and REDUCE functions.
The whole code is:
import math import random import string import collections import datetime import re import time import copy from functools import reduce
def loadDictionary(book, sequence, key = 'X') : book[key] = sequence return
def dotProduct(myDictParam) : # your code here... (fill in this) # end def
def main( ) : letters = ['M', 'X', 'Y', 'Z'] myDict = { } seq1 = [31, 32, 33, 34, 35, 36, 37, 38, 39] seq2 = [5, -3, 8, 17, 1, 9, -4, -2, 7] seq3 = [6, -5, 12, -3, 25, 18, -7, 21, 88] seq4 = [8, 2, 29, 8, 10, 4, 91, 76, 15] newDict = loadDictionary(myDict, seq1, letters[0]) # key of 'M' newDict = loadDictionary(newDict, seq2) # key will use default value of 'X' result = dotProduct(newDict) print(newDict) print("dotProduct with seq1 and seq2: ", result) newDict = loadDictionary(newDict, seq3, letters[2]) # key of 'Y' result = dotProduct(newDict) print(newDict) print("dotProduct with seq1, seq2, and seq3: ", result) newDict = loadDictionary(newDict, seq4, letters[3]) # key of 'X' result = dotProduct(newDict) print(newDict) print("dotProduct with seq1, seq2, seq3, and seq4: ", result) print("keys in final dictionary: ", newDict.keys( )) if __name__ == "__main__" : main( )
The OUTPUT should be EXACTLY as displayed below: {'M': [31, 32, 33, 34, 35, 36, 37, 38, 39], 'X': [5, -3, 8, 17, 1, 9, -4, -2, 7]} dotProduct with seq1 and seq2: 1309 {'M': [31, 32, 33, 34, 35, 36, 37, 38, 39], 'X': [5, -3, 8, 17, 1, 9, -4, -2, 7], 'Y': [6, -5, 12, -3, 25, 18, -7, 21, 88]} dotProduct with seq1, seq2, and seq3: 33015 {'M': [31, 32, 33, 34, 35, 36, 37, 38, 39], 'X': [5, -3, 8, 17, 1, 9, -4, -2, 7], 'Y': [6, -5, 12, -3, 25, 18, -7, 21, 88], 'Z': [8, 2, 29, 8, 10, 4, 91, 76, 15]} dotProduct with seq1, seq2, seq3, and seq4: 451818 keys in final dictionary: dict_keys(['M', 'X', 'Y', 'Z']) Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
