Convert roman numerals to numbers python3/18/2024 ![]() ![]() With that done, we don't need a regexp, nor do we need to test by calling int_to_roman(result) as my first version did. It's based on Mark's, except that I use an additional field in each tuple to enforce the maximum allowed repetitions of each numeral, and I rely on the ordering of the tuple to enforce the correct ordering of numerals. That's fine, and makes the function nicely short but it puts a lot of the logic in the regexp, which I feel is more likely to be misread than is a slightly longer function. However, Mark relies on a regexp to validate the input. His roman-to-int converter is much simpler than the one I posted above. Good stuff, and all free under the Python license. Result = result + nums + numsĪ better roman_to_int. # or we'll get stuff like 'VIX' when we want 'IX'. # We'll also need to delete the previous character, # otherwise we'd get 'IV' when we want 'IX'. # 'the next' character is 2 away from here, not 1 # 4 repetitions means we're trying to represent 4 or 9 Raise ValueError, "Argument must be between " Raise TypeError, "expected integer, got %s" % type(input) It turns out to be easier to just forcibly assign values to "IV" and its friends than it is to implement the rule that determines the values. I rejected this version because it actually ends up being longer and more complex than the version given above. In this version, my approach was simply to follow, as closely as I could, the plain english description of the rules given above. The following is my first attempt at int_to_roman. A numeral used for subtraction in this way must be the largest power of 10 that is less than the numeral it precedes. Thus, IIII is invalid and would be written as IV (one less than five) XC represents 90 (ten less than 100), and XL represents 40 (ten less than 50). Five can be represented with a single larger numeral to represent four, use the next larger numeral, but precede it with a numeral to subtract from it. Thus, VV is invalid 5 + 5 would be better expressed as X.ĭ) No more than three repetitions of a numeral can be used. Numbers greater than 3999 are not represented.ī) Roman numerals are repeated to add value: III is equivalent to 1 +1 +1 = 3.Ĭ) Only powers of 10 may be repeated in this way. I use two tuples for the mapping, instead of a dictionary, because I need to go through them sequentially and don't care about random access therefore a dictionary would be more hindrance than help.Ī) I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. I just establish a mapping between integer values and roman numerals, then count how many of each value can fit into the input integer. This is the easiest to read that I've been able to come up with. There are probably many algorithms to create roman numerals. if int_to_roman ( sum ) = input : return sum else : raise ValueError, 'input is not a valid roman numeral: %s ' % input append ( value ) sum = 0 for n in places : sum += n # Easiest test for validity. try : nextvalue = ints )] if nextvalue > value : value *= - 1 except Inde圎rror : # there is no next place. upper () nums = ints = places = for c in input : if not c in nums : raise ValueError, "input is not a valid roman numeral: %s " % input for i in range ( len ( input )): c = input value = ints # If the next place holds a larger number, this value is negative. ValueError: input is not a valid roman numeral: IL """ if type ( input ) != type ( "" ): raise TypeError, "expected string, got %s " % type ( input ) input = input. ![]() ValueError: input is not a valid roman numeral: A > roman_to_int('IL') Traceback (most recent call last). TypeError: expected string, got > roman_to_int('a') Traceback (most recent call last). ValueError: input is not a valid roman numeral: VVVIV > roman_to_int(1) Traceback (most recent call last). I II III IV V VI VII VIII IX X XI XII XIII XIV XV XVI XVII XVIII XIX XX > print int_to_roman(2000) MM > print int_to_roman(1999) MCMXCIX """ if type ( input ) != type ( 1 ): raise TypeError, "expected integer, got %s " % type ( input ) if not 0 > r = range(1, 4000) > nums = > ints = > print r = ints 1 > roman_to_int('VVVIV') Traceback (most recent call last). ![]() Examples: > int_to_roman(0) Traceback (most recent call last): ValueError: Argument must be between > int_to_roman(-1) Traceback (most recent call last): ValueError: Argument must be between > int_to_roman(1.5) Traceback (most recent call last): TypeError: expected integer, got > for i in range(1, 21): print int_to_roman(i). Def int_to_roman ( input ): """ Convert an integer to Roman numerals. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |