Skip to content

Latest commit

 

History

History
55 lines (33 loc) · 874 Bytes

Remove_the_parentheses.md

File metadata and controls

55 lines (33 loc) · 874 Bytes

CodeWars Python Solutions


Remove the parentheses

In this kata you are given a string for example:

"example(unwanted thing)example"

Your task is to remove everything inside the parentheses as well as the parentheses themselves.

The example above would return:

"exampleexample"

Notes

  • Other than parentheses only letters and spaces can occur in the string. Don't worry about other brackets like "[]" and "{}" as these will never appear.
  • There can be multiple parentheses.
  • The parentheses can be nested.

Given Code

def remove_parentheses(s):
    pass

Solution

import re
def remove_parentheses(s):
    while re.findall(r"\([^()]*\)", s):
        s = re.sub(r"\([^()]*\)", "", s)
    return s

See on CodeWars.com