![]() ![]() I again left out exception trapping, but at least now there's a place to put it inline. Return "".join(reversed(all_roman_digits)) "MMMM" = 4000, but thisĭoesn't match the pattern anymore, breaking the assumptions of the algorithm. 9 -> "02" like for 1, plus the next 10's level up (2 + i*2) -> IX, XC, CM.Īt 4000 and above, this will throw an exception.8 -> "1000" add another I/X/C -> VIII, LXXX, DCCC.7 -> "100" add another I/X/C -> VII LXX, DCC.5 -> "1" maps to odd roman numeral digits -> V, L, D.4 -> "01" like for 1, then 1 +2*i maps to V, L, or D -> IV, XL, CD.0 -> "" roman numerals don't show zeros.String of roman numeral digits (up to four of them, each being one of three types 0, 1, or 2) for each different 10's place that a decimal digit can occupy. Of the digits, but order within the digits ("digits"?) is unaffected. This is the target of the generate_all_of_numeral function used by the OP.Ĭoncatenate everything back in reversed order. Numeral digits appropriate to the original 10's place of the decimal digit. Basically, you now have your decimal digit expressed as roman The magic is explained a little later on.Ĭonvert your selected magic digit string into a string of roman numeral ![]() ![]() Stepthroughīut I'll explain the lambda function version here.Ĭonvert a decimal integer to a reversed string of its digits, and enumerateĬonvert each digit c back to an integer (range of 0-9), and use it as an index into a list of magic digit strings. Is given that avoids list comprehension and lambda functions. For people without time for puzzles, a version below The data table is fairlyĬompressed, and no subtraction or division is used.Īdmittedly, in the form given, whatever is gained in brevity is immediately That way, digits can be isolated by list indexing. The approach here goes straight for converting the decimal number to a string. Isolate decimal digits and their place, as OP and many of the examples do. This approach gives an alternative to using arithmetical manipulations to It anchors some corner of the space of "unreadable things you probably don't actually want to do". Here's a lambda function for integer to roman numeral conversion, working up to 3999. Finally, I join all generated answers to produce a string. I use an OrderedDict to make sure that I can iterate "downwards" the list, then I use a recursion of divmod to generate matches. Some numbers will have remainders when you use the modulo function, so you also apply the same logic to the remainder. At every match, you should return the respective character. You check if the given number matches any Roman numeral from the highest to the lowest. One of the best ways to deal with this is using the divmod function. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |