nam24
nam24

Reputation: 51

Convert Roman Numerals into Word-Form When the String is Multiple Words

I created the below function to convert roman numerals into word-form. It works as intended. If the building/string ends in a roman numeral from I-X, it converts it to the word. There is one hole that I know of (if the roman numeral is not the last word). I can't think of how to implement that. Thoughts?

Also, I'm pretty new to Python so I am curious if there was anyway to clean up the code. It looks bulky to me.

def replace_roman_numerals(string):
if len(string.split()) > 1:     # if the community name has more than one word, split the words, else return community as is
    first, *middle, last = string.split()
else:
    first = ""
    middle = ""
    last = ""
w = string.split()  # splits the community name into words
lw = w[len(w)-1]    # returns the last word in the community
# convert RN into a word if the last word in the community is a RN and remove excessive blanks and characters from the middle variable
if lw in ("I", "1"):         return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " One").split())
elif lw in ("II", "2"):      return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Two").split())
elif lw in ("III", "3"):     return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Three").split())
elif lw in ("IV", "4"):      return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Four").split())
elif lw in ("V", "5"):       return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Five").split())
elif lw in ("VI", "6"):      return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Six").split())
elif lw in ("VII", "7"):     return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Seven").split())
elif lw in ("VIII", "8"):    return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Eight").split())
elif lw in ("IX", "9"):      return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Nine").split())
elif lw in ("X", "10"):       return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " Ten").split())
else:                 return " ".join((first + " " + str(middle)[1:-1].replace("'","").replace(",","") + " " + lw).split())

Inputs:

s = "Park III"
s = replace_roman_numerals(s)
print (s)
s = "American Tower - Bldg IV"
print (replace_roman_numerals(s))
s = "Estancia"
print (replace_roman_numerals(s))
s = "Park Place 9"
print (replace_roman_numerals(s))

Outputs are:

Park Three
American Tower - Bldg Four
Estancia
Park Place Nine

Upvotes: 0

Views: 265

Answers (1)

Georgina Skibinski
Georgina Skibinski

Reputation: 13397

That would be a bit more python-ish approach:

conv_map={"1": "One", "I": "One", "2": "Two", "II": "Two", "3": "Three", "III": "Three", "4": "Four", "IV": "Four", "5": "Five", "V": "Five", "6": "Six", "VI": "Six", "7": "Seven", "VII": "Seven", "8": "Eight", "VIII": "Eight", "9": "Nine", "IX": "Nine", "10": "Ten", "X": "Ten"}

def replace_roman_numerals(inputStr, map_=conv_map):
    w = inputStr.split()
    w[-1]=map_.get(w[-1], w[-1])
    return ' '.join(w)

s = "Park III"
s = replace_roman_numerals(s)
print (s)
s = "American Tower - Bldg IV"
print (replace_roman_numerals(s))
s = "Estancia"
print (replace_roman_numerals(s))
s = "Park Place 9"
# in case you'd need it - you can also pass other conversion map:
print (replace_roman_numerals(s, conv_map))

Outputs:

Park Three
American Tower - Bldg Four
Estancia
Park Place Nine

Upvotes: 1

Related Questions