fork(4) download
  1. # http://w...content-available-to-author-only...t.com/r/dailyprogrammer/comments/1s061q/120313_challenge_143_easy_braille
  2.  
  3. # Braille is a writing system based on a series of raised / lowered
  4. # bumps on a material, for the purpose of being read through touch
  5. # rather than sight. It's an incredibly powerful reading & writing
  6. # system for those who are blind / visually impaired. Though the
  7. # letter system has up to 64 unique glyph, 26 are used in English
  8. # Braille for letters. The rest are used for numbers, words, accents,
  9. # ligatures, etc.
  10. #
  11. # Your goal is to read in a string of Braille characters (using standard
  12. # English Braille defined here) and print off the word in standard English
  13. # letters. You only have to support the 26 English letters.
  14. #
  15. # Formal Inputs & Outputs
  16. #
  17. # Input Description
  18. # Input will consistent of an array of 2x6 space-delimited Braille characters.
  19. # This array is always on the same line, so regardless of how long the text is,
  20. # it will always be on 3-rows of text. A lowered bump is a dot character '.',
  21. #
  22. #
  23. # Output Description
  24. # Print the transcribed Braille.
  25. #
  26. # Sample Inputs & Outputs
  27. # Sample Input
  28. # O. O. O. O. O. .O O. O. O. OO
  29. # OO .O O. O. .O OO .O OO O. .O
  30. # .. .. O. O. O. .O O. O. O. ..
  31. #
  32. # Sample Output:
  33. # helloworld
  34.  
  35. import sys
  36. import re
  37.  
  38. raw_data = [x.strip().split(" ") for x in sys.stdin]
  39.  
  40. alphabet = "abcdefghijklmnopqrstuvwxyz "
  41. space = " "
  42.  
  43. braille_data = \
  44. ["O.....", "O.O...", "OO....", "OO.O..", "O..O..",
  45. "OOO...", "OOOO..", "O.OO..", ".OO...", ".OOO..",
  46. "O...O.", "O.O.O.", "OO..O.", "OO.OO.", "O..OO.",
  47. "OOO.O.", "OOOOO.", "O.OOO.", ".OO.O.", ".OOOO.",
  48. "O...OO", "O.O.OO", ".OOO.O", "OO..OO", "OO.OOO",
  49. "O..OOO", space]
  50.  
  51. letter_lookup = {k:v for (k, v) in zip(braille_data, alphabet)}
  52. togethered = "".join([letter_lookup["".join(x)] for x in list(zip(*raw_data))])
  53. print(togethered, end="\n\n")
  54.  
  55. #--------------------------------------
  56. # Now we implement the reverse function
  57. #
  58. # This is me just messing around with something
  59. # and iterating over it to make it better. I'm
  60. # really experimenting with args and kwargs and
  61. # whatnot and what have you
  62. #--------------------------------------
  63.  
  64. # Add a couple other lists to build a dictionary/lookup with
  65. # TODO: Generate lists based on a char for raised and a char
  66. # for lowered.
  67. poundlabel="'#' for raised, '.' for lowered"
  68. braille_pound = \
  69. ["#.....", "#.#...", "##....", "##.#..", "#..#..",
  70. "###...", "####..", "#.##..", ".##...", ".###..",
  71. "#...#.", "#.#.#.", "##..#.", "##.##.", "#..##.",
  72. "###.#.", "#####.", "#.###.", ".##.#.", ".####.",
  73. "#...##", "#.#.##", ".###.#", "##..##", "##.###",
  74. "#..###", space]
  75.  
  76. binlabel="'1' for raised and '0' for lowered"
  77. braille_binary = \
  78. ["100000", "101000", "110000", "110100", "100100",
  79. "111000", "111100", "101100", "011000", "011100",
  80. "100010", "101010", "110010", "110110", "100110",
  81. "111010", "111110", "101110", "011010", "011110",
  82. "100011", "101011", "011101", "110011", "110111",
  83. "100111", space]
  84.  
  85. lookup = {k:v for (k, v) in zip(alphabet, braille_data)}
  86. lookup_p = {k:v for (k, v) in zip(alphabet, braille_pound)}
  87. lookup_b = {k:v for (k, v) in zip(alphabet, braille_binary)}
  88.  
  89. def pad(string, length=2, delim=' '):
  90. """ Take a string and add a specified character every nth index """
  91. return delim+delim.join(string[i:i+length] for i in range(0,len(string),length))+delim
  92.  
  93. def create_dict(raised, lowered):
  94. alpha = "abcdefghijklmnopqrstuvwxyz "
  95. sp = " "
  96. base = \
  97. ["O.....", "O.O...", "OO....", "OO.O..", "O..O..",
  98. "OOO...", "OOOO..", "O.OO..", ".OO...", ".OOO..",
  99. "O...O.", "O.O.O.", "OO..O.", "OO.OO.", "O..OO.",
  100. "OOO.O.", "OOOOO.", "O.OOO.", ".OO.O.", ".OOOO.",
  101. "O...OO", "O.O.OO", ".OOO.O", "OO..OO", "OO.OOO",
  102. "O..OOO", sp]
  103. newlist = [x.replace("O", raised).replace(".", lowered) for x in base]
  104. return {k:v for (k, v) in zip(alpha, newlist)}
  105.  
  106.  
  107. def get_braille(sentence, *args, bmap=lookup,
  108. delimchar=' ', lname="'O' for raised and '.' for lowered", ljust=True):
  109. # """This takes a string and returns the English Braille
  110. # representation in three lines of text. It supports
  111. # [aA-zZ] and spaces. It assumes the lookups passed
  112. # or built already."""
  113.  
  114. r1, r2, r3, r4 = [], [], [], []
  115. for c in sentence.lower():
  116. if ljust:
  117. r1+=c+" "
  118. else:
  119. r1+=" "+c
  120. translated = re.findall('..', bmap[c])
  121. r2+=translated[0]
  122. r3+=translated[1]
  123. r4+=translated[2]
  124. print("Translating '", sentence.upper(), "' to braille, with ", lname, sep='')
  125. if args:
  126. for line in ["".join(x) for x in args]: print(line)
  127. print("{0:3}".format(pad("".join(r1), delim=delimchar)),
  128. "{0:3}".format(pad("".join(r2), delim=delimchar)),
  129. "{0:3}".format(pad("".join(r3), delim=delimchar)),
  130. "{0:3}".format(pad("".join(r4), delim=delimchar)),
  131. sep="\n", end="\n\n")
  132.  
  133. # Demonstrate the different usages
  134. get_braille("Hello World")
  135. get_braille("Hello World", bmap=lookup_p, lname=poundlabel)
  136. get_braille("Hello World", bmap=lookup_b, lname=binlabel)
  137. get_braille("Rob Wett")
  138. get_braille("Rob Wett", bmap=lookup_p, lname=poundlabel)
  139. get_braille("Rob Wett", "Using '|' as a delimiting character: ",
  140. "Lets add another line, just args stuff an all",
  141. bmap=lookup_p, lname=poundlabel, delimchar='|')
  142. get_braille("Rob Wett", "Using '+' as a delimiting character: ",
  143. bmap=lookup_b, lname=binlabel, delimchar='+')
  144. get_braille("Rob Wett", "Using '-' as a delimiting character: ",
  145. bmap=lookup_b, lname=binlabel, delimchar='-')
  146. get_braille("Rob Wett", "Using '-' as a delimiting character: ",
  147. bmap=lookup_b, lname=binlabel, delimchar='-', ljust=False)
  148. get_braille("braille")
  149. # messin
  150. messin_args="library\ncards\nr\n4\ntards"
  151. get_braille("braille", messin_args, bmap=lookup_p, lname=poundlabel)
  152. get_braille("braille", bmap=lookup_b, lname=binlabel)
  153. get_braille("j is nonsense", messin_args)
  154. get_braille("j is nonsense", bmap=lookup_p, lname=poundlabel, ljust=False)
  155. get_braille("j is nonsense", bmap=lookup_b, lname=binlabel)
  156.  
  157. get_braille("jis nonsense", bmap=create_dict("X", "o"))
Success #stdin #stdout 0.03s 9440KB
stdin
Standard input is empty
stdout

Translating 'HELLO WORLD' to braille, with 'O' for raised and '.' for lowered
 h  e  l  l  o     w  o  r  l  d  
 O. O. O. O. O.    .O O. O. O. OO 
 OO .O O. O. .O    OO .O OO O. .O 
 .. .. O. O. O.    .O O. O. O. .. 

Translating 'HELLO WORLD' to braille, with '#' for raised, '.' for lowered
 h  e  l  l  o     w  o  r  l  d  
 #. #. #. #. #.    .# #. #. #. ## 
 ## .# #. #. .#    ## .# ## #. .# 
 .. .. #. #. #.    .# #. #. #. .. 

Translating 'HELLO WORLD' to braille, with '1' for raised and '0' for lowered
 h  e  l  l  o     w  o  r  l  d  
 10 10 10 10 10    01 10 10 10 11 
 11 01 10 10 01    11 01 11 10 01 
 00 00 10 10 10    01 10 10 10 00 

Translating 'ROB WETT' to braille, with 'O' for raised and '.' for lowered
 r  o  b     w  e  t  t  
 O. O. O.    .O O. .O .O 
 OO .O O.    OO .O OO OO 
 O. O. ..    .O .. O. O. 

Translating 'ROB WETT' to braille, with '#' for raised, '.' for lowered
 r  o  b     w  e  t  t  
 #. #. #.    .# #. .# .# 
 ## .# #.    ## .# ## ## 
 #. #. ..    .# .. #. #. 

Translating 'ROB WETT' to braille, with '#' for raised, '.' for lowered
Using '|' as a delimiting character: 
Lets add another line, just args stuff an all
|r |o |b |  |w |e |t |t |
|#.|#.|#.|  |.#|#.|.#|.#|
|##|.#|#.|  |##|.#|##|##|
|#.|#.|..|  |.#|..|#.|#.|

Translating 'ROB WETT' to braille, with '1' for raised and '0' for lowered
Using '+' as a delimiting character: 
+r +o +b +  +w +e +t +t +
+10+10+10+  +01+10+01+01+
+11+01+10+  +11+01+11+11+
+10+10+00+  +01+00+10+10+

Translating 'ROB WETT' to braille, with '1' for raised and '0' for lowered
Using '-' as a delimiting character: 
-r -o -b -  -w -e -t -t -
-10-10-10-  -01-10-01-01-
-11-01-10-  -11-01-11-11-
-10-10-00-  -01-00-10-10-

Translating 'ROB WETT' to braille, with '1' for raised and '0' for lowered
Using '-' as a delimiting character: 
- r- o- b-  - w- e- t- t-
-10-10-10-  -01-10-01-01-
-11-01-10-  -11-01-11-11-
-10-10-00-  -01-00-10-10-

Translating 'BRAILLE' to braille, with 'O' for raised and '.' for lowered
 b  r  a  i  l  l  e  
 O. O. O. .O O. O. O. 
 O. OO .. O. O. O. .O 
 .. O. .. .. O. O. .. 

Translating 'BRAILLE' to braille, with '#' for raised, '.' for lowered
library
cards
r
4
tards
 b  r  a  i  l  l  e  
 #. #. #. .# #. #. #. 
 #. ## .. #. #. #. .# 
 .. #. .. .. #. #. .. 

Translating 'BRAILLE' to braille, with '1' for raised and '0' for lowered
 b  r  a  i  l  l  e  
 10 10 10 01 10 10 10 
 10 11 00 10 10 10 01 
 00 10 00 00 10 10 00 

Translating 'J IS NONSENSE' to braille, with 'O' for raised and '.' for lowered
library
cards
r
4
tards
 j     i  s     n  o  n  s  e  n  s  e  
 .O    .O .O    OO O. OO .O O. OO .O O. 
 OO    O. O.    .O .O .O O. .O .O O. .O 
 ..    .. O.    O. O. O. O. .. O. O. .. 

Translating 'J IS NONSENSE' to braille, with '#' for raised, '.' for lowered
  j     i  s     n  o  n  s  e  n  s  e 
 .#    .# .#    ## #. ## .# #. ## .# #. 
 ##    #. #.    .# .# .# #. .# .# #. .# 
 ..    .. #.    #. #. #. #. .. #. #. .. 

Translating 'J IS NONSENSE' to braille, with '1' for raised and '0' for lowered
 j     i  s     n  o  n  s  e  n  s  e  
 01    01 01    11 10 11 01 10 11 01 10 
 11    10 10    01 01 01 10 01 01 10 01 
 00    00 10    10 10 10 10 00 10 10 00 

Translating 'JIS NONSENSE' to braille, with 'O' for raised and '.' for lowered
 j  i  s     n  o  n  s  e  n  s  e  
 oX oX oX    XX Xo XX oX Xo XX oX Xo 
 XX Xo Xo    oX oX oX Xo oX oX Xo oX 
 oo oo Xo    Xo Xo Xo Xo oo Xo Xo oo