Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. # COMP 202 A2 Part 2
  2. # Author: Steven Christopher Philipper
  3. # Student ID: 260731823
  4.  
  5. from helpers import *
  6.  
  7. # More constants you'll want to use:
  8. SPACE = '..\n..\n..'
  9. HYPHEN = '..\n..\noo'
  10. APOSTROPHE = '..\n..\no.'
  11. QUOTES = '..\noo\noo'
  12.  
  13.  
  14. ############################ Functions
  15.  
  16. def convert_irregular(c):
  17. '''(str) -> str
  18. Convert the irregular characters to French Braille.
  19. Recall these are: space, hyphen, apostrophe, guillements
  20. Apostrophe could be represented by `, ’ or '.
  21. Hyphen could be represented by - or by –.
  22. Note the constants such as SPACE and HYPHEN above.
  23.  
  24. >>> print(convert_irregular('-'))
  25. ..
  26. ..
  27. oo
  28. >>> convert_irregular('–')
  29. '..\\n..\\noo'
  30. >>> convert_irregular('`')
  31. '..\\n..\\no.'
  32. >>> convert_irregular("'")
  33. '..\\n..\\no.'
  34. >>> convert_irregular("’")
  35. '..\\n..\\no.'
  36. >>> convert_irregular("»")
  37. '..\\noo\\noo'
  38. '''
  39. if c in IRREGULAR_CHARS:
  40. if c == '-' or c == '–':
  41. return HYPHEN
  42. elif c == ' ':
  43. return SPACE
  44. elif c == "`" or c == "’" or c == "'":
  45. return APOSTROPHE
  46. elif c == '«' or c == '»':
  47. return QUOTES
  48. return None
  49.  
  50.  
  51. def decade_pattern(decade_position):
  52. '''(int) -> str
  53. Using position in Braille decade, get associated Braille pattern.
  54. Provided to students. Do not edit this function.
  55.  
  56. >>> decade_pattern(0)
  57. 'o.\\n..'
  58. >>> decade_pattern(9)
  59. '.o\\noo'
  60. '''
  61. DEC_SEQ = ['o.\n..', 'o.\no.', 'oo\n..', 'oo\n.o',
  62. 'o.\n.o', 'oo\no.', 'oo\noo', 'o.\noo',
  63. '.o\no.', '.o\noo']
  64. return DEC_SEQ[decade_position]
  65.  
  66.  
  67. def convert_digit(c):
  68. '''(str) -> str
  69. Convert string representation of digit
  70. to Braille. For this, put the decade value in the top two rows,
  71. and put '..' in the bottow row.
  72. Hints:
  73. - Remember: we provided the string DIGITS to you
  74. - For full credit, this should have fewer than 4 lines of code.
  75.  
  76. >>> print(convert_digit('1'))
  77. o.
  78. ..
  79. ..
  80. >>> print(convert_digit('3'))
  81. oo
  82. ..
  83. ..
  84. >>> print(convert_digit('0'))
  85. .o
  86. oo
  87. ..
  88. '''
  89. if c in DIGITS:
  90. return decade_pattern(int(c)-1) + '\n..'
  91. return None
  92.  
  93.  
  94. def convert_punctuation(c):
  95. '''(str) -> str
  96. Convert string representation of common punctuation
  97. to French Braille. For this put the decade value in the bottom
  98. two rows, and put '..' in the top row.
  99. Hints:
  100. - Use the string PUNCTUATION we provided to you
  101. - Recall there are helper functions we gave you
  102. - For full credit, this should have fewer than 4 lines of code.
  103. - You should not have to manually enter any Braille strings
  104.  
  105. >>> print(convert_punctuation(','))
  106. ..
  107. o.
  108. ..
  109. >>> print(convert_punctuation(';'))
  110. ..
  111. o.
  112. o.
  113. >>> print(convert_punctuation(':'))
  114. ..
  115. oo
  116. ..
  117. >>> print(convert_punctuation('"'))
  118. ..
  119. oo
  120. oo
  121. '''
  122. if c in PUNCTUATION:
  123. return '..\n' + decade_pattern(is_punctuation(c))
  124. return False
  125.  
  126.  
  127. #############################
  128.  
  129.  
  130. def decade_ending(dec_num):
  131. '''(int) -> str
  132. For one of the four decades of standard letters in French Braille,
  133. return the associated bottom-row (see page 3 of pdf.)
  134.  
  135. >>> decade_ending(0)
  136. '..'
  137. >>> decade_ending(1)
  138. 'o.'
  139. >>> decade_ending(2)
  140. 'oo'
  141. >>> decade_ending(3)
  142. '.o'
  143. '''
  144. return INCOMPLETE
  145.  
  146.  
  147. def letter_row(c):
  148. '''(str) -> int
  149. For a standard letter in French Braille, return
  150. the number of the decade it belongs to. (See table on page 3 of pdf.)
  151. Provided to students. Do not edit this function.
  152.  
  153. >>> letter_row('a')
  154. 0
  155. >>> letter_row('w')
  156. 3
  157. >>> letter_row('n')
  158. 1
  159. >>> letter_row('N')
  160. 1
  161. '''
  162. c = c.lower() # convert
  163. for i, decade in enumerate(LETTERS):
  164. if c in decade:
  165. return i
  166.  
  167.  
  168. def letter_column(c):
  169. '''(str) -> int
  170. For a standard letter in French Braille, return
  171. its position within its decade. (See table on page 3 of pdf.)
  172. Provided to students. Do not edit this function.
  173.  
  174. >>> letter_column('a')
  175. 0
  176. >>> letter_column('b')
  177. 1
  178. >>> letter_column('v')
  179. 1
  180. >>> letter_column('w')
  181. 9
  182. >>> letter_column('W')
  183. 9
  184. '''
  185. c = c.lower() # convert
  186. for decade in LETTERS:
  187. if c in decade:
  188. return decade.find(c)
  189.  
  190.  
  191. def convert_letter(c):
  192. '''(str) -> str
  193. For one of the standard letters in French Braille,
  194. return its Braille representation.
  195.  
  196. >>> print(convert_letter('a'))
  197. o.
  198. ..
  199. ..
  200. >>> print(convert_letter('b'))
  201. o.
  202. o.
  203. ..
  204. >>> print(convert_letter('p'))
  205. oo
  206. o.
  207. o.
  208. >>> print(convert_letter('ç'))
  209. oo
  210. o.
  211. oo
  212. >>> print(convert_letter('ô'))
  213. oo
  214. .o
  215. .o
  216. >>> print(convert_letter('A'))
  217. o.
  218. ..
  219. ..
  220. >>> print(convert_letter('Œ'))
  221. .o
  222. o.
  223. .o
  224. '''
  225. return INCOMPLETE
  226.  
  227.  
  228. def char_to_braille(c):
  229. '''(str) -> str
  230. Convert a character, c, into French Braille.
  231. If c is a character we don't know how to convert, return
  232. the same character as before.
  233.  
  234. >>> print(char_to_braille('-'))
  235. ..
  236. ..
  237. oo
  238. >>> print(char_to_braille('w'))
  239. .o
  240. oo
  241. .o
  242. >>> print(char_to_braille('1'))
  243. o.
  244. ..
  245. ..
  246. >>> print(char_to_braille('?'))
  247. ..
  248. o.
  249. .o
  250. >>> char_to_braille('.')
  251. '..\\noo\\n.o'
  252. >>> char_to_braille('a')
  253. 'o.\\n..\\n..'
  254. >>> char_to_braille('n')
  255. 'oo\\n.o\\no.'
  256. >>> char_to_braille('Z')
  257. 'o.\\n.o\\noo'
  258. >>> char_to_braille('Œ')
  259. '.o\\no.\\n.o'
  260. >>> char_to_braille(' ')
  261. '..\\n..\\n..'
  262. >>> char_to_braille('ß')
  263. 'ß'
  264. >>> char_to_braille('\\n')
  265. '\\n'
  266. '''
  267. return INCOMPLETE
  268.  
  269.  
  270. if __name__ == '__main__':
  271. doctest.testmod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement