+ 1

Please rate my code and how I can improve

I am new to coding so they aren’t many commands I know how to use. Still, I want to try C# coding “Deja Vu” Challenge and it took me quite long to solve it (due to some dumb error lol). Can you guys please rate my code and comment on how I can improve it? Thank you in advance https://code.sololearn.com/cB1XA7EPpLWG/?ref=app

10th Jun 2023, 2:19 PM
Papidopulas
Papidopulas - avatar
11 RĂ©ponses
+ 6
Hi, great, you've done a code! If you want to promote it, here are the threads to do it: https://www.sololearn.com/discuss/1496027/?ref=app https://www.sololearn.com/discuss/475577/?ref=app
10th Jun 2023, 2:39 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Nice job, the only real comment I have is that C# also has "foreach", which will make your loops much more readable. But I also think it's better to have done this the old C way at least once, first, so 👍
10th Jun 2023, 6:40 PM
Orin Cook
Orin Cook - avatar
+ 2
(also, you could replace the whole count business by simply returning out of main after deja vu, but that's not going to be a good solution in every situation, so again I think better to have done it this way first)
10th Jun 2023, 6:45 PM
Orin Cook
Orin Cook - avatar
+ 2
Thank you!, I’ll be sure to try that in the future But if you dont mind can you give me a brief detail of how “foreach” works? And also I’m pretty sure i saw a command “for i in range” somewhere Not sure if that from C# or not If they can be use in C# does it work the same as they were use in Python?
10th Jun 2023, 8:58 PM
Papidopulas
Papidopulas - avatar
+ 2
For example, in this context you could say: foreach(char letter in input){ foreach(char repeat in input){ if(letter == repeat){ ....etc } } } the only catch is you can't do it naively in this case, you'd need to do something extra to avoid the i==j case, so thinking about it further it may not be easier in the long run. And yes, it works exactly the same as "for X in Y" in Python, BUT there's no "range" equivalent in C#, so in that specific analogy, you'd have to manually construct a list of numbers to iterate through beforehand.
10th Jun 2023, 9:29 PM
Orin Cook
Orin Cook - avatar
+ 2
Oh neat, thanks!
11th Jun 2023, 3:13 AM
Orin Cook
Orin Cook - avatar
+ 2
A different way of solving this problem, is using the set data structure. The characteristic of a set is that it only contains unique values. So if you can convert the input string, which is also of type IEnumerable<char> , to a HashSet<char>, then you can just compare their length. The simple version of this program: (I also used a ternary expression to determine the result, before writing it to console) string input = Console.ReadLine(); HashSet<char> unique = Enumerable.ToHashSet(input); string result = input.Length == unique.Count() ? "Unique" : "Deja Vu"; Console.WriteLine(result);
11th Jun 2023, 5:33 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Probably go for python next time you can choose whichever you want it's just I'm also new
11th Jun 2023, 7:49 PM
WaterNinja
WaterNinja - avatar
0
You can use the following line of code to check if there are repeated letter in a string using for(or foreach) loop(Make sure the var in foreach or for loop is a char type!): string n = takeInput.Split(x).Length-1; The line of code above allows you to check how many times each letter appear in a string..Where the Split() method is used to split a string to a type of array, the char (type) x here is used as parameter passed to the Split method, you can search up for it in google.. The code you provided is very similar to the first time I solve this code coach problem using Python, took me quite a long time actually.. Now it becomes really handy.. If you got stuck in a coding problem, please post questions here and link us to your code, we may help you!
12th Jun 2023, 2:10 AM
Dragon RB
Dragon RB - avatar
0
Patiphon Nophichai The `for x in range(a, b, (c))` is only valid in Python. It's used to set the value x to a, then we increment it by c and repeats until it reaches b. If the value c is not set, the default value of c is 1, and if the value a isnt set, the default value of a would be 0. Some example: for x in range(0,10,2): #x starts with 0 #Increase x by 2, now x is 2 #Increase x by 2, now x is 4 #... #Increase x by 2, now x is 8 #Loops break, because x is now 10. the following code repeats the code execute in the for loop 5(or 6) times. In C#, the foreach loop looks like for x in var in Python, where var isn't a digit or isn't a way of storing data types(like list,etc) foreach(char x in somestr) {} The following code will get each character in a string.(If you are using a foreach loop for a type that stores data, change the char to the correctsponding type of that data types.)
12th Jun 2023, 2:24 AM
Dragon RB
Dragon RB - avatar
0
Can I ask one thing, what does the Enumerable class do? Or.. What methods or property does it provide?
12th Jun 2023, 8:12 AM
Dragon RB
Dragon RB - avatar