Weblog of an Aspiring Computer Scientist
Random header image... Refresh for more!

Roman Numerals - More Python

Since I love Python and since 90 % of those online judges won’t allow me to use Python to submit my solutions, I have to contend with writing the script and hoping it is right. I am now solving a few ICPC sums from last year and I absolutely love them. I first picked the North America, East/Central Problem Set and found this question on roman numerals. Basically subtracting two supplied roman numbers. Guess what this reminded me of, Claude Shannon of course ! Shannon designed a box that used only roman numbers. All hail God Shannon - the best the world had to offer. Well, I hacked up a simple solution in a few seconds. It looks horrible but who cares.

#!/usr/bin/env python
#Author: Shriphani Palakodety
#Roman Numerals Problem.
#Solution to:
#ICPC North America East-Central Problem1

#Roman to Decimal dictionary

rom2dec_dict = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}

def romToDec(roman_string):
    ‘Convert from Roman to Decimal’
    #Move from right to left.
    number_dec = 0
    i = len(roman_string) - 1
    if i % 2 != 0:
        while i > 0:
            if rom2dec_dict[roman_string[i]] > rom2dec_dict[roman_string[i-1]]:
                number_dec += rom2dec_dict[roman_string[i]] - rom2dec_dict[roman_string[i-1]]
                i = i - 2    
                continue
            else:
                number_dec += rom2dec_dict[roman_string[i]] + rom2dec_dict[roman_string[i-1]]
                i = i - 2
                continue
    else:
        while i > 1:
            if rom2dec_dict[roman_string[i]] > rom2dec_dict[roman_string[i-1]]:
                number_dec +- rom2dec_dict[roman_string[i]] - rom2dec_dict[roman_string[i-1]]
                i = i - 2
                continue
            else:
                number_dec += rom2dec_dict[roman_string[i]] + rom2dec_dict[roman_string[i-1]]
                i = i - 2
                continue
        number_dec += rom2dec_dict[roman_string[0]]
    return number_dec

 

There, first ICPC problem. I would love to crunch this set in this week. Some problems seem tough and I really would love to solve them all. I found another interesting problem on circular combination locks. I am still thinking about that one and I hope I can solve it tomorrow.

Well, I am just loving the freedom. Happy Coding! :)

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment