Private GIT
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sick-Beard
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vlbox
Sick-Beard
Commits
f372017f
Commit
f372017f
authored
Aug 24, 2010
by
Nic Wolfe
Browse files
Options
Downloads
Patches
Plain Diff
Add roman numeral library
parent
d93b7649
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/tvnamer/roman.py
+116
-0
116 additions, 0 deletions
lib/tvnamer/roman.py
with
116 additions
and
0 deletions
lib/tvnamer/roman.py
0 → 100644
+
116
−
0
View file @
f372017f
## {{{ http://code.activestate.com/recipes/81611/ (r2)
def
int_to_roman
(
input
):
"""
Convert an integer to Roman numerals.
Examples:
>>>
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
,
21
):
print
int_to_roman
(
i
)
...
I
II
III
IV
V
VI
VII
VIII
IX
X
XI
XII
XIII
XIV
XV
XVI
XVII
XVIII
XIX
XX
>>>
print
int_to_roman
(
2000
)
MM
>>>
print
int_to_roman
(
1999
)
MCMXCIX
"""
if
type
(
input
)
!=
type
(
1
):
raise
TypeError
,
"
expected integer, got %s
"
%
type
(
input
)
if
not
0
<
input
<
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
(
input
/
ints
[
i
])
result
+=
nums
[
i
]
*
count
input
-=
ints
[
i
]
*
count
return
result
def
roman_to_int
(
input
):
"""
Convert a roman numeral to an integer.
>>>
r
=
range
(
1
,
4000
)
>>>
nums
=
[
int_to_roman
(
i
)
for
i
in
r
]
>>>
ints
=
[
roman_to_int
(
n
)
for
n
in
nums
]
>>>
print
r
==
ints
1
>>>
roman_to_int
(
'
VVVIV
'
)
Traceback
(
most
recent
call
last
):
...
ValueError
:
input
is
not
a
valid
roman
numeral
:
VVVIV
>>>
roman_to_int
(
1
)
Traceback
(
most
recent
call
last
):
...
TypeError
:
expected
string
,
got
<
type
'
int
'
>
>>>
roman_to_int
(
'
a
'
)
Traceback
(
most
recent
call
last
):
...
ValueError
:
input
is
not
a
valid
roman
numeral
:
A
>>>
roman_to_int
(
'
IL
'
)
Traceback
(
most
recent
call
last
):
...
ValueError
:
input
is
not
a
valid
roman
numeral
:
IL
"""
if
type
(
input
)
!=
type
(
""
):
raise
TypeError
,
"
expected string, got %s
"
%
type
(
input
)
input
=
input
.
upper
()
nums
=
[
'
M
'
,
'
D
'
,
'
C
'
,
'
L
'
,
'
X
'
,
'
V
'
,
'
I
'
]
ints
=
[
1000
,
500
,
100
,
50
,
10
,
5
,
1
]
places
=
[]
for
c
in
input
:
if
not
c
in
nums
:
raise
ValueError
,
"
input is not a valid roman numeral: %s
"
%
input
for
i
in
range
(
len
(
input
)):
c
=
input
[
i
]
value
=
ints
[
nums
.
index
(
c
)]
# If the next place holds a larger number, this value is negative.
try
:
nextvalue
=
ints
[
nums
.
index
(
input
[
i
+
1
])]
if
nextvalue
>
value
:
value
*=
-
1
except
IndexError
:
# there is no next place.
pass
places
.
append
(
value
)
sum
=
0
for
n
in
places
:
sum
+=
n
# Easiest test for validity...
if
int_to_roman
(
sum
)
==
input
:
return
sum
else
:
raise
ValueError
,
'
input is not a valid roman numeral: %s
'
%
input
## end of http://code.activestate.com/recipes/81611/ }}}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment