0

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.

Dezmond H
  • 25
  • 7
  • This kind of questions is better suited for [codereview.se]. – Filburt Nov 28 '17 at 07:00
  • I would say it is off-topic because you are not asking about a precise programming problem but how to be smarter in coding. This is something that none can simply explain in an answer and any site pretending to do so are just clickbaiting you. You can solve this only with a constant effort to learn. – Steve Nov 28 '17 at 07:10
  • It is ironic that the highest rated answer does not meet the specification's return type requirement. – Tom Blodget Nov 28 '17 at 17:42
  • @Filburt - I didn't know about the Code Review. I'll keep that in mind in the future when asking. Thank you. – Dezmond H Nov 28 '17 at 22:53
  • @Steve - Thank you commenting. What if I edit my question to ask for the meaning of the '(_ => _ > 64 && _ < 91) / 26' code written by Slomo? It would then – Dezmond H Nov 28 '17 at 22:57
  • Well sure it will be better for the purpose of SO but I am afraid that also asking to explain a documented functionality is not the best question to ask and could possibly attract downvotes. – Steve Nov 28 '17 at 23:05
  • @ Steve - I've edited my question to ask how Slomo is able to return that integer (either 0 or 1) without using the `return` keyword. – Dezmond H Nov 28 '17 at 23:23

2 Answers2

0

Concise code isn't always something to strive for. Realistically, the sample you provided, the highly rated one, isn't what I would call concise. I would call it terse, and I might even call it incorrect.

Being pedantic, a more correct version of the provided sample would be:

bool isPangram(string s) => s.ToUpper().Distinct().Count(_ => _ >= 'A' && _ <= 'Z') == 26;

As to what the sample does, there are a few steps:

  1. .ToUpper() converts the input string to upper case.
  2. .Distinct() converts the string into a discrete list of characters (each character value that appears in the string will appear in the list once).
  3. .Count(_ => _ > 64 && _ < 91) returns the number of elements in the list that are greater than 64 (the ASCII code for the letter A is 65) and less than 91 (the ASCII code for the letter Z is 90).
  4. / 26 will return 1 if there are 26 elements in the final list (there are 26 letters in the (English) alphabet), and 0 if there are less than 26.
  5. The values 0 and 1 will automatically cast to boolean false and true respectively.

As to how to write code more concisely, it will come with time and practice. Knowing the path of least resistance to your goal is the first step, then knowing all the ins and outs of your chosen language will help you find the easiest way to implement the easiest solution.

Hope that helps.

user5151179
  • 575
  • 2
  • 10
  • Thank you for taking the time to answer. I'm somewhat self-taught, so when I first saw it I was worried that I had missed an important lesson on coding principles. – Dezmond H Nov 28 '17 at 22:52
0

"How is Slomo's code returning a value without using the return keyword?"

It uses C#. It declares an expression-bodied function. Here is an old article on that C# 6.0 Gets More Concise…

And, from the specification for methods:

An expression body consists of => followed by an expression and a semicolon, and denotes a single expression to perform when the method is invoked.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • I can't upvote yet, and I already accepted an answer, but I have to thank you for your answer, as well as the resources you linked to. This is exactly what I was looking for and then some. – Dezmond H Dec 15 '17 at 00:41