+ 2

Code algorithm and optimization

Is a dictionary object like a hashtable the best way to do this? https://code.sololearn.com/cMGCFjL8JWaL/?ref=app

1st Jan 2022, 5:28 PM
Sanjay Kamath
Sanjay Kamath - avatar
5 Réponses
+ 1
Sorry, I couldn't immediately decipher your intended algorithm. It is a massive violation of the KISS principle, so I would say no, it is not the best way to do this. Go with simpler, like these lines that do direct calculation: string s = Console.ReadLine().ToLower(); //convert alpha chars to backward alphabet foreach (char ch in s) { Console.Write(Char.IsLetter(ch) ? (char)('z'-ch+'a') : ch); }
2nd Jan 2022, 1:17 AM
Brian
Brian - avatar
+ 2
Brian If you want to customise this for random assignment then I think that what I did is correct
2nd Jan 2022, 11:41 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Brian What if some of the keys are uppercase and some lowercase....???
2nd Jan 2022, 3:29 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
In that case, yes, a lookup table is better. For my taste, the KISS approach would use a simple array. char[] t = "qwertyuiopasdfghjklzxcvbnm".ToCharArray(); string s = Console.ReadLine().ToLower(); //convert alpha chars to jumbled alphabet foreach (char ch in s) { Console.Write(Char.IsLetter(ch) ? t[ch-'a'] : ch); }
3rd Jan 2022, 1:18 AM
Brian
Brian - avatar
0
Sanjay Kamath, just like your original code, string s is made all lowercase by use of the ToLower() method in the input line.
2nd Jan 2022, 6:02 PM
Brian
Brian - avatar