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 Moff
class BrailleTranslator(object):
alpha = {'a': 32, 'b': 40, 'c': 48, 'd': 52, 'e': 36, 'f': 56, 'g': 60, 'h': 44, 'i': 24, 'j': 28, 'k': 34, 'l': 42,
'm': 50, 'n': 54, 'o': 38, 'p': 58, 'q': 62, 'r': 46, 's': 26, 't': 30, 'u': 35, 'v': 43, 'w': 31, 'x': 51,
'y': 55, 'z': 39, '0': 28, '1': 32, '2': 40, '3': 48, '4': 52, '5': 36, '6': 56, '7': 60, '8': 44, '9': 24,
' ': 0, ',': 8, '.': 13, '-': 12, '?': 11, '_': 3, '!': 14, 'capital': 1, 'number': 23}
@classmethod
def letter_matrix(cls, c):
n = cls.alpha[c]
result = [[], [], []]
for i in range(2, -1, -1):
for _ in range(2):
result[i].insert(0, n & 1)
n >>= 1
return result
@classmethod
def encode(cls, s):
letters = []
for c in s:
if c.isupper():
letters.extend(['capital', c.lower()])
elif c.isdigit():
letters.extend(['number', c])
else:
letters.append(c)
if len(letters) > 10 and len(letters) % 10 > 0:
letters.extend([' '] * (10 - len(letters) % 10))
result = []
for i in range(0, len(letters), 10):
if i > 0:
result.append([0] * 29)
mx = list(map(cls.letter_matrix, letters[i: i + 10]))
for j in range(3):
row = []
for a in mx:
if row:
row.append(0)
row.extend(a[j])
result.append(row)
return result
def braille_page(s):
return BrailleTranslator.encode(s)
Aug. 19, 2015