0

I'm pretty new to python, and am learning data structures at the moment. I need to make a function that can validate an expression. I have user input asking for the expression, and the opening & closing characters they want to validate. E.g., (9,0) = valid; (9,0)) or (9,0 etc. = invalid.

The task mentions requirements, but I'm all new to this and not understanding what its telling me to do. It says this:

Go through the expression character by character and insert occurrences of the opening character to the data structure until you reach a closing character. If the closing character is found, remove the opening character from the data structure. If you try to remove it and there is nothing on the data structure, and there are several closing brackets, then the expression is incorrect, and you do not need to check the expression further. After you have gone through the whole expression, check the size of the data structure. If it has no elements, the expression is correct.

It's particularly the first sentence I'm struggling to understand, especially when it comes to the idea of expressions like: [[4,5][6]]. Here are some different interpretations I had of it:

  • Am I taking a single instance of the opening character, and inserting it after each item in the expression until I reach the closing character, like this: exp = {12} , list = {{1{2{}
  • Am I creating a new list where I stack occurances and then delete them all? E.g., exp = {12}, list = {{{}
  • Insert a single instance and have it hop through the sequence, like: {{12}, {1{2}, {12{}

Someway else perhaps?

paparonnie
  • 170
  • 8
  • When you find an opening character (`{`), put it in the data structure (e.g. `['{', '{']` → `['{', '{', '{']`). When you find a closing character, remove one (e.g. `['{', '{', '{']` → `['{', '{']`). You could also use a simple counter and add and subtract `1`. – deceze Oct 31 '22 at 08:58

0 Answers0