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