+ 1

Need help on Deja Vu challenge.

This challenge took me hours, but I still can't pass it. I created a list to store all the letter that user inputs, sort it then find duplicate letter. If duplicate letter is found, it prints "Deja Vu", if duplicate letter not found it prints "Unique". I tried a lot but still can't pass every test. 4 of 5. Here's my attempt: https://code.sololearn.com/cVyG9VV01T6W/?ref=app , when I place the 2 duplicate letter at the first or at the end of the string, i get unique instead of Deja Vu. How do I pass this?

23rd Oct 2022, 4:13 PM
Dragon RB
Dragon RB - avatar
7 ответов
+ 4
Try "outdenting" the sort statement and the second for loop, so that they are at the same level as the first for loop, not inside it. Currently you sort the list every time after adding a letter to your list, and at their same time you do a duplicate check after adding each letter. The first part can actually be simplified by just sorting the input word: letters = sorted(input()) Then a single loop is sufficient that runs len(letters)-1 times and it would check each pair of consecutive letters for equality. it's a bit confusing what you are comparing currently... I mean something like this should work: for i in range(len(letters)-1): if letters[i] == letters[i+1]: duplicate = True
23rd Oct 2022, 4:40 PM
Tibor Santa
Tibor Santa - avatar
+ 1
My solution works.
24th Oct 2022, 12:59 AM
Dragon RB
Dragon RB - avatar
+ 1
Tibor Santa Oh yeah, i almost forgot to thank you for the solution. Its useful.
24th Oct 2022, 3:51 AM
Dragon RB
Dragon RB - avatar
0
Tibor Santa I think i found a solution. I print "Yes" everytime i find a duplicated letter in a list, otherwise I print "No". When I input "Hello", it would look like this: ["No","No",No","Yes","No"] If there is atleast 1 "Yes" in a list, it should print Deja Vu, if there a no "Yes" in the list, it should print "Unique".
24th Oct 2022, 12:18 AM
Dragon RB
Dragon RB - avatar
0
I am glad it helped. We all think differently about solving a problem. Now that you managed to make it work, I can share a simple trick for this. The input string is a collection of letters, which has a specific length. If you convert it to a set of characters, all duplicate letters will be removed, because a set data structure can contain only unique values. Then you can compare the size of the original string with the size of the set, using the len() function, and if they are the same, there were no duplications.
24th Oct 2022, 3:56 AM
Tibor Santa
Tibor Santa - avatar
0
Tibor Santa If then, can I ask you a silly question? How do you add every character in the input to a set? Hmm
24th Oct 2022, 4:30 AM
Dragon RB
Dragon RB - avatar
0
Dragon RB Creating a set can be done multiple ways, simplest one is to use the set() function and the parameter can be any iterable object (including a string). You could also use a comprehension, or declare an empty set and add each element one by one inside a loop https://code.sololearn.com/cYiM5ZvRV2vZ/?ref=app
24th Oct 2022, 4:53 AM
Tibor Santa
Tibor Santa - avatar