Created
July 3, 2014 12:25
-
-
Save azhai/7037d0d8fe1ee810e8ce to your computer and use it in GitHub Desktop.
判断是否合法的罗马数字
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-*- coding:utf-8 -*- | |
| """ | |
| VIII 8 | |
| XDIX 99 | |
| """ | |
| numerals = 'IVXLCDM' | |
| numdict = dict(zip(numerals, range(len(numerals)))) | |
| def is_roman_number(number): | |
| m = '' | |
| repeat = 0 | |
| discard = False | |
| ridge = 6 | |
| for n in number: | |
| if n not in numerals: | |
| return False | |
| if m==n: | |
| repeat += 1 | |
| if repeat > 2: | |
| return False | |
| elif numdict[n] % 2 == 1: | |
| return False | |
| else: | |
| repeat = 0 | |
| if m: | |
| near = numdict[n] - numdict[m] | |
| if near > 2: | |
| return False | |
| elif near > 0: | |
| if numdict[n] > ridge: | |
| return False | |
| else: | |
| ridge = numdict[n] | |
| m = n | |
| return True | |
| if __name__ == '__main__': | |
| number = raw_input() | |
| print is_roman_number(number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment