+ 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
13 Antworten
+ 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.
+ 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
+ 6
if any(input_sentence.count(letter)>=2 for letter in input_sentence):
print("Deja Vu")
else:
print("Unique")
+ 5
Happy to know that my answer helped someone , thanks for your information 😄
+ 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
+ 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
+ 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.
+ 1
Ipang ohhh....yeah, thank you bro for informing me that😁
+ 1
Tomiwa Joseph Woah! Thank you for giving more than just a correction 😃😊
0
Ipang Thank you for answering 😊
0
Proলয় मिश्ra I don't know who downvoted your comment, but thank you for answering, it works😊
0
Lothar thank you for answering 😊
- 1
Julia Shabanova Thank you for answering, but your code works only when there is a break statement 😄