Can anyone please explain what the error is in my code?
for _ in range(int(input())):
tag_input = input()
tag = list(tag_input)
length = len(tag)
s = True
if length > 3 and tag[0: 2] == ['<', '/'] and tag[-1] == '>':
for i in range(3, length - 1):
if '1' <= tag[i] <= '9' or 'a' <= tag[i] <= 'z':
continue
else:
s = False
break
else:
s = False
if s:
print('Success')
else:
print('Error')
Thanks - the two solutions are very different - why do you assume that it is the type of n that’s causing the problem? For example, try the WA version with the test input:
0 is considered as a digit so </0> is test case where ur code fails
also u should write like this (!((s[i]>=‘a’ && s[i]<=‘z’) || (s[i]>=‘0’ && s[i]<=‘9’)))
alnum() function returns true when character is uppercase, lowercase or digit so therefore your code also make temp false if it is a lowercase letter, therefore causing error.
I’ve checked all the 3 conditions still I’m getting wrong ans – Here is the code –
t = int(input())
for _ in range(t):
tags = list(input())
if tags[0] != “<” or tags[1]!="/" or tags[-1]!=’>’:
print(“Error”)
else:
for i in range(2,len(tags)-1):
if tags[i].isalnum() == False:
flag = 0
break
elif tags[i].isalpha() and tags[i].isupper():
flag = 0
break
else:
flag = 1
if flag:
print(“Success”)
else:
print(“Error”)