I'm a beginner coding on a site called CodeFights and I noticed that many of the higher rated coders would have much more concise answers than me.
Take this problem for instance:
Given a sentence, check whether it is a pangram or not.
Example:
For sentence = "The quick brown fox jumps over the lazy dog.", the output should be isPangram(sentence) = true;
For sentence = "abcdefghijklmnopqrstuvwxya", the output should be isPangram(sentence) = false.Input/Output
- [time limit] 3000ms (cs)
- [input] string sentence
A string containing characters with their ASCII-codes in the range [32, 126].
Guaranteed constraints:
1 ≤ sentence.length ≤ 100.
- [output] boolean
true if sentence is a pangram, false otherwise.[C#] Syntax Tips
// Prints help message to the console // Returns a string string helloWorld(string name) { Console.Write("This prints to the console when you Run Tests"); return "Hello, " + name; }
My code is:
bool isPangramMine(string sentence)
{
char[] stringToCharArray = sentence.ToLower().Distinct().ToArray();
Array.Sort(stringToCharArray);
string joinedkArrayToCheck = String.Join("", stringToCharArray);
MatchCollection matches = Regex.Matches(joinedkArrayToCheck, "[a-z]");
string[] stringMatchArray = new string[matches.Count];
for (int i = 0; i < matches.Count; i++)
stringMatchArray [i] = matches[i].Groups[0].Value;
string joinedArrayToString = String.Join("", stringMatchArray );
Console.WriteLine(joinedArrayToString );
if (joinedArrayToString .Length == 26)
return true;
else
return false;
}
The highest rated C# answer was written by someone named Slomo. That answer is:
int isPangram(string s) => s.ToUpper().Distinct().Count(_ => _ > 64 && _ < 91) / 26;
How is Slomo's code returning a value without using the return
keyword?
I've seen answers similar to this here and here, but they only have the concise code without explaining how it works.
I'm looking for an answer that points me to a resource that I can use break down Slomo's code on my own, but someone taking the time to explain it themselves would be greatly appreciated as well.