Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for English to Braille Translator by U.V
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)]
CAPITAL = 32
NUMBER = 60
ld = {
'a': [[1, 0], [0, 0], [0, 0]], 'b': [[1, 0], [1, 0], [0, 0]], 'c': [[1, 1], [0, 0], [0, 0]],
'd': [[1, 1], [0, 1], [0, 0]], 'e': [[1, 0], [0, 1], [0, 0]], 'f': [[1, 1], [1, 0], [0, 0]],
'g': [[1, 1], [1, 1], [0, 0]], 'h': [[1, 0], [1, 1], [0, 0]], 'i': [[0, 1], [1, 0], [0, 0]],
'j': [[0, 1], [1, 1], [0, 0]], 'k': [[1, 0], [0, 0], [1, 0]], 'l': [[1, 0], [1, 0], [1, 0]],
'm': [[1, 1], [0, 0], [1, 0]], 'n': [[1, 1], [0, 1], [1, 0]], 'o': [[1, 0], [0, 1], [1, 0]],
'p': [[1, 1], [1, 0], [1, 0]], 'q': [[1, 1], [1, 1], [1, 0]], 'r': [[1, 0], [1, 1], [1, 0]],
's': [[0, 1], [1, 0], [1, 0]], 't': [[0, 1], [1, 1], [1, 0]], 'u': [[1, 0], [0, 0], [1, 1]],
'v': [[1, 0], [1, 0], [1, 1]], 'w': [[0, 1], [1, 1], [1, 1]], 'x': [[1, 1], [0, 0], [1, 1]],
'y': [[1, 1], [0, 1], [1, 1]], 'z': [[1, 0], [0, 1], [1, 1]], '1': [[1, 0], [0, 0], [0, 0]],
'2': [[1, 0], [1, 0], [0, 0]], '3': [[1, 1], [0, 0], [0, 0]], '4': [[1, 1], [0, 1], [0, 0]],
'5': [[1, 0], [0, 1], [0, 0]], '6': [[1, 1], [1, 0], [0, 0]], '7': [[1, 1], [1, 1], [0, 0]],
'8': [[1, 0], [1, 1], [0, 0]], '9': [[0, 1], [1, 0], [0, 0]], '0': [[0, 1], [1, 1], [0, 0]],
',': [[0, 0], [1, 0], [0, 0]], '-': [[0, 0], [1, 1], [0, 0]], '?': [[0, 0], [1, 0], [1, 1]],
'!': [[0, 0], [1, 1], [1, 0]], '.': [[0, 0], [1, 1], [0, 1]], '_': [[0, 0], [0, 0], [1, 1]],
' ': [[0, 0], [0, 0], [0, 0]], 32: [[0, 0], [0, 0], [0, 1]], 60: [[0, 1], [0, 1], [1, 1]],
}
def braille1(text):
ntl = []
for ltr in text:
if ltr.isdigit():
ntl.append(NUMBER)
elif ltr.isupper():
ntl.append(CAPITAL)
ltr = ltr.lower()
ntl.append(ltr)
if len(ntl) > 9:
ntl.extend([' '] * (9 - (len(ntl)-1) % 10))
return ntl
def braille_letter(ltr):
#ntl = []
if ltr.isdigit():
yield NUMBER_FORMAT
yield ld[ltr]
elif ltr.isupper():
yield CAPITAL_FORMAT
yield ld[ltr.lower()]
else:
yield ld[ltr]
def braille_page(text: str):
text = braille1(text)
out = []
s, b, r = 0, 0, []
for ix in range(0, len(text), 10):
line = text[ix:ix+10]
print(line)
if ix:
out.append([0]*29)
for l in (0, 1, 2):
r = []
for ltr in line:
if r: r.append(0)
r.extend(ld[ltr][l])
out.append(r)
return out
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"
May 2, 2022
Comments: