Python URI Error - 2588 - Palindromes

0

As I upload my code to URI Online Judge (problem 2588) , give a error, and I can not fix it at all.

The statement is this:

Andmycodesentwasthis:

entrada=input()letras_unicas=set([iforiinset(list(entrada))ifentrada.count(i)%2!=0])iflen(letras_unicas)>1:print(len(letras_unicas)-1)else:print(0)

Andtheerrorpresented:

    
asked by anonymous 14.12.2017 / 15:10

1 answer

2

You broke the line where it should not, within set(...) . I think the way you wanted to be this:

entrada = input()

letras_unicas = set([i for i in set(list(entrada)) if entrada.count(i) %2 != 0])

if len(letras_unicas) > 1:
   print(len(letras_unicas)-1)
else:
   print(0)

However, this code still does not work completely. Well, he's just looking at the first line of the entrance. If the first line is batata , it will output 1 . If it is aabb , it will give 0. If it is abc , it will give 2. These are the expected results.

See here that running on ideone.

To end the exercise, I think the only thing you will need to do is put a loop to read several lines of the file. Each time the input() is executed, one line is read, then a while True: would be enough to read all the lines. Use a try with a except EOFError and a break as a stop condition to exit the loop.

The code would look like this:

while True:
   try:
      entrada = input()
   except EOFError:
      break

   letras_unicas = set([i for i in set(list(entrada)) if entrada.count(i) %2 != 0])

   if len(letras_unicas) > 1:
      print(len(letras_unicas)-1)
   else:
      print(0)

See here that running on ideone.

    
14.12.2017 / 16:41