#🔒 (),{},&[] strings

35 messages · Page 1 of 1 (latest)

wide junco
#

I have this question in my coding club, and I'm kind of confused.
Given a string containing '(', ')', '{', '}', '[', ']', write a function that returns true/false if the string is a valid expression. An expression is considered valid if:
Each type of bracket is properly closed.
Open brackets must be closed in the correct order.

For example: "()", "()[]{}", and "{[]}" are valid expressions.
"(]", "([)]", and "{" are not valid expressions.

return true if the string is valid, otherwise return false.

Can someone explain to me how to code this?

steady scrollBOT
#

@wide junco

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dull reef
wide junco
#

you need to use a hash table right?

dull reef
wide junco
#

if im gonna be honest

#

im not sure

#

thats kind of why i asked

dull reef
#

then why did you choose a hash table instead of something else like a list or an int or anything else

wide junco
#

one of my friends told me a hash table was involved

sonic tartan
# wide junco im not sure

it would be useful if you had a data structure where you can add items, and if a closing bracket matches the item at the top you would pop it. do you know of any such data structures?

dull reef
#

i c
spoilers, a hash table doesn't have to be involved

wide junco
#

he said there isnt lmao

dull reef
wide junco
#

alright

dull reef
#

some examples that might get you going a bit more:

  • (()) is valid because all parentheses match
  • (() obv isn't valid because there are too many (s that don't have a matching )
  • ()) is similarly invalid
  • ))(( is invalid despite there being 2 ) and (s, because ) in front of ( doesn't close properly
  • ( [ ) ] is invalid, because ) doesn't close a [, which was the last opening paren we saw
wide junco
#

hey so im woring on this quesiton with my friend

#

weve come up with this so far

#

are we on the right track so far?

dim verge
#

What's a synonym of the term "pile"

#

Enough hints

dim verge
dim verge
dim verge
dusty timber
#

stack

dim verge
wary jewel
#

I confess I'd use regexps for this.

vital mica
rigid birch
#

heres a hint: think in terms of "at what point does the string become invalid?". rather than looking at the entire string, imagine you were writing it yourself, left to right. how do you yourself know, at any given point what characters may go next? what is it about the other characters that would make the string invalid?

for example, think about why ({(})) is invalid. write it out yourself and take note of the first moment where you can tell something is wrong and figure out why it must be wrong

steady scrollBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.