id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

david.white@denison.edu cs111fall2019 > balance_parens
prev  |  next  |  chance

Write a function that takes as input a string that may contain some parentheses, and returns a version of the string with balanced parentheses, formed by adding the minimum number of parens, and otherwise leaving the string unchanged. In general, there are several possible ways to do this. For example, the string "((" could be balanced as "(())" or as "()()". For the purpose of this exercise, do it so that the missing parens are added at the last possible position in the string; that is, as far right as possible, so that we would chose "(())".


balance_parens('((') → '(())'
balance_parens('(x + 1') → '(x + 1)'
balance_parens('(x * (x - 1') → '(x * (x - 1))'

...Save, Compile, Run (ctrl-enter)

def balance_parens(text):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Post-solution available

Copyright Nick Parlante 2017 - privacy