Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 1, 2017 17:47
Show Gist options
  • Save tyler-austin/5c56d307d4a64bd315beebcdd408380e to your computer and use it in GitHub Desktop.
Save tyler-austin/5c56d307d4a64bd315beebcdd408380e to your computer and use it in GitHub Desktop.
Code Fights - reverseParentheses

You have a string s that consists of English letters, punctuation marks, whitespace characters, and brackets. It is guaranteed that the parentheses in s form a regular bracket sequence.

Your task is to reverse the strings contained in each pair of matching parentheses, starting from the innermost pair. The results string should not contain any parentheses.

Example

For string s = "a(bc)de", the output should be
reverseParentheses(s) = "acbde".

Input/Output

  • [time limit] 4000ms (py3)

  • [input] string s

    • A string consisting of English letters, punctuation marks, whitespace characters and brackets. It is guaranteed that parentheses form a regular bracket sequence.

Constraints: 5 ≤ s.length ≤ 55.

  • [output] string
import re
class ReverseParentheses:
@classmethod
def reverse_parentheses(cls, input_str: str):
if '(' in input_str:
return cls.reverse_parentheses(cls._reverse_substring(input_str))
else:
return input_str
@classmethod
def _reverse_substring(cls, input_str: str) -> str:
pattern = r"\(([^()]*)\)"
regexp = re.compile(pattern)
substr = regexp.search(input_str)
substr_list = list(substr.group())
substr_list = [c for c in substr_list if c not in '()']
substr_list.reverse()
substr = ''.join(substr_list)
return re.sub(pattern, substr, input_str, count=1)
import re
def reverseParentheses(input_str: str):
if '(' in input_str:
return reverseParentheses(_reverse_substring(input_str))
else:
return input_str
def _reverse_substring(input_str: str):
pattern = r"\(([^()]*)\)"
regexp = re.compile(pattern)
substr = regexp.search(input_str)
substr_list = list(substr.group())
substr_list = [c for c in substr_list if c not in '()']
substr_list.reverse()
substr = ''.join(substr_list)
return re.sub(pattern, substr, input_str, count=1)
import unittest
from reverse_parentheses import ReverseParentheses
class MyTestCase(unittest.TestCase):
def test_1(self):
s = 'a(bc)de'
solution = 'acbde'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
def test_2(self):
s = 'a(bcdefghijkl(mno)p)q'
solution = 'apmnolkjihgfedcbq'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
def test_3(self):
s = 'co(de(fight)s)'
solution = 'cosfighted'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
def test_4(self):
s = 'Code(Cha(lle)nge)'
solution = 'CodeegnlleahC'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
def test_5(self):
s = 'Where are the parentheses?'
solution = 'Where are the parentheses?'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
def test_6(self):
s = 'abc(cba)ab(bac)c'
solution = 'abcabcabcabc'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
def test_7(self):
s = 'The ((quick (brown) (fox) jumps over the lazy) dog)'
solution = 'The god quick nworb xof jumps over the lazy'
result = ReverseParentheses.reverse_parentheses(s)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment