Question

Write a Python program (question2.py) that reads from a file called “input.txt” numbers in [1,39] separated in by commas. The numbers are in [1-99]. The program will then convert each number to a possible Roman Numeral equivalent, and print it on the screen. Remember, I is 1, V is 5, X is 10

For example, if the input is: 23, 11 the output is: XXIII, XI.ROMAN NUMERALS CHART 1 TO 100 69 LXIX 11 2 11 3 III 4 IV 5 V 6 VI 1 VII 8 VIII 9 IX 10 X 11 XI 12 XII 13 XIII 14 XIV 15 XV 16

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

f = open("input.txt")
data = f.read()
# split the data using comma as seperator
# convert string list into integers list using map and lambda
numbers = map(lambda x: int(x),data.split(","))
# map of where all changes happens in roman numbers
roman_map = [(100,"C"),(90,"XC"),(50,"L"),(40,"XL"),(10,"X"),(9,"IX"),(5,"V"),(4,"IV"),(1,"I")]

# process numbers one by one
for number in numbers:
    #print number =>, here end is not new line but space
    print(number, "=>",end=" ")
    # process all factors in roman numerals
    # 0th element of each x is integer and 1st is roman numerals
    for x in roman_map:
        # we can get number of times the roman character should be
        # printed by dividing our number with the factor
        # if it's 0 then nothing will be printed
        count = number // x[0]
        # print the character as times as we calculated
        print(x[1]*count,end="")
        # reduce the this count times the factor as we processed it
        number -= (x[0]*count)
    # lets print a new line for next result
    print()

Explanation

I've provided most of the explanation in the code as comments but I'll give a general working logic here too.

The program starts by reading input.txt

by using read method it reads all data of file and store it into data variable.

Now using split method we split it into a list of strings.

Now using map we process each strings and convert it to integer datatype as specified in our inline lambda function.

Now to convert the number into roman numbers first we need analyse how number or character pattern changes in roman numerical system.Since our question only points us to do the calculations for numbers till 100 we only going to do that.

First change of pattern occurs in number 4

that's

1 - I

2 - II

3 - III

4 - IV

Since the beginning of number 1 the number starts to show variation in pattern and introduces new character V in it.

So we need to take a note of this.

So we store these kind of "changing points and store their corresponding roman numerical characters in a list of tuples with first element as integer and 2nd one as corresponding roman number, for easy lookup.

roman_map = [(100,"C"),(90,"XC"),(50,"L"),(40,"XL"),(10,"X"),(9,"IX"),(5,"V"),(4,"IV"),(1,"I")]

Note: since our question specifies till 100, we're only going to analyse till 100.

Now as we got our guiding map lets process all numbers in the numbers list by looping

inside loop we prints the "number =>" with end character as a specifier.If didn't specify this it'll lead to printing a newline.

now in the inner loop what we going to do is we iterate through our roman_map's elements.

We can get number of times the roman character should be printed by dividing our number with the changing point integer.

Since this division is integer division (// operator), if the number is greater than our number it will produce zero and no character would be printed.

Since in Python "string" * integer will print the "string" number of times as specified in the multiplying number,we'll also use that trick to print our roman numeral characters as many times we need to print.

print(x[1]*count,end="")

x[1] is our roman numerals and count is what we calculated.

now we subtract this count times changing point as we might have somemore integers left and we don't need to process the already processed part.

and same process is continued.

Screenshots and Output

Add a comment
Know the answer?
Add Answer to:
Write a Python program (question2.py) that reads from a file called “input.txt” numbers in [1,39] separated...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (IN C LANGUAGE) 4.33 (Roman-Numeral Equivalent of Decimal Values) Write a program that prints a table...

    (IN C LANGUAGE) 4.33 (Roman-Numeral Equivalent of Decimal Values) Write a program that prints a table of all the Roman-numeral equivalents of the decimal numbers in the range 1 to 100. Decimal→→Roman↵ -------→→-----↵ 1→→I↵ 2→→II↵ 3→→III↵ 4→→IV↵ 5→→V↵ 6→→VI↵ 7→→VII↵ 8→→VIII↵ 9→→IX↵ 10→→X↵ 11→→XI↵ 12→→XII↵ 13→→XIII↵ 14→→XIV↵ 15→→XV↵ 16→→XVI↵ 17→→XVII↵ 18→→XVIII↵ 19→→XIX↵ 20→→XX↵ 21→→XXI↵ 22→→XXII↵ 23→→XXIII↵ 24→→XXIV↵ 25→→XXV↵ 26→→XXVI↵ 27→→XXVII↵ 28→→XXVIII↵ 29→→XXIX↵ 30→→XXX↵ 31→→XXXI↵ 32→→XXXII↵ 33→→XXXIII↵ 34→→XXXIV↵ 35→→XXXV↵ 36→→XXXVI↵ 37→→XXXVII↵ 38→→XXXVIII↵ 39→→XXXIX↵ 40→→XL↵ 41→→XLI↵ 42→→XLII↵ 43→→XLIII↵ 44→→XLIV↵ 45→→XLV↵ 46→→XLVI↵ 47→→XLVII↵...

  • i. What is the difference between sample and population? ii. What is the difference between statistic...

    i. What is the difference between sample and population? ii. What is the difference between statistic and parameter? iii. What is the difference between descriptive statistics and statistical inference? iv. Categorical random variable contrast with numerical random variable. v. Compare discrete data from continuous data. saw. Detail the difference between nominal and ordinal scale. vii. Detail the difference between interval and ratio scale. viii. Explain the main reasons for obtaining data. ix. What is the difference between probabilistic and non-probabilistic...

  • for future views please dont use it for spring 2020 Write a Perl program to accomplish...

    for future views please dont use it for spring 2020 Write a Perl program to accomplish each of the following on the file solar.txt (see link at the class homepage) 1. Print all records that do not list a discoverer in the eighth field. 2. Print every record after erasing the second field. Note: It would be better to say "print every record" omitting the second field . 3. Print the records for satellites that have negative orbital periods. (A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT