Python greek numeral converter

Quick code for converting to Greek Numerals. Doesn’t handle Myriads.

# coding: utf8
# Greek Numeral Converter
#
# https://en.wikipedia.org/wiki/Greek_numerals

keraia = 'ʹ'

# Multiply by 10000
myriads = 'Μ'

numbers_to_letters = {
    1: 'Α',
    2: 'Β',
    3: 'Γ',
    4: 'Δ',
    5: 'Ε',
    6: 'ΣΤ',
    7: 'Ζ',
    8: 'Η',
    9: 'Θ',
    10: 'Ι',
    20: 'Κ',
    30: 'Λ',
    40: 'Μ',
    50: 'Ν',
    60: 'Ξ',
    70: 'Ο',
    80: 'Π',
    90: 'Ϟ',
    100: 'Ρ',
    200: 'Σ',
    300: 'Τ',
    400: 'Υ',
    500: 'Φ',
    600: 'Χ',
    700: 'Ψ',
    800: 'Ω',
    900: 'Ϡ',
    1000: '͵Α',
    2000: '͵Β',
    3000: '͵Γ',
    4000: '͵Δ',
    5000: '͵Ε',
    6000: '͵Ϛ',
    7000: '͵Z',
    8000: '͵H',
    9000: '͵Θ',
}


def to_greek_numeral(num):
    if num == 0:
        return ''
    if num in numbers_to_letters:
        return numbers_to_letters[num]
    nums = list(numbers_to_letters.keys()); nums.reverse()
    initial = []
    for n in nums:
        if num - n >= 0:
            initial.append(numbers_to_letters[n])
            num = num - n
    return ''.join(initial) + keraia



def date_string_to_greek_number(datestring):
    parts = datestring.split('/')
    return '/'.join(list(to_greek_numeral(int(p)) for p in parts))


if __name__ == '__main__':
    import unittest


    class GreekNumericsTest(unittest.TestCase):
        def test_numbers(self):
            for i in range(1, 10):
                self.assertEqual(numbers_to_letters[i], to_greek_numeral(i))

            tests = (
                (1983, '͵ΑϠΠΓʹ'),
                (2017, '͵ΒΙΖʹ'),
                (13000, '͵Θ͵Δʹ'),
                (666, 'ΧΞΣΤʹ')
            )

            for n, expected in tests:
                self.assertEqual(expected, to_greek_numeral(n))

    unittest.main()

Published by pgk

Person

%d bloggers like this: