annotate Doc/tools/roman.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 6d13e18f706a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42629
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Convert to and from Roman numerals"""
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 __author__ = "Mark Pilgrim (f8dy@diveintopython.org)"
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 __version__ = "1.4"
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 __date__ = "8 August 2001"
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 __copyright__ = """Copyright (c) 2001 Mark Pilgrim
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 This program is part of "Dive Into Python", a free Python tutorial for
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 experienced programmers. Visit http://diveintopython.org/ for the
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 latest version.
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 This program is free software; you can redistribute it and/or modify
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 it under the terms of the Python 2.1.1 license, available at
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 http://www.python.org/2.1.1/license.html
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 """
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 import re
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #Define exceptions
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 class RomanError(Exception): pass
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 class OutOfRangeError(RomanError): pass
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 class NotIntegerError(RomanError): pass
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 class InvalidRomanNumeralError(RomanError): pass
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 #Define digit mapping
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 romanNumeralMap = (('M', 1000),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 ('CM', 900),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 ('D', 500),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 ('CD', 400),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ('C', 100),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 ('XC', 90),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 ('L', 50),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 ('XL', 40),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 ('X', 10),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 ('IX', 9),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 ('V', 5),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 ('IV', 4),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 ('I', 1))
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 def toRoman(n):
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 """convert integer to Roman numeral"""
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 if not (0 < n < 5000):
42827
6d13e18f706a Cosmetic fixes to make this work with Py3k (as well as with 2.5 still).
Guido van Rossum <guido@python.org>
parents: 42629
diff changeset
43 raise OutOfRangeError("number out of range (must be 1..4999)")
6d13e18f706a Cosmetic fixes to make this work with Py3k (as well as with 2.5 still).
Guido van Rossum <guido@python.org>
parents: 42629
diff changeset
44 if int(n) != n:
6d13e18f706a Cosmetic fixes to make this work with Py3k (as well as with 2.5 still).
Guido van Rossum <guido@python.org>
parents: 42629
diff changeset
45 raise NotIntegerError("decimals can not be converted")
42629
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 result = ""
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 for numeral, integer in romanNumeralMap:
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 while n >= integer:
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 result += numeral
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 n -= integer
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 return result
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 #Define pattern to detect valid Roman numerals
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 romanNumeralPattern = re.compile("""
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 ^ # beginning of string
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 M{0,4} # thousands - 0 to 4 M's
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 (CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # or 500-800 (D, followed by 0 to 3 C's)
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 (XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # or 50-80 (L, followed by 0 to 3 X's)
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 (IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # or 5-8 (V, followed by 0 to 3 I's)
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 $ # end of string
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 """ ,re.VERBOSE)
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 def fromRoman(s):
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 """convert Roman numeral to integer"""
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if not s:
42827
6d13e18f706a Cosmetic fixes to make this work with Py3k (as well as with 2.5 still).
Guido van Rossum <guido@python.org>
parents: 42629
diff changeset
70 raise InvalidRomanNumeralError('Input can not be blank')
42629
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if not romanNumeralPattern.search(s):
42827
6d13e18f706a Cosmetic fixes to make this work with Py3k (as well as with 2.5 still).
Guido van Rossum <guido@python.org>
parents: 42629
diff changeset
72 raise InvalidRomanNumeralError('Invalid Roman numeral: %s' % s)
42629
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 result = 0
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 index = 0
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 for numeral, integer in romanNumeralMap:
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 while s[index:index+len(numeral)] == numeral:
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 result += integer
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 index += len(numeral)
a6c48925de53 First merge from the trunk straight into the py3k branch. I'm not
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return result