tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
12
Task/Roman-numerals-Decode/Python/roman-numerals-decode-1.py
Normal file
12
Task/Roman-numerals-Decode/Python/roman-numerals-decode-1.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
_rdecode = dict(zip('MDCLXVI', (1000, 500, 100, 50, 10, 5, 1)))
|
||||
|
||||
def decode( roman ):
|
||||
result = 0
|
||||
for r, r1 in zip(roman, roman[1:]):
|
||||
rd, rd1 = _rdecode[r], _rdecode[r1]
|
||||
result += -rd if rd < rd1 else rd
|
||||
return result + _rdecode[roman[-1]]
|
||||
|
||||
if __name__ == '__main__':
|
||||
for r in 'MCMXC MMVIII MDCLXVI'.split():
|
||||
print( r, decode(r) )
|
||||
20
Task/Roman-numerals-Decode/Python/roman-numerals-decode-2.py
Normal file
20
Task/Roman-numerals-Decode/Python/roman-numerals-decode-2.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
roman_values = (('I',1), ('IV',4), ('V',5), ('IX',9),('X',10),('XL',40),('L',50),('XC',90),('C',100),
|
||||
('CD', 400), ('D', 500), ('CM', 900), ('M',1000))
|
||||
|
||||
def roman_value(roman):
|
||||
total=0
|
||||
for symbol,value in reversed(roman_values):
|
||||
while roman.startswith(symbol):
|
||||
total += value
|
||||
roman = roman[len(symbol):]
|
||||
return total
|
||||
|
||||
if __name__=='__main__':
|
||||
for value in "MCMXC", "MMVIII", "MDCLXVI":
|
||||
print('%s = %i' % (value, roman_value(value)))
|
||||
|
||||
""" Output:
|
||||
MCMXC = 1990
|
||||
MMVIII = 2008
|
||||
MDCLXVI = 1666
|
||||
"""
|
||||
Loading…
Add table
Add a link
Reference in a new issue