Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simplified solution in Clear category for English to Braille Translator by CDG.Axel
CODES = [1, 3, 9, 25, 17, 11, 27, 19, 10, 26, 5, 7, 13, 29, 21, 15, 31, 23,
14, 30, 37, 39, 62, 45, 61, 53, 2, 18, 38, 22, 50, 36, 32, 0, 60]
LETTERS = {e: CODES[i] for i, e in enumerate('abcdefghijklmnopqrstuvwxyz,-?!._^ #')}
def convert(code):
code = f'{LETTERS[code]:06b}'[::-1]
return [code[j] + code[j + 3] for j in range(3)]
def braille_page(text: str):
"""
1. transform text to line of symbols
- add # + letter for digits
- add ^ + letter for capitals
2. align line if needed, init page variable
3. transform to int matrix taking by 10 symbols
4. add empty line if text isn't run out
"""
text = ''.join('#' + chr(97 + (ord(c) - 49) % 10) if c.isdigit() else
'^' + c.lower() if c.isupper() else c for c in text)
page, text = [], text + ' ' * (9 - (len(text) - 1) % 10) * (len(text) > 10)
while text:
page.extend('0'.join(convert(el)[row] for el in text[:10]) for row in [0, 1, 2])
_, text = page.extend(['0' * 29] * (len(text) > 10)), text[10:]
return [map(int, el) for el in page]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def checker(func, text, answer):
result = func(text)
return answer == tuple(tuple(row) for row in result)
assert checker(braille_page, "hello 1st World!", (
(1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1),
(1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1),
(0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0))
), "Example"
assert checker(braille_page, "42", (
(0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0),
(0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0),
(1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0))), "42"
assert checker(braille_page, "CODE", (
(0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1),
(0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0))
), "CODE"
print('Completed!')
Oct. 7, 2021
Comments: