Check if a sentence is a pangram

in #programming3 years ago

I've seen this post, Learning GoLang with me: Check if the Sentence Is Pangram, by justyy — I'm always searching for programming posts, after all. More than music posts — but I'm preparing something about this topic, too.

That algorithm is bugged: it just counts the different “runes” (characters in Go parlance), and if they are as much as the letters in the English alphabet (26), it says it is a pangram.

But you can fool it easily: for instance, the following is a pangram according to his algo:

!$%cdefgh&/jklmn()qrs=?'[]

And since it considers lowercase and uppercase letters as different, according to that algorithm the following is not a pangram:

thequickbrownfoxjumpsoverThelazydog

Now, this is the kind of task I like to do in oneliners. Go is a great language, I like it very much, but one-liners aren't its thing.

Perl, Raku and Ruby are a better fit for one-liners.

The true algorithm

We must check if the sentence contains not just 26 random characters, but the 26 letters of the alphabet, at least once.

if you want to see how it can be done in Go, without bugs, check out here — this is a wonderful blog: Pangram checker in Go.

The very same site gives also a possible one-liner in ruby:

('a'..'z').all? {|chars| "writeherethesentence".downcase.include? (chars) }

If we run it with the classical sentence where I've replaced a lowercase t with T, we obtain true, as it should be:

irb(main):002:0> ('a'..'z').all? {|chars| "thequickbrownfoxjumpsoverThelazydog".downcase.include? (chars) }
=> true

That's it.

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.032
BTC 60832.04
ETH 2902.69
USDT 1.00
SBD 3.55