Jump to content

Mayan numerals: Difference between revisions

→‎{{header|Python}}: Pylinted for Python 3, added {works with} tag
(→‎{{header|Python}}: Pylinted for Python 3, added {works with} tag)
Line 1,907:
 
=={{header|Python}}==
{{Works with|Python|3.7}}
<lang Python>from functools import (reduce)
<lang Python>'''Mayan numerals'''
 
<lang Python>from functools import (reduce)
 
# main :: IO ()
def main():
print (
'\n'.join(mayanFramed(n) for n in [
4005, 8017, 326205, 886205, 1081439556, 1000000, 1000000000
])
)
 
 
# MAYAN NUMBERS -------------------------------------------
 
# MAYAN NUMBERSNUMERALS -------------------------------------------
 
# mayanNumerals :: Int -> [[String]]
def mayanNumerals(n):
'''Rows of Mayan digit cells,
representing the integer n.
'''
return showIntAtBase(20)(
mayanDigit
Line 1,931 ⟶ 1,927:
# mayanDigit :: Int -> [String]
def mayanDigit(n):
'''List of strings representing a Mayan digit.'''
if 0 < n:
r = n % 5
Line 1,940 ⟶ 1,937:
# mayanFramed :: Int -> String
def mayanFramed(n):
'''Mayan integer in the form of a
Wiki table source string.
'''
return 'Mayan ' + str(n) + ':\n\n' + (
wikiTable({
Line 1,954:
'<br>'.join(col) for col in mayanNumerals(n)
]])
)
 
 
# TEST ----------------------------------------------------
 
# main :: IO ()
def main():
'''Mayan numeral representations of various integers'''
print (
main.__doc__ + ':\n\n' +
'\n'.join(mayanFramed(n) for n in [
4005, 8017, 326205, 886205, 1081439556, 1000000, 1000000000
1000000, 1000000000
])
)
 
Line 1,961 ⟶ 1,975:
# wikiTable :: Dict -> [[a]] -> String
def wikiTable(opts):
'''Source text for wiki-table display of rows of cells,
using CSS key-value pairs in the opts dictionary.
'''
def colWidth():
return 'width:' + opts['colwidth'] + '; ' if (
Line 1,990 ⟶ 2,007:
# cssFromDict :: Dict -> String
def cssFromDict(dct):
'''CSS string from a dictinary of key-value pairs'''
return reduce(
lambda a, k: a + k + ':' + dct[k] + '; ', dct.keys(), ''
Line 1,997 ⟶ 2,015:
# showIntAtBase :: Int -> (Int -> String) -> Int -> String -> String
def showIntAtBase(base):
'''String representation of an integer in a given base,
def f(toStr, n, rs):
using a supplied function for the string representation
of digits.
'''
def fwrap(toStrtoChr, n, rs):
def go(nd, r):
n, d = nd
r_ = toStrtoChr(d) + r
return go(divmod(n, base), r_) if 0 != n else r_
return go(divmod(n,'unsupported base),' rs)if 1 >= base else (
'negative number' if 0 > n else (
return lambda toStr: lambda n: lambda rs: (
f go(toStrdivmod(n, nbase), rs))
)
return lambda toStrtoChr: lambda n: lambda rs: (
wrap(toChr, n, rs)
)
 
 
# MAIN ---
if __name__ == '__main__':
main()</lang>
{{Out}}
Mayan numeral representations of various integers:
 
Mayan 4005:
 
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.