+ 2
How do you think I could optimize this code in Python?
3 Réponses
+ 6
Jean Carlo Martinez Lo Iacono
cadena = input()
print ("Deja Vu" if any(cadena.count(l)>1 for l in cadena) else "Unique")
+ 4
You don't need norepetida, because you're doing nothing with it.
So you can shorten the first part like this:
cadena = input()
repetida = 0
for i in cadena:
if cadena.count(i) > 1:
repetida += 1
Another thing you could do is just make repetida a boolean.
Because all you need to know is:
Is one of the letters in there more than once or not?
Then your code looks like this:
cadena = input()
repetida = False
for i in cadena:
if cadena.count(i) > 1:
repetida = True
if repetida:
print ("Deja Vu")
else:
print ("Unique")
Using the extended for loop of Python, you can shrink this further. Actually let's get rid of all variables:
for i in input() :
if cadena.count(i) > 1:
print ("Deja Vu")
break
else:
print ("Unique")
Python has a nice function any which can get rid of the loop.
cadena = input()
if any(cadena.count(l)>1 for l in cadena):
print ("Deja Vu")
else:
print ("Unique")
+ 3
excellent I will take it into account, thanks