How do you think I could optimize this code in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
23rd Mar 2020, 12:13 AM
Jean Carlo Martinez Lo Iacono
Jean Carlo Martinez Lo Iacono - avatar
3 Answers
+ 6
Jean Carlo Martinez Lo Iacono cadena = input() print ("Deja Vu" if any(cadena.count(l)>1 for l in cadena) else "Unique")
23rd Mar 2020, 3:19 AM
Cmurio
Cmurio - avatar
+ 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")
23rd Mar 2020, 12:44 AM
HonFu
HonFu - avatar
+ 3
excellent I will take it into account, thanks
23rd Mar 2020, 12:49 AM
Jean Carlo Martinez Lo Iacono
Jean Carlo Martinez Lo Iacono - avatar