Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Roman Numeral Pedantry

From: "Fredrik Lundh" <fred...@pythonware.com>
Subject: Re: Roman Numeral Pedantry
Date: 1998/03/09
Message-ID: <01bd4b49$a44c46c0$f29b12c2@panik.pythonware.com>#1/1
X-Deja-AN: 332208719
Sender: dae...@cwi.nl (0000-Admin(0000))
X-Priority: 3
X-Msmail-Priority: Normal
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_NextPart_000_000D_01BD4B52.0610AEC0"
X-Mimeole: Produced By Microsoft MimeOLE V4.71.1712.3
Organization: CWI, Amsterdam
Newsgroups: comp.lang.python


Det här är ett multipart-meddelande i MIME-format.

------=_NextPart_000_000D_01BD4B52.0610AEC0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

>Back to Python: is there a "roman numerals" module anywhere?


Here's one (which seems to agree with your definition, btw).

>>> print roman.roman(1998)
MCMXCVIII

Cheers /F
fred...@pythonware.com
http://www.pythonware.com


------=_NextPart_000_000D_01BD4B52.0610AEC0
Content-Type: application/octet-stream;
	name="roman.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="roman.py"

#
# roman numbers
#
# fredrik lundh, november 1996 (based on a C hack from 1984)
#
# fred...@pythonware.com
# http://www.pythonware.com
#

TEMPLATES = [ 0, 01, 011, 0111, 012, 02, 021, 0211, 02111, 013 ]
LETTERS = "IVXLCDM"

def roman(value):
    if value < 0 or value > 3999:
	raise ValueError, "illegal value"
    str = ""
    base = -1
    while value:
	value, index = divmod(value, 10)
	tmp = TEMPLATES[index]
	while tmp:
	    tmp, index = divmod(tmp, 8)
	    str = LETTERS[index+base] + str
	base = base + 2
    return str

print "print roman(1998)"
print roman(1998)

------=_NextPart_000_000D_01BD4B52.0610AEC0--