Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for English to Braille Translator by mortonfox
import string
from functools import reduce
from copy import deepcopy
def convert(code):
bin_code = bin(code)[2:].zfill(6)[::-1]
return [[int(bin_code[j + i * 3]) for i in range(2)] for j in range(3)]
LETTERS_NUMBERS = list(map(convert,
[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, 47, 63, 55, 46, 26]))
CAPITAL_FORMAT = convert(32)
NUMBER_FORMAT = convert(60)
PUNCTUATION = {",": convert(2), "-": convert(18), "?": convert(38),
"!": convert(22), ".": convert(50), "_": convert(36)}
WHITESPACE = convert(0)
def join_horiz(blocks):
return reduce(lambda x, y: [x[i] + [0] + y[i] for i in range(len(x))], blocks)
def join_vert(rows):
return reduce(lambda x, y: x + [[0] * len(x[0])] + y, rows)
def braille_page(text: str):
braille_blocks = []
for c in text:
if c in string.ascii_letters:
if c in string.ascii_uppercase:
braille_blocks.append(CAPITAL_FORMAT)
c = c.lower()
braille_blocks.append(LETTERS_NUMBERS[ord(c) - ord('a')])
elif c in string.digits:
braille_blocks.append(NUMBER_FORMAT)
braille_blocks.append(LETTERS_NUMBERS[(int(c) - 1) % 10])
elif c == ' ':
braille_blocks.append(WHITESPACE)
elif c in PUNCTUATION.keys():
braille_blocks.append(PUNCTUATION[c])
# Pad out last row if more than one row.
if len(braille_blocks) > 10:
braille_blocks += [WHITESPACE] * ((10 - len(braille_blocks) % 10) % 10)
return join_vert(join_horiz(braille_blocks[i : i + 10]) for i in range(0, len(braille_blocks), 10))
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"
Nov. 7, 2017
Comments: