Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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) )
|
||||
14
Task/Roman-numerals-Decode/Python/roman-numerals-decode-2.py
Normal file
14
Task/Roman-numerals-Decode/Python/roman-numerals-decode-2.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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)))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
numerals = { 'M' : 1000, 'D' : 500, 'C' : 100, 'L' : 50, 'X' : 10, 'V' : 5, 'I' : 1 }
|
||||
def romannumeral2number(s):
|
||||
return reduce(lambda x, y: -x + y if x < y else x + y, map(lambda x: numerals.get(x, 0), s.upper()))
|
||||
111
Task/Roman-numerals-Decode/Python/roman-numerals-decode-4.py
Normal file
111
Task/Roman-numerals-Decode/Python/roman-numerals-decode-4.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
'''Roman numerals decoded'''
|
||||
|
||||
from operator import mul
|
||||
from functools import reduce
|
||||
from collections import defaultdict
|
||||
from itertools import accumulate, chain, cycle
|
||||
|
||||
|
||||
# intFromRoman :: String -> Maybe Int
|
||||
def intFromRoman(s):
|
||||
'''Just the integer represented by a Roman
|
||||
numeral string, or Nothing if any
|
||||
characters are unrecognised.
|
||||
'''
|
||||
dct = defaultdict(
|
||||
lambda: None,
|
||||
zip(
|
||||
'IVXLCDM',
|
||||
accumulate(chain([1], cycle([5, 2])), mul)
|
||||
)
|
||||
)
|
||||
|
||||
def go(mb, x):
|
||||
'''Just a letter value added to or
|
||||
subtracted from a total, or Nothing
|
||||
if no letter value is defined.
|
||||
'''
|
||||
if None in (mb, x):
|
||||
return None
|
||||
else:
|
||||
r, total = mb
|
||||
return x, total + (-x if x < r else x)
|
||||
|
||||
return bindMay(reduce(
|
||||
go,
|
||||
[dct[k.upper()] for k in reversed(list(s))],
|
||||
(0, 0)
|
||||
))(snd)
|
||||
|
||||
|
||||
# ------------------------- TEST -------------------------
|
||||
def main():
|
||||
'''Testing a sample of dates.'''
|
||||
|
||||
print(
|
||||
fTable(__doc__ + ':\n')(str)(
|
||||
maybe('(Contains unknown character)')(str)
|
||||
)(
|
||||
intFromRoman
|
||||
)([
|
||||
"MDCLXVI", "MCMXC", "MMVIII",
|
||||
"MMXVI", "MMXVIII", "MMZZIII"
|
||||
])
|
||||
)
|
||||
|
||||
|
||||
# ----------------------- GENERIC ------------------------
|
||||
|
||||
# bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
|
||||
def bindMay(m):
|
||||
'''Injection operator for the Maybe monad.
|
||||
If m is Nothing, it is passed straight through.
|
||||
If m is Just(x), the result is an application
|
||||
of the (a -> Maybe b) function (mf) to x.'''
|
||||
return lambda mf: (
|
||||
m if None is m else mf(m)
|
||||
)
|
||||
|
||||
|
||||
# maybe :: b -> (a -> b) -> Maybe a -> b
|
||||
def maybe(v):
|
||||
'''Either the default value v, if m is Nothing,
|
||||
or the application of f to x,
|
||||
where m is Just(x).
|
||||
'''
|
||||
return lambda f: lambda m: v if None is m else (
|
||||
f(m)
|
||||
)
|
||||
|
||||
|
||||
# snd :: (a, b) -> b
|
||||
def snd(ab):
|
||||
'''Second member of a pair.'''
|
||||
return ab[1]
|
||||
|
||||
|
||||
# ---------------------- FORMATTING ----------------------
|
||||
|
||||
# fTable :: String -> (a -> String) ->
|
||||
# (b -> String) -> (a -> b) -> [a] -> String
|
||||
def fTable(s):
|
||||
'''Heading -> x display function ->
|
||||
fx display function -> f -> xs -> tabular string.
|
||||
'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
ys = [xShow(x) for x in xs]
|
||||
w = max(map(len, ys))
|
||||
return s + '\n' + '\n'.join(map(
|
||||
lambda x, y: (
|
||||
f'{y.rjust(w, " ")} -> {fxShow(f(x))}'
|
||||
),
|
||||
xs, ys
|
||||
))
|
||||
return lambda xShow: lambda fxShow: lambda f: (
|
||||
lambda xs: go(xShow, fxShow, f, xs)
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue