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 jcg
def braille_page(text, NB_BLOCKS_PER_LINE=10):
page = []
for i, line in enumerate(genereLineOfBlocks(text, NB_BLOCKS_PER_LINE)):
if i: # if not first line, put a line of 0 before actual line
addLineSep(page)
addLineOfBlocks(page, line)
if i: # if not first line, we complete line with 0
completeLastLine(page)
return page
ROW, COL = (3, 2) # dim block three rows two cols
SEP_BLOCK = 0 # the symbol for separate blocks
DICO_BLOCKS = {} # dictionary letters - blocks
CAPS, NUM = '^', '§' # for prefix blocks
def addBlock(page, block):
# for a block of dots, extend the ROW last lines of page
# whith COL symbols of the block
# ------------- -------------
# ------------- -------------
# 010000 010000+01
# 010000 + 010010 -> 010000+00
# 001100 001100+10
#
t = 0
for subLine in range(-ROW, 0):
page[subLine].extend(block[t: t + COL])
t += COL
def addBlockSep(page):
# extend the ROW last lines of page
# whith SEP symbol
# ------------- -------------
# ------------- -------------
# 01000 01000+0
# 01000 -> 01000+0
# 00110 00110+0
#
for l in range(-ROW,0):
page[l].append(SEP_BLOCK)
def addLineOfBlocks(page, lineOfBlocks):
# extend the page with ROW lines of symbols
# for a line of blocks
# -------------
# ------------- + [010010, 100010, .....] -->
# -------------
#
# -------------
# 01 0 10 0 ...
# 00 0 00 0 ...
# 10 0 10 0 ...
page.extend([] for _ in range(ROW)) # ROW more empty lines
for i,block in enumerate(lineOfBlocks):
if i: # if not fisrst block
addBlockSep(page)
addBlock(page, block)
def addLineSep(page):
# extend the page with an empty line of 0
page.append([SEP_BLOCK] * len(page[0]))
def completeLastLine(page):
# complete the ROW last lines with 0
for row in range(-ROW, 0):
page[row].extend([SEP_BLOCK]*(len(page[0]) - len(page[-1])))
def genereLineOfBlocks(txt, length):
l = []
for b in genereBlocks(txt):
l.append(b)
if len(l) == length:
yield l
l = []
if l:
yield l
def genereBlocks(txt):
yield from (b for c in txt for b in brailleBlocks(c))
def brailleBlocks(c):
# returns the generator (1 or 2) of Braille blocks
# for the character c
if c.isupper():
yield DICO_BLOCKS[CAPS]
elif c.isdigit():
yield DICO_BLOCKS[NUM]
yield DICO_BLOCKS[c.lower()]
# dict for each dot, ^ for CAPS, § for NUM
d = {
0: 'abcdefghklmnopqruvxyz',
1: 'cdfgijmnpqstxyw§',
2: 'bfghijlpqrstvw,.-?!',
3: 'deghjnoqrtyzw§.-!',
4: 'klmnopqrstuvxyzw§?_!', #§ for num,
5: 'uvxyzw^§.?_' #^ for caps
}
# reverse _dico
DICO_BLOCKS = {}
for dot in d:
for c in d[dot]:
if c not in DICO_BLOCKS:
DICO_BLOCKS[c] = [0]*6
DICO_BLOCKS[c][dot] = 1
for d, c in zip("1234567890",
"abcdefghij"):
DICO_BLOCKS[d] = DICO_BLOCKS[c]
DICO_BLOCKS[' '] = [0]*6
# dico_blocks= {
##'a': [1, 0,
## 0, 0,
## 0, 0],
##'b': [1, 0,
## 1, 0,
## 0, 0],
##'c': [1, 1,
## 0, 0,
## 0, 0],
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"
Oct. 16, 2014