Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difference between codes
#1
What is the difference between these two codes?


while True:
    try:
        List1 = input()
        List2 = []
        correct = False
        P1 = 0
        P2 = 0
        for v in List1:
            if (v == '('):
                P1 += 1
                List2.append(v)
            if (v == ')'):
                P2 += 1
                List2.append(v)
        if(len(List2) % 2 != 0):
            correct = False
        else:
            if(List2[0] == ')'):
                correct = False
            else:
                if (List2[len(List2) - 1] == '('):
                    correct = False
                else:
                    if (P1 != P2):
                        correct = False
                    else:
                        correct = True
        if(correct):
            print("correct")
        else:
            print("incorrect")
    except (EOFError):
        break
while True:
    try:
        a = input()
        b = 0
        for i in range(len(a)):
            if(a[i] == '('):
                b += 1
            elif(a[i] == ')'):
                b -= 1
            if(b < 0):
                break
        if(b != 0):
            print('incorrect')
        else:
            print('correct')

    except EOFError:
        break
Yoriz write Nov-17-2021, 06:27 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
They appear to accomplish the same thing. So does this Smile
while True:
	test = input()
	uneven = 0
	for character in test :
		if character == '(' :
			uneven += 1
		elif character == ')' :
			uneven -= 1
	print ('incorrect' if uneven else 'correct')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020