Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Exercises: Question #1 Write the code for a Python function called loadDictionary that accepts a dictionary, a list, and a letter (with a default

Lab Exercises: Question #1 Write the code for a Python function called loadDictionary that accepts a dictionary, a list, and a letter (with a default value of 'X') and stores the list in the dictionary using the letter as the key before returning the entire dictionary. The function prototype MUST be: def loadDictionary(book, sequence, key = 'X') : For example, given the following variables: letter = 'Q' myDict = { } seq1 = [31, 32, 33, 34, 35, 36, 37, 38, 39] then the function call and print: newDict = loadDictionary(myDict, seq1, letter) print(newDict) would display: {'Q': [31, 32, 33, 34, 35, 36, 37, 38, 39]} Question #2 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. 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 The function prototype MUST be: def dotProduct(myDictParam) : 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 lamba and reduce functions. Question #3: Years ago the Romans used a different system to represent numbers. Instead of using the digits (0, 1, 2, 3, 4, 5, 6, etc.), the Romans formed numbers by joining combinations of the characters (I, V, X, L, C, D, and M). Roman Numeral characters and their integer values are: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M = 1000. Examples of simple Roman Numerals are: VIII (5+1+1+1 = 8), XV (10+5 = 15), LXVI (50+10+5+1 = 66), CLXXV (100+50+10+10+5 = 175), MXV (1000+10+5 = 1015), where the integer amounts are arrived at by simply adding the individual numeral values one after the other. However, the Romans decided not to use the same 4 consecutive numerals when forming numbers, and instead formed values such as 4, 9, 14, 19, etc., using subtraction as follows: IV = (5-1 = 4), IX = (10-1 = 9), XIV = (10+5-1 = 14), where a smaller numeral is placed to the left of the larger numeral, and the smaller numeral's value is subtracted from the larger numerals value. As well, only the numerals I, X, and C are used for subtraction, and then, only in the following cases: I can only be subtracted from V or X, X can only be subtracted from L or C, and C can only be subtracted from D or M. Additionally, at most a single (1) numeral can be subtracted from another numeral. So, with the symbols and rules above, write the code for a Python function with the following prototype: def integerToRoman(n) : that accepts an integer number from 1 to 3999 and returns the value as a Roman Numeral as a string. If the value 'n' is outside the range 1 to 3999 inclusive, then no conversion is performed and a string of "Invalid" is returned. Your soltion MUST use the following dictionary: keys: 1, 5, 10, 50, 100, 500, 1000 values: I, V, X, L, C, D, M For example: 1929 would be: MCMXXIX index 0 1 2 3 4 5 6 _ _ _ _ _ _ _ |M|C|M|X|X|I|X| | |_| | | |_| 1000__| | | | |____9 1000 + 900 + 10 + 10 + 9 = 1929 | | |10 900______| | |_10 HINT: The only situations in which you will need to consider subtraction is when the ones, tens, or hundreds columns are either 4 or 9. Begin by computing the thousand's digit by determining if the number / 1000 results in a value >= 1, and if so, given that the first digit can only be a 1, 2, or 3, the Roman numeral will begin with either an "M", "MM", or "MMM" (added to a string). After determining the the first digit's value, subtract that amount (which will be either 1000, 2000, or 3000) from the number. Then, use a loop that continues until the number becomes 0. Determine the value of the hundred's digit by dividing by 100. If the digit is a 9, 5, or 4, then the hundreds portion will result in a Roman numeral of "CM", "D", or "CD" (added to the string), and requiring subtraction of either 900, 500, or 400 from the number. If the hundred's digit is any other value, simply add a "C" to the string and subtract 100 each time through the loop. Continue this process for the 10's and 1's columnns using the appropriate symbols for those digits! NOTE: This solution will require many if : / elif : / else : statements! For example: roman = integerToRoman(999) print(roman) # would display: CMXCIX roman = integerToRoman(8) print(roman) # would display: VIII roman = integerToRoman(-1) print(roman) # would display: Invalid MAIN PROGRAM: import math import random import string import collections import datetime import re import time import copy from functools import reduce # YOUR CODE BELOW...  def loadDictionary(myDict, seq1, letter='X') : # your code here... # end def def dotProduct(myDictParam) : # your code here... # end def def integerToRoman(n) : # your code here... # 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( )) for i in range(51) : print(integerToRoman(i)) romansList = [ 499, 500, 504, 508, 509, -6, 600, 777, 800, 850, 860, 870, 880, 890, 900, 1000, 1004, 1008, 1009, 1222, -1222, 1400, 1404, 1409, 1889, 1900, 1909, 1929, 1969, 5000, 3450, 3979, 3999, 4000, 4001, 88 ] for i in romansList : print(integerToRoman(i)) # end main( ) 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']) Invalid I II III IV V VI VII VIII IX X XI XII XIII XIV XV XVI XVII XVIII XIX XX XXI XXII XXIII XXIV XXV XXVI XXVII XXVIII XXIX XXX XXXI XXXII XXXIII XXXIV XXXV XXXVI XXXVII XXXVIII XXXIX XL XLI XLII XLIII XLIV XLV XLVI XLVII XLVIII XLIX L CDXCIX D DIV DVIII DIX Invalid DC DCCLXXVII DCCC DCCCL DCCCLX DCCCLXX DCCCLXXX DCCCXC CM M MIV MVIII MIX MCCXXII Invalid MCD MCDIV MCDIX MDCCCLXXXIX MCM MCMIX MCMXXIX MCMLXIX Invalid MMMCDL MMMCMLXXIX MMMCMXCIX Invalid Invalid LXXXVIII

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions