fork download
  1. import re
  2.  
  3. string = """
  4. Which will legally declare, construct, and initialize an array?
  5. A. int [] myList = {"1", "2", "3"};
  6. B. int [] myList = (5, 8, 2);
  7. C. int myList [] [] = {4,9,7,0};
  8. D. int myList [] = {4, 3, 7};
  9. ANSWER: D
  10.  
  11. Which will legally declare, construct, and initialize an array?
  12. A. int [] myList = {"1", "2", "3"};
  13. B. int [] myList = (5, 8, 2);
  14. C. int myList [] [] = {4,9,7,0};
  15. D. int myList [] = {4, 3, 7};
  16. ANSWER: C
  17.  
  18. Which will legally declare, construct, and initialize an array?
  19. A. int [] myList = {"1", "2", "3"};
  20. B. int [] myList = (5, 8, 2);
  21. C. int myList [] [] = {4,9,7,0};
  22. D. int myList [] = {4, 3, 7};
  23. ANSWER: A
  24.  
  25. Which will legally declare, construct, and initialize an array?
  26. A. int [] myList = {"1", "2", "3"};
  27. B. int [] myList = (5, 8, 2);
  28. C. int myList [] [] = {4,9,7,0};
  29. D. int myList [] = {4, 3, 7};
  30. ANSWER: E
  31. """
  32.  
  33. rx = re.compile(r'''
  34. (?!^[A-E]\.)
  35. (?P<question>.+[\n\r])
  36. (?P<choices>[\s\S]+?)
  37. ^ANSWER:\ (?P<answer>[A-E])
  38. ''', re.MULTILINE | re.VERBOSE)
  39.  
  40. rq = re.compile(r'^[A-E]\.\ (?P<choice>.+)')
  41.  
  42. for match in rx.finditer(string):
  43.  
  44. def repl(line, answer):
  45. if line.startswith(answer):
  46. line = rq.sub(r"*\1", line)
  47. else:
  48. line = rq.sub(r"\1", line)
  49. return line
  50.  
  51. lines = [repl(line, match.group('answer'))
  52. for line in match.group('choices').split("\n")
  53. if line]
  54.  
  55. block = match.group('question') + "\n".join(lines)
  56. print(block)
Success #stdin #stdout 0.01s 9992KB
stdin
Standard input is empty
stdout
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
*int myList [] = {4, 3, 7};
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
*int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
Which will legally declare, construct, and initialize an array?
*int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};