Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 

def integerToRoman(n) : romans = { 1:"I", 5:"V", 10:"X", 50:"L", 100:"C", 500:"D", 1000:"M" } romanString = "" th = 0 if n < 1 or n > 3999 : romanString = "Invalid" else : # computations th = int(n / 1000) # thousands for i in range(th) : romanString += romans[1000] n -= 1000 print("DEBUG only: leftover after computing the thousands digits: ", n) return romanString # end def nums = [1299, 2257, 3892, 1000, 2000, 3000, 88, 192, 912 ] for i in nums : rv = integerToRoman(i) print("num: ", i, " roman: ", rv)

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( ) 

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

Step: 3

blur-text-image

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

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions

Question

10. Microsoft Corporation

Answered: 1 week ago