Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
English to Braille Translator solution in Clear category for English to Braille Translator by tssrkt777
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 braille_page(text: str):
from string import ascii_lowercase as al
nums_caps = len([x for x in text if x.isdigit()]) + len([x for x in text if x.isupper()])
ln = (len(text) + nums_caps)
if ln<=10:
rows = 1
cols = ln*3-1
else:
num = 0
if ln%10:
num = 1
rows = ln // 10 + num
cols = 29
m = []
for x in range(rows):
for y in range(3):
m.append([0] * cols)
if x < rows - 1:
m.append([0] * cols)
step = [0, 0]
for x in text:
a = []
if x.isalpha():
if x.isupper():
a.append(CAPITAL_FORMAT)
a.append(LETTERS_NUMBERS[al.index(x.lower())])
elif x.isdigit():
a.append(NUMBER_FORMAT)
a.append(LETTERS_NUMBERS[int(x)-1])
elif x.isspace():
a.append(WHITESPACE)
else:
a.append(PUNCTUATION.get(x))
for n, y in enumerate(a):
for z in range(3):
m[step[0] + z][step[1]] = y[z][0]
m[step[0] + z][step[1]+1] = y[z][1]
if step[1]<(cols-3):
step[1] += 3
else:
step[0] += 4
step[1] = 0
return m
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"
Sept. 21, 2021
Comments: