Python Forum
Arabic to Roman numerals conversion(and backwards)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arabic to Roman numerals conversion(and backwards)
#1
I have been thinking on how to make a script to convert roman numerals to arabic and backwards, and after a whole day of optimizing and compressing the code, i came up with this:
V={'_M': 10**6, '_C_M': 900000, '_D': 500000, '_C_D': 400000, '_C': 10**5, '_X_C': 90000, '_L': 50000, '_X_L': 40000, '_X': 10**4, '_I_X': 9000, '_V': 5000, '_I_V': 4000, 
'M': 1000, 'CM': 900, 'D': 500, 'CD': 400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1} # Roman numerals/pairs to arabic

def ro(a): # Function to convert arabic to roman numerals
    return ''.join(k for k in V for _ in iter(lambda: a >= V[k], False) if (a := a - V[k]) >= 0) 
# for every key(k)(letter/pair) in values(V), try to subtract the value associated with that letter/pair from the input(arabic number) and if the end result is 0 or more, append the letter/pair. for example, if we have 900 as input, it stops at 'CM' and appends it and since 900 - 900 is 0, it should stop iterating

while True:
    r = 3999999 # range
    n = input(f"Enter Rom/Arab number (1-{r}) or 'q': ").upper() # input - arabic or roman number or quit
    if n == "Q":
        break
    else:
        #there used to be a separate function to convert roman to arabic but i decided it was unnecessary and it took extra lines of space, now it's all here:
        a = i = 0 # set arabic number and index to 0
        while i < len(n):
            p = i+1<len(n) and V.get(n[i:i+2]) # check if a pair of index and index + 1 letters exists in the values dictionary
            a += V[n[i:i+2]] if p else V.get(n[i], 0) # if the pair exists, add the pair value, else add the single number value
            i += 2 if p else 1 # jump forward 2 steps if a pair is decoded, else jump 1 step if a single letter is decoded
        print(f"{ro(int(n))}" if n.isdigit() and 1 <= int(n) <= r else f"{a}" if ro(a) == n and 1 <= a <= r else "OUT OF RANGE") 
# prints out the result. if user_input is an integer (arabic number) and is in range, prints the result of conversion to roman numerals. otherwise if user_input is not an integer(is a roman numeral), tries to decode it into an arabic number, then encode it again and if the encoded value == original value then the number is valid and it prints it, else input is invalid
i was wondering if there is a way to compress it even further? i couldnt find a way to do that
Reply
#2
you can take a look at other's code here.
Reply
#3
i dont want a library, i want to make it myself, but im wondering if what i already made can be improved at all
Reply
#4
Go to the package home page or repository and look at the source code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 3,177 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Converting number to roman numerals jagasrik 6 5,453 Aug-11-2020, 03:47 AM
Last Post: voidptr
  Roman Numeral Ordered List? (first post) CopperyMarrow15 1 2,334 Nov-06-2019, 11:06 PM
Last Post: Larz60+
  search a string backwards Skaperen 2 2,905 Dec-30-2018, 04:32 AM
Last Post: Skaperen
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 11,316 Aug-06-2018, 05:19 PM
Last Post: vanicci
  Arabic language is backward in python charts bandar 3 4,897 Feb-23-2018, 07:10 AM
Last Post: Larz60+
  Backwards strings Wolfpack2605 2 3,555 Dec-30-2017, 12:39 AM
Last Post: hshivaraj
  Backwards compatable ? louwho 3 4,015 Aug-26-2017, 01:05 PM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020