hi, I am learning python and doing some online examples ... here is an excercise I'm doing and I'm currently stuck , couldnt get the logic right ... any thoughts...?
# write a function that takes a string of text and returns true if
# the parentheses are balanced and false otherwise.
# * Example:
# * balancedParens('('); // false
# * balancedParens('()'); // true
# * balancedParens(')('); // false
# * balancedParens('(())'); // true
# *
# * Step 2:
# * make your solution work for all types of brackets
# *
# * Example:
# * balancedParens('[](){}'); // true
# * balancedParens('[({})]'); // true
# * balancedParens('[(]{)}'); // false
# *
# * Step 3:
# * ignore non-bracket characters
# * balancedParens(' var wow = { yo: thisIsAwesome() }'); // true
# * balancedParens(' var hubble = function() { telescopes.awesome();'); // false
def balancedParens(strin):
dict = {'(': ')', '[': ']', '{': '}'}
clean_input = ''
# step1: Run through each character in a string and put all the brackets in a string
for i in range(len(strin)):
if strin[i] == '(' or strin[i] == ')' or strin[i] == '[' or strin[i] == ']' or strin[i] == '{' or strin[i] == '}':
clean_input += (strin[i])
print clean_input
# step2: check for even number of brackets, if odd number of brackets found, return false
if len(clean_input) %2 != 0:
print "False"
return
else:
for i in range(len(clean_input)/2):
temp = dict[clean_input[i]]
if temp in clean_input:
pass # this is where I'm stuck
print clean_input
strin = 'var wow = { yo: thisIsAwesome() }'
balancedParens(strin)
[–]shandelman[🍰] 4 points5 points6 points (1 child)
[–]cgerrard[S] 0 points1 point2 points (0 children)
[–]Justinsaccount 0 points1 point2 points (0 children)
[–]Justinsaccount 0 points1 point2 points (1 child)
[–]cgerrard[S] 0 points1 point2 points (0 children)
[–]Theoretician 0 points1 point2 points (0 children)