## Problem with the Beginning of the Bracket Problem

This forum is a place to ask questions about the python programming language.
jhon
New User
Posts: 2
Joined: 24 May 2018, 08:46

### Problem with the Beginning of the Bracket Problem

To solve the bracket problem, I wanted to start by creating an array with all the brackets in the order that they are in and check if they are correct from the array. The code below is what I wrote to create the array from the inserted text. The variable text is the text to check. The variable numOfChars is the number of characters in the text. The variable charNum is to count which character the loop is on. The variable numOfBrackets is the total number of brackets. The variable x is just used to print the list at the end. The list is the list of brackets. The problem that this code has is that after the code is complete, the last bracket is added to the beginning, so there is always one more bracket that there should be, and that extra one is at the beginning and is whatever the last bracket is. Thank you for your help.

Code: Select all

text = input("What text do you want to check?")
numOfChars = len(text)
charNum = 0
numOfBrackets = 0
x=0
list = []
for count in range(0,int(numOfChars)):
if text[int(charNum - 1)] == '(' or text[int(charNum - 1)] == '[' or text[int(charNum - 1)] == '{' or text[int(charNum - 1)] == ')' or text[int(charNum - 1)] == ']' or text[int(charNum - 1)] == '}':
numOfBrackets = numOfBrackets + 1
list.append(text[charNum])
charNum = charNum + 1
print(numOfBrackets)
#print(list)

for count in range(int(numOfBrackets)):
print(list[x], sep='')
x = x + 1

0 x

Tags:

Mr. MacKenty
Site Admin
Posts: 89
Joined: 28 Apr 2018, 17:06
Answers: 3

### Re: Problem with the Beginning of the Bracket Problem

Hiya Jhon!

You don't know why your program isn't doing what you want it to do. When I encounter a problem like this I break it into small parts and run each small part so I can see where my code might not be working. This technique almost always works to understand the problem.

Code: Select all

#
# let's start THIS simple: are we grabbing the correct count of charatcers in the input?
#
text = input("What text do you want to check?")
print(len(text))
#
# it seems to return the correct length.
#
ok. Let's poke at that loop you have.

Code: Select all

text = input("What text do you want to check?")
numOfChars = len(text)
for count in range(0,int(numOfChars)):
print(count)
When the input is 8 characters, this code produces:

0
1
2
3
4
5
6
7

This is what I would expect to see, as lists have a zero index. So now I'm going to add a bit of code to better debug this. Let's see what python thinks each character is:

Code: Select all

text = input("What text do you want to check?")
numOfChars = len(text)
for count in range(0,int(numOfChars)):
print(count, " is a ", text[count])

The code above return this result:

Code: Select all

0  is a  [
1  is a  ]
2  is a  {
3  is a  }
4  is a  (
5  is a  )
I notice something odd in your code. You have:

Code: Select all

text[int(charNum - 1)]
I'm unsure what the purpose of this is. Let's break this down even more. You have this conditional which checks if the character is a bracket.

Code: Select all

if text[int(charNum - 1)] == '(' or text[int(charNum - 1)] == '[' or text[int(charNum - 1)] == '{' or text[int(charNum - 1)] == ')' or text[int(charNum - 1)] == ']' or text[int(charNum - 1)] == '}':

If you find a bracket, you:
• increment a counter of the total number of brackets
• append the character to a list named list (but you don't define a list named list anywhere
There might be a more elegant way to check if the current character is a bracket.

Code: Select all

# Define opening brackets here:
openingBracket = ["[","(","{"]

# Define closing brackets here:
closingBracket = ["]",")","}"]

# we'll treat this list basically as a stack:
brackets=[]

# obviously we'll want to read from a text file eventually:
text = input("What text do you want to check?")

# This counts the total number of characters:
numOfChars = len(text)

# Now we loop through our characters. At each iteration, we simply print the character we iterating.
for count in range(0,int(numOfChars)):
print(count, " is a ", text[count])

# Below we check if the current character is in one our 2 lists above.
if text[count] in openingBracket or text[count] in closingBracket:
print("This is a bracket!")

# now that you have identified if something is an opening or closing bracket, it looks like you want to put them in a list
# I think you want to do something like bracket.append(text[count]).
# run the code above with input like {(hello there)[my name is John]}
Now you want to append these brackets into a list. At this point, I'm going to ask you to please think a bit more about this problem.

A few points:

1. Please comment your code the way I have commented my code. It makes it so much easier to read
2. Please remember to run small snippets of code when you run into problems like this.
3. I am profoundly impressed!!!!!!!

jhon wrote:
03 Sep 2018, 18:22
To solve the bracket problem, I wanted to start by creating an array with all the brackets in the order that they are in and check if they are correct from the array. The code below is what I wrote to create the array from the inserted text. The variable text is the text to check. The variable numOfChars is the number of characters in the text. The variable charNum is to count which character the loop is on. The variable numOfBrackets is the total number of brackets. The variable x is just used to print the list at the end. The list is the list of brackets. The problem that this code has is that after the code is complete, the last bracket is added to the beginning, so there is always one more bracket that there should be, and that extra one is at the beginning and is whatever the last bracket is. Thank you for your help.

Code: Select all

text = input("What text do you want to check?")
numOfChars = len(text)
charNum = 0
numOfBrackets = 0
x=0
list = []
for count in range(0,int(numOfChars)):
if text[int(charNum - 1)] == '(' or text[int(charNum - 1)] == '[' or text[int(charNum - 1)] == '{' or text[int(charNum - 1)] == ')' or text[int(charNum - 1)] == ']' or text[int(charNum - 1)] == '}':
numOfBrackets = numOfBrackets + 1
list.append(text[charNum])
charNum = charNum + 1
print(numOfBrackets)
#print(list)

for count in range(int(numOfBrackets)):
print(list[x], sep='')
x = x + 1

0 x