Question: PYTHON LANGUAGE QUESTION, CREATE A FUNCTION IN PYTHON TO CALL IN MAIN: def integerToRoman(n) : that accepts an integer number from 1 to 3999 and
PYTHON LANGUAGE QUESTION, CREATE A FUNCTION IN PYTHON TO CALL IN MAIN: 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 integerToRoman(n) : # your code here # end def def main( ) : 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)) if __name__ == "__main__" : main( )
OUTPUT SHOULD BE EXACTLY (INVALID MEANS NOT WITHIN RANGE):
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
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
