Skip to content

Instantly share code, notes, and snippets.

@duanebester
Created October 21, 2020 16:16
Show Gist options
  • Save duanebester/0d12434e1251d25fb455d60e73159513 to your computer and use it in GitHub Desktop.
Save duanebester/0d12434e1251d25fb455d60e73159513 to your computer and use it in GitHub Desktop.
Python Braille Challenge
def merge(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z
# ASCII val of 'a' to shift by
a_ord = ord('a') - 1
# braille sums
sums = (1, 5, 3, 11, 9, 7, 15, 13, 6, 14)
# a - j
braille_map = dict(zip(range(1, 11), sums))
# k - t
braille_map = merge(braille_map, dict(
zip(range(11, 21), map(lambda x: x + 16, sums))))
# u - v
braille_map = merge(braille_map, dict(
zip(range(21, 23), map(lambda x: x + 48, sums))))
# w - z
braille_map = merge(
braille_map,
{
23: 14 + 32, # w is special
24: 3 + 48, # x
25: 11 + 48, # y
26: 9 + 48, # z
})
def shuffle(i):
"""
Returns the Braille binary in the order the printer expects
"""
s = format(i, '06b')
a = s[0]
b = s[1]
c = s[2]
d = s[3]
e = s[4]
f = s[5]
return f + d + b + e + c + a
def letter_to_braille(letter):
"""
Convert ascii letter to braille
"""
if letter == " ":
return '000000'
elif letter.isupper():
return '000001' + letter_to_braille(letter.lower())
else:
letter_ord = ord(letter) - a_ord
return shuffle(braille_map[letter_ord])
def solution(s):
b = ""
for letter in s:
b += letter_to_braille(letter)
return b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment