fork download
  1. # Constants for making it easy
  2. leftBrackets = ['(', '{', '[' ];
  3. rightBrackets = [')', '}', ']'];
  4. examples = [ '((your[drink {remember to}]) ovaltine)',
  5. '[racket for {brackets (matching) is a} computers]',
  6. '[can {and it(it (mix) up ) } look silly]',
  7. '((your[drink {remember to))) ovaltine)',
  8. '[can {and it(it (mix) up ) look silly]',
  9. '[racket for brackets (matching) is a} computers]',
  10. '{years [four score] ago (and seven) our fathers}'
  11. ];
  12.  
  13. def testForErrors(example):
  14. for leftBracket, rightBracket in zip(leftBrackets, rightBrackets):
  15. if(example.count(leftBracket) != example.count(rightBracket)):
  16. print 'Generates an error "Missing opening bracket';
  17. return False;
  18.  
  19. return True;
  20.  
  21. def MARCHMADNESS(s):
  22. if s == '':
  23. return '';
  24.  
  25. for leftBracket, rightBracket in zip(leftBrackets, rightBrackets):
  26. s = s.replace(leftBracket, '(');
  27. s = s.replace(rightBracket, ')');
  28.  
  29. leftBracketIndex = s.rfind('(');
  30. rightBracketIndex = s.find(')');
  31. while(leftBracketIndex > rightBracketIndex):
  32. leftBracketIndex = s.rfind('(', 0, leftBracketIndex);
  33.  
  34. return s[leftBracketIndex+1:rightBracketIndex]+ ' ' + str(MARCHMADNESS(s[0:leftBracketIndex] + s[rightBracketIndex+1:])).strip().replace(' ', ' ');
  35.  
  36. for example in examples:
  37. if(testForErrors(example) == False):
  38. continue;
  39. print MARCHMADNESS(example);
  40.  
  41.  
Success #stdin #stdout 0.01s 7852KB
stdin
Standard input is empty
stdout
remember to drink your ovaltine
matching brackets is a racket for computers
mix it up and it can look silly
Generates an error "Missing opening bracket
Generates an error "Missing opening bracket
Generates an error "Missing opening bracket
four score and seven years ago our fathers