write a program that detects if a sentence is a pangram in ruby

To write a program that detects if a sentence is a pangram in Ruby, you can use the following code:

main.rb
def is_pangram?(sentence)
  alphabet = ('a'..'z').to_a
  sentence.downcase.chars.uniq.sort == alphabet
end

# Example usage
puts is_pangram?("The quick brown fox jumps over the lazy dog") #=> true
puts is_pangram?("This sentence is not a pangram") #=> false
258 chars
9 lines

Explanation:

  1. The is_pangram? function takes a sentence as an input parameter.
  2. We first create an array alphabet containing all the lowercase letters of the English alphabet.
  3. We then convert the sentence to lowercase using .downcase and convert it to an array of characters using .chars.
  4. Next, we remove any duplicate characters from the sentence array using .uniq and sort the array using .sort.
  5. Finally, we check if the sorted array is equal to the alphabet array. If they are equal, then the sentence is a pangram.

The above code will return true if the sentence is a pangram and false otherwise.

Please note that this implementation assumes the input sentence contains only valid English letters and no special characters or numbers.

gistlibby LogSnag