#I'm looking for help regarding the matching brackets exercise, so far I can pass 21 of 23 tests.

18 messages · Page 1 of 1 (latest)

loud lantern
#

This is my code:
def is_paired(input_string):
input_string = input_string.replace("()", "").replace("{}", "").replace("[]", "")
if input_string == "":
return True

para = 0
brack = 0
curly = 0
for char in input_string:
    if char == "(":
        para += 1
    elif char == "[":
        brack += 1
    elif char == "{":
        curly += 1
    elif char == ")":
        if para == 0:
            return False
        para -= 1
    elif char == "]":
        if brack == 0 and para > 0:
            return False
        brack -= 1
    elif char == "}":
        if curly == 0:
            return False
        curly -= 1
if para == brack == curly:
    return True
else:
    return False
heady moss
#

what tests does it fail on, what does it return and what do you expect instead?

#

just from looking at the big if block in the middle there, I have a feeling that it'd falsely mark ({)} as well-formed

calm creekBOT
#

To make code easier for everyone to read (and to make you look like a pro), **please use a codeblock when sharing code.**For example, you can type the following:

```
for number in range(10):
total += number;
```
Discord will render that as so:

for number in range(10):
    total += number;

You can even get syntax highlighting by adding the language to the codeblock opening:
```python
for number in range(10):
total += number;
```
Discord will render that as so:

for number in range(10):
    total += number;
ruby badger
#

What would it do with input {[( ?

loud lantern
#

I'm really not proud of this code, I mean I passed but one test case I hard coded
it's probably the opposite of elegant

loud lantern
heady moss
#

instead of keeping track of three different counters of bracket types, I would recommend keeping track of opening brackets by using an array that you add new opening brackets to the top of and pop matching brackets off of

#

that will take care of the problem of keeping track of nested brackets

#

like for example for ([]):

  • at the start, the stack is empty, []
  • the first bracket is an opening parens, so now the stack is ["("]
  • the second bracket is an opening square, stack is ["[", "("]
  • third bracket matches the head of the stack, so you pop it off, stack is now ["("]
  • fourth bracket matches the head of the stack, stack is now []
  • there's no more brackets and the stack is empty so this is valid
somber ravine
#

That is a excelente Idea! I will try also! 😀✌️

#

But one question: You said: Third bracket matches the head of the stack..". But it is not true, because the head is "[" and it received as parameter "]". How to match them???

heady moss
#

write a function that checks if an opening and closing bracket match each other and use that

#

for example, ```python
def is_matching(left, right):
return left + right in ["()", "[]", "{}"]

ruby badger
#

Or a dictionary 😉

heady moss
#

yeah, dictionary would be cool, if you were writing a "real" parser that'd be the way to go