A pangram is a sentence that contains every single letter of the alphabet at least once. For example, the sentence "The quick brown fox jumps over the lazy dog" is a pangram, because it uses the letters A-Z at least once (case is irrelevant). I'm trying to make a method that takes a string and returns true or false for if it is a pangram. This is what I tried so far.
def pangram?(string)
letters = string.chars.downcase.uniq
letters.uniq.all? {|c| string.count(c)==26}
end
def pangram?(string)
string.downcase
("a".."z").all?{|c| string.count(c) <= 1}
end
Any better suggestions? Thanks in advance!
You could go with something like:
s.downcase.scan(/[a-z]/).uniq.size == 26
This downcases the String scans for all characters "a" through "z" and checks that the uniq size of these characters equals 26.
Issues with your Current solutions
The first one will never work as is
chars
returns an Array
and Array#downcase
is not a methodstring.count(c)==26
) so 'a' * 26
will pass this test but "The quick brown fox jumps over the lazy dog" will not.The second has issues too:
String#count
is going to be inefficient;''
will pass this test as each letter occurs 0 times. e.g. <= 1
times.