Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Created October 14, 2014 11:41
Show Gist options
  • Save juancarlospaco/8952649e9b55b1b90690 to your computer and use it in GitHub Desktop.
Save juancarlospaco/8952649e9b55b1b90690 to your computer and use it in GitHub Desktop.
spoken and roman numbers for python 3
# -*- coding: utf-8 -*-
def int_to_roman(integer_argument):
"""Convert an integer to Roman numerals.
>>> int_to_roman(0)
Traceback (most recent call last):
ValueError: Argument must be between 1 and 3999
>>> int_to_roman(-1)
Traceback (most recent call last):
ValueError: Argument must be between 1 and 3999
>>> int_to_roman(1.5)
Traceback (most recent call last):
TypeError: expected integer, got <type 'float'>
>>> for i in range(1, 5): print int_to_roman(i)
...
I
II
III
IV
V
>>> print int_to_roman(2000)
MM
>>> print int_to_roman(1999)
MCMXCIX"""
if not isinstance(integer_argument, int):
raise TypeError("Expected integer,got{}".format(type(integer_argument)))
if not 0 < integer_argument < 4000:
raise ValueError("Argument must be between 1 and 3999")
ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
nums = ('M', 'CM', 'D', 'CD', 'C', 'XC',
'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
result = ""
for i in range(len(ints)):
count = int(integer_argument / ints[i])
result += nums[i] * count
integer_argument -= ints[i] * count
return result
###############################################################################
_known = {
0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six',
7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve',
13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen',
17: 'seventeen', 18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty',
40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty',
90: 'ninety'}
def _positive_spoken_number(integer_argument):
"""Assume integer_argument is a positive integer.
>>> _positive_spoken_number(900)
'nine hundred'
>>> _positive_spoken_number(100)
'one hundred'
>>> _positive_spoken_number(100000000000)
'one hundred billion'
>>> _positive_spoken_number(1000000000000)
'one trillion'
>>> _positive_spoken_number(33000000000000)
'thirty-three trillion'"""
if not isinstance(integer_argument, int) and integer_argument >= 0:
raise TypeError("Expected integer,got{}".format(type(integer_argument)))
if integer_argument in _known:
return _known[integer_argument]
bestguess, remainder = str(integer_argument), 0
h, T, M, B, T = ' hundred', ' thousand', ' million', ' billion', ' trillion'
if integer_argument < 100:
bestguess = str(_positive_spoken_number((integer_argument // 10) * 10) +
'-' + _positive_spoken_number(integer_argument % 10))
return bestguess
elif integer_argument < 1000:
bestguess = _positive_spoken_number(integer_argument // 100) + h
remainder = integer_argument % 100
elif integer_argument < 1000000:
bestguess = _positive_spoken_number(integer_argument // 1000) + T
remainder = integer_argument % 1000
elif integer_argument < 1000000000:
bestguess = _positive_spoken_number(integer_argument // 1000000) + M
remainder = integer_argument % 1000000
elif integer_argument < 1000000000000:
bestguess = _positive_spoken_number(integer_argument // 1000000000) + B
remainder = integer_argument % 1000000000
else:
bestguess = str(
_positive_spoken_number(integer_argument // 1000000000000) + T)
remainder = integer_argument % 1000000000000
if remainder:
comma = ', ' if remainder >= 100 else ''
bestguess = bestguess + comma + ' ' + _positive_spoken_number(remainder)
return bestguess
def spoken_number(integer_argument):
"""Return integer_argument as it would be spoken else str(integer_argument).
>>> spoken_number(0)
'zero'
>>> spoken_number(-2)
'minus two'
>>> spoken_number(42)
'forty-two'"""
if not isinstance(integer_argument, int):
raise TypeError("Expected integer,got{}".format(type(integer_argument)))
if integer_argument < 0:
if integer_argument in _known:
spoken_number_string = _known[integer_argument]
else:
spoken_number_string = 'minus '
spoken_number_string += _positive_spoken_number(-integer_argument)
else:
spoken_number_string = _positive_spoken_number(integer_argument)
return spoken_number_string
###############################################################################
if __name__ == '__main__':
print((spoken_number(101).replace(' ', '-')))
print((int_to_roman(23)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment