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 Blastus
# Braille is a tactile writing system used by the blind and the visually
# impaired. It is traditionally written with embossed paper. Braille characters
# are small rectangular blocks, called cells, which contain tiny palpable bumps
# called raised dots. The number and arrangement of these dots distinguish one
# character from another.
# We will use a 6-dots Braille alphabet. Each letter can be represented as a
# 3x2 matrix where 1 is a raised dot and 0 is an empty space.
# alphabet
# a1 b2 c3 d4 e5 f6 g7 h8 i9 j0
# 10 10 11 11 10 11 11 10 01 01
# 00 10 00 01 01 10 11 11 10 11
# 00 00 00 00 00 00 00 00 00 00
# k l m n o p q r s t
# 10 10 11 11 10 11 11 10 01 01
# 00 10 00 01 01 10 11 11 10 11
# 10 10 10 10 10 10 10 10 10 10
# u v x y z w
# 10 10 11 11 10 01
# 00 10 00 01 01 11
# 11 11 11 11 11 11
# Letters should be separated by an empty column. Whitespaces are two empty
# columns (plus a separator empty column if it is between letters). Various
# formatting marks indicate the values of the letters that follow them. They
# have no direct equivalent in print. The most important indicators in English
# Braille are: "capital" and "number". These marks work as "shift" - only for a
# follow letter.
# formating
# capital number
# 00 01
# 00 01
# 01 11
# We will use several basic punctuation symbols:
# punctuation
# , . - ? _ !
# 00 00 00 00 00 00
# 10 11 11 10 00 11
# 00 01 00 11 11 10
# You are given a page of text and you should convert it into Braille. The page
# contains one or several lines represented as a matrix. Each line contains no
# more than 10 symbols (including non-printed). Lines are separated by one
# empty row. Symbols are separated by empty columns but there are no separators
# in beginnings and ends of lines. If text can be placed in one line, then the
# page width is proportional to the length of the text. If the page has more
# than one line, then the width is equal to the longer line and the final line
# is appended by whitespaces.
# For example, this is the text "hello 1st World!".
# example
# 10010010010010000001010001001
# 11001010010001000001000010011
# 00000010010010000011000010010
# 00000000000000000000000000000
# 00000001010010010011000000000
# 00000011001011010001011000000
# 00001011010010010000010000000
# Input: A text as a string.
# Output: The page as a list of lists or tuple of tuples with integers (1/0).
# Precondition:
# 0 < len(text)
# all(ch in string.ascii_letters + " .,!?-_0123456789" for ch in text)
import math
################################################################################
def test():
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"
def checker(func, text, answer):
result = func(text)
return answer == tuple(tuple(row) for row in result)
################################################################################
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)
################################################################################
class Braille:
def __init__(self, text):
self.__text = text
def __iter__(self):
for c in self.__text:
if c.islower():
yield LETTERS_NUMBERS[ord(c) - ord('a')]
elif c.isupper():
yield CAPITAL_FORMAT
yield LETTERS_NUMBERS[ord(c) - ord('A')]
elif c.isnumeric():
yield NUMBER_FORMAT
yield LETTERS_NUMBERS[(ord(c) - ord('1')) % 10]
elif c.isspace():
yield WHITESPACE
elif c in PUNCTUATION:
yield PUNCTUATION[c]
@property
def matrix(self):
code = tuple(self)
size = len(code)
rows, columns = math.ceil(size / 10), min(size, 10)
matrix = [[0] * (columns * 3 - 1) for row in range(rows * 4 - 1)]
character = iter(code)
try:
for y in range(0, rows * 4, 4):
for x in range(0, columns * 3, 3):
for y_offset, row in enumerate(next(character)):
for x_offset, dot in enumerate(row):
matrix[y + y_offset][x + x_offset] = dot
except StopIteration:
pass
return matrix
################################################################################
braille_page = lambda text: Braille(text).matrix
if __name__ == '__main__':
test()
Oct. 29, 2014