+ 5

I have tried to solve this code coach problem of Deja vu in python. Correct me with my code.

https://www.sololearn.com/coach/54?ref=app You aren't paying attention and you accidentally type a bunch of random letters on your keyboard. You want to know if you ever typed the same letter twice, or if they are all unique letters. Task: If you are given a string of random letters, your task is to evaluate whether any letter is repeated in the string or if you only hit unique keys while you typing. Input Format: A string of random letter characters (no numbers or other buttons were pressed). # code goes from here strng = input() count = {} for i in strng: if i in count: count[i] += 1 else: count[i] = 1 for s in count: if count[s] == 1: print ("Unique") elif count[s] > 1: print ("Deja Vu") break

31st Jan 2021, 10:05 AM
thandava krishna
thandava krishna - avatar
35 Respuestas
+ 9
s = input() c = 0 for l in s: if s.count(l) > 1: c += s.count(l) c -= int(c/2) if c > 1: print('Deja Vu') else: print('Unique') this will work
1st Feb 2021, 3:57 AM
Yusuf M Hashmi
+ 6
#DIY test in_str = list(str(input())) str_len = len(in_str) set_letters = set(in_str) set_len = len(set_letters) if str_len == set_len: print("Unique") else: print("Deja Vu")
2nd Feb 2021, 12:08 AM
Sadokatkhon Abdukhalilova
Sadokatkhon Abdukhalilova - avatar
+ 2
TASK: If you are given a string of random letters, your task is to evaluate whether any letter is repeated in the string or if you only hit unique keys while you typing. Input Format: / A string of random letter characters (no numbers or other buttons were pressed). ==MY ANSWER IN JAVA PROGRAMMING=== import java.util.*; public class Program { public static void main(String[] args) { Scanner ip=new Scanner(System.in); String name=ip.nextLine(); char[] c=name.toCharArray(); int count=0; for(int i=0;i<c.length;i++) { for(int j=i+1;j<c.length;j++) { if(c[i]==c[j]) { count++; } } } if(count==0) System.out.print("Unique"); else System.out.print("Deja Vu"); } }
23rd May 2021, 5:08 PM
Maruthamuthu.MS
Maruthamuthu.MS - avatar
0
its the line: "for s in count:" count, i beleive, is a dictionary. It has key and value pairs that need to be seperated. only_one = True for key, value in count.items(): if value > 1: print("DejaVu") only_one = False break if only_one: print("Unique")
31st Jan 2021, 10:15 AM
Slick
Slick - avatar
0
Actually the code is working in this way but the problem is it's giving only the output as 'unique', even where it suppose to give the output as 'Deja vu' that's the problem here with this code.
31st Jan 2021, 10:31 AM
thandava krishna
thandava krishna - avatar
0
So it working or its not working?
31st Jan 2021, 10:32 AM
Slick
Slick - avatar
0
I will post the problem here
31st Jan 2021, 10:35 AM
thandava krishna
thandava krishna - avatar
0
You aren't paying attention and you accidentally type a bunch of random letters on your keyboard. You want to know if you ever typed the same letter twice, or if they are all unique letters. Task: If you are given a string of random letters, your task is to evaluate whether any letter is repeated in the string or if you only hit unique keys while you typing. Input Format: A string of random letter characters (no numbers or other buttons were pressed). Output Format: A string that says 'Deja Vu' if any letter is repeated in the input string, or a string that says 'Unique' if there are no repeats. Sample Input: aaaaaaaghhhhjkll Sample Output: Deja Vu
31st Jan 2021, 10:35 AM
thandava krishna
thandava krishna - avatar
0
That's not needed as the code isn't difficult. I saw and explained your problem in the first post. You created a dictionary, then didn't access it the right way
31st Jan 2021, 10:36 AM
Slick
Slick - avatar
0
Try running my code
31st Jan 2021, 10:36 AM
thandava krishna
thandava krishna - avatar
0
How to do it then
31st Jan 2021, 10:37 AM
thandava krishna
thandava krishna - avatar
0
Dont need to yours wont work, just delete your code from the line I mentioned and put the code I supplied in.
31st Jan 2021, 10:38 AM
Slick
Slick - avatar
0
K i will try
31st Jan 2021, 10:39 AM
thandava krishna
thandava krishna - avatar
0
Same output, no change
31st Jan 2021, 11:39 AM
thandava krishna
thandava krishna - avatar
0
post the code. And not just text, put it in the playground and link it here
31st Jan 2021, 11:40 AM
Slick
Slick - avatar
0
Thank you it worked. But, i can't understand the logic can you explain that
31st Jan 2021, 11:53 AM
thandava krishna
thandava krishna - avatar
0
only_one = True # keeps track of if there are duplicates # when you have a dictionary and call <dict>.items() it gives a list of tuples. In each tuple, the first value is the key and the second is the value for key, value in count.items(): # this check will print a message if there ARE duplicates if value > 1: print("DejaVu") # if duplicates, the duplicate tracker is set to False and then breaks out of loop only_one = False break # if duplicate tracker was not set to False, print the positive message saying it's a unique string if only_one: print("Unique")
31st Jan 2021, 11:58 AM
Slick
Slick - avatar
0
Only_one is a variable, right?
31st Jan 2021, 2:09 PM
thandava krishna
thandava krishna - avatar
0
And it is dealing with Boolean operations
31st Jan 2021, 2:09 PM
thandava krishna
thandava krishna - avatar
0
Yes, it's a variable using boolean True/False
31st Jan 2021, 2:11 PM
Slick
Slick - avatar