Spaces:
Running
Running
class TifinaghNumberConverter: | |
AND = " ⴷ " | |
UNITS = ["ⴰⵎⵢⴰ", "ⵢⴰⵏ", "ⵙⵉⵏ", "ⴽⵕⴰⴹ", "ⴽⴽⵓⵥ", "ⵙⵎⵎⵓⵙ", "ⵚⴹⵉⵚ", "ⵙⴰ", "ⵜⴰⵎ", "ⵜⵥⴰ"] | |
TENS = ["", "ⵎⵔⴰⵡ", "ⵙⵉⵎⵔⴰⵡ", "ⴽⵕⴰⵎⵔⴰⵡ", "ⴽⴽⵓⵎⵔⴰⵡ", "ⵙⵎⵎⵓⵎⵔⴰⵡ", "ⵚⴹⵉⵎⵔⴰⵡ", "ⵙⴰⵎⵔⴰⵡ", "ⵜⴰⵎⵔⴰⵡ", "ⵜⵥⴰⵎⵔⴰⵡ"] | |
HUNDREDS = ["", "ⵜⵉⵎⵉⴹⵉ", "ⵙⵏⴰⵜ ⵜⵎⴰⴹ", "ⴽⵕⴰⴹⵜ ⵜⵎⴰⴹ", "ⴽⴽⵓⵥⵜ ⵜⵎⴰⴹ", "ⵙⵎⵎⵓⵙⵜ ⵜⵎⴰⴹ", "ⵚⴹⵉⵚⵜ ⵜⵎⴰⴹ", "ⵙⴰⵜ ⵜⵎⴰⴹ", "ⵜⴰⵎⵜ ⵜⵎⴰⴹ", "ⵜⵥⴰⵜ ⵜⵎⴰⴹ"] | |
ORDERS = ["", "ⵉⴼⴹ", "ⴰⵎⵍⵢⵓⵏ", "ⴰⵎⵍⵢⴰⵕ", "ⴰⵜⵔⵉⵍⵢⵓⵏ", "ⴰⴽⵡⴰⴹⵕⵉⵍⵢⵓⵏ", "ⴰⴽⵡⵉⵏⵜⵔⵉⵍⵢⵓⵏ", "ⴰⵙⵉⴽⵙⵜⵉⵍⵢⵓⵏ", "ⴰⵙⵉⴱⵜⵉⵍⵢⵓⵏ", "ⴰⵡⴽⵜⵉⵍⵢⵓⵏ", "ⴰⵏⵓⵏⵉⵍⵢⵓⵏ"] | |
ORDERS_PLURAL = ["", "ⵡⴰⴼⴹⴰⵏ", "ⵉⴷ ⴰⵎⵍⵢⵓⵏ", "ⵉⴷ ⴰⵎⵍⵢⴰⵕ", "ⵉⵜⵔⵉⵍⵢⵓⵏⵏ", "ⵉⴽⵡⴰⴹⵕⵉⵍⵢⵓⵏⵏ", "ⵉⴽⵡⵉⵏⵜⵔⵉⵍⵢⵓⵏⵏ", "ⵉⵙⵉⴽⵙⵜⵉⵍⵢⵓⵏⵏ", "ⵉⵙⵉⴱⵜⵉⵍⵢⵓⵏⵏ", "ⵉⵡⴽⵜⵉⵍⵢⵓⵏⵏ", "ⵉⵏⵓⵏⵉⵍⵢⵓⵏⵏ"] | |
def convert_number_to_999(cls, n): | |
"""Convert numbers from 0 to 999 to Tifinagh.""" | |
if n == 0: | |
return "" | |
if n < 10: | |
return cls.UNITS[n] | |
if n < 20: | |
if n % 10 == 0: | |
return cls.TENS[n // 10] | |
return cls.UNITS[n % 10] + cls.AND + cls.TENS[n // 10] | |
if n < 100: | |
if n % 10 == 0: | |
return cls.TENS[n // 10] | |
return cls.TENS[n // 10] + cls.AND + cls.UNITS[n % 10] | |
if n < 1000: | |
if n % 100 == 0: | |
return cls.HUNDREDS[n // 100] | |
return cls.HUNDREDS[n // 100] + cls.AND + cls.convert_number_to_999(n % 100) | |
return "" | |
def convert_large_number(cls, n): | |
"""Convert large numbers to Tifinagh.""" | |
if n < 1000: | |
return cls.convert_number_to_999(n) | |
for order in range(len(cls.ORDERS) - 1, 0, -1): | |
order_value = 10 ** (order * 3) | |
if n >= order_value: | |
quotient = n // order_value | |
remainder = n % order_value | |
if quotient == 1: | |
if remainder: | |
return cls.ORDERS[order] + cls.AND + cls.convert_large_number(remainder) | |
return cls.ORDERS[order] | |
else: | |
if remainder: | |
return cls.convert_number_to_999(quotient) + " " + \ | |
cls.ORDERS_PLURAL[order] + cls.AND + cls.convert_large_number(remainder) | |
return cls.convert_number_to_999(quotient) + " " + cls.ORDERS_PLURAL[order] | |
return "" | |
def convert(cls, number): | |
"""Main conversion method.""" | |
if number == 0: | |
return "ⴰⵎⵢⴰ" | |
if number < 0: | |
return "- " + cls.convert_large_number(abs(number)) | |
return cls.convert_large_number(number) | |