+ 2

What's the missing part in my code below?

This is related to Deja Vu problem. I'm getting "Unique" even if a string is repeated. Please help input_sentence = input() for letter in input_sentence: if input_sentence.count(letter)>=2: print("Deja Vu") else: print("Unique") break

1st Aug 2020, 6:31 AM
psprises
psprises - avatar
14 Respostas
+ 10
A different approach input_sentence = input() or "hello" status = "Unique" for letter in input_sentence: if input_sentence.count(letter)>=2: status = "Deja Vu" break print(status) # alternatively unique_letters = set(input_sentence) # make a `set` containing unique letters from original input print("Unique" if len(input_sentence) == len(unique_letters) else "DeJa Vu") # compare original input length with the `set` length. Difference in size means there were duplicate.
1st Aug 2020, 6:59 AM
Ipang
+ 7
Here is an other approach: No loop is used, but just a conparison of original input string and a set of input string (1) take your input string and get the length of it (2) use the same input string and make it a set and get also the length of it (3) if both length values are identical, the input string does not contain duplicated characters (4) if both length values are different, the string contains duplicatet characters
1st Aug 2020, 9:58 AM
Lothar
Lothar - avatar
+ 6
if any(input_sentence.count(letter)>=2 for letter in input_sentence): print("Deja Vu") else: print("Unique")
1st Aug 2020, 6:59 AM
Julia Shabanova
Julia Shabanova - avatar
+ 5
Happy to know that my answer helped someone , thanks for your information 😄
1st Aug 2020, 7:27 AM
Bot
Bot - avatar
+ 3
this might work: input_sentence = input() for letter in input_sentence: if input_sentence.count(letter) >1 : i=0 break else: i=1 print ("unique" if i==1 else "deja vu") edit: to whoever downvoter , why did you downvote me?? i was just trying to help him
1st Aug 2020, 6:56 AM
Bot
Bot - avatar
+ 3
Pendem Shiva Prakash continue is not used as often as break but it is essential in a loop that needs it. Check out the correction and other approaches...😃 https://code.sololearn.com/cka7kr8GdfM7/?ref=app
1st Aug 2020, 7:39 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Pendem Shiva Prakash You're welcome 👌 Julia's code doesn't need `break` statement bro. In fact, it doesn't need to be put inside a loop at all.
1st Aug 2020, 7:19 AM
Ipang
+ 1
Ipang ohhh....yeah, thank you bro for informing me that😁
1st Aug 2020, 7:24 AM
psprises
psprises - avatar
+ 1
Tomiwa Joseph Woah! Thank you for giving more than just a correction 😃😊
1st Aug 2020, 10:20 AM
psprises
psprises - avatar
0
Ipang Thank you for answering 😊
1st Aug 2020, 7:11 AM
psprises
psprises - avatar
0
ProàŠČàŠŻàŠŒ à€źà€żà€¶à„ra I don't know who downvoted your comment, but thank you for answering, it works😊
1st Aug 2020, 7:25 AM
psprises
psprises - avatar
0
Lothar thank you for answering 😊
1st Aug 2020, 10:22 AM
psprises
psprises - avatar
- 1
Julia Shabanova Thank you for answering, but your code works only when there is a break statement 😄
1st Aug 2020, 7:12 AM
psprises
psprises - avatar