Skip to content

Commit

Permalink
IT CALCS LIKE A ROMAN MOMMA!
Browse files Browse the repository at this point in the history
  • Loading branch information
tomviner committed Mar 1, 2012
1 parent 60bae1a commit e83fb50
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions romannums.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re

vals = {
'I': 1,
Expand All @@ -12,9 +13,9 @@
def to_den(rom):
"""
>>> to_den('I')
1
'1'
>>> to_den('XXIV')
24
'24'
"""
tot = 0
last_l = 0
Expand All @@ -25,23 +26,40 @@ def to_den(rom):
else:
tot += val
last_l = val
return tot
return str(tot)

def splitter(s):
prolematic_chars = '()+-/*'
for l in prolematic_chars:
s = s.replace(l, ' %s ' % l)
return s.split()

def attempt_roman(tweet):
"""
>>> attempt_roman('I love you!')
1
>>> attempt_roman('Please tell me the arabic numeral for MMXII')
2012
'Please tell me the arabic numeral for 2012'
>>> attempt_roman('( IV + MM ) / II')
'1002'
>>> attempt_roman('(IV+MM)/II')
'1002'
>>> attempt_roman('this has no roman NUMS')
"""
for word in tweet.split():
if len(word) == 1:
orig = tweet
for word in splitter(tweet):
if tweet == orig and len(word) == 1:
continue
try:
return to_den(word)
tweet = tweet.replace(word, to_den(word))
except KeyError:
pass
#print tweet
if orig != tweet:
#print ''.join(sorted(set(tweet)))
#print ''.join(sorted(set('()1234567890+-/*. ')))
if not set(tweet) - set('()1234567890+-/*. '):
return str(eval(tweet))
return tweet
return None

0 comments on commit e83fb50

Please sign in to comment.