🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of remcopeereboom

remcopeereboom's solution

to Pangram in the Ruby Track

Published at Jul 13 2018 · 9 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.


For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby pangram_test.rb

To include color from the command line:

ruby -r minitest/pride pangram_test.rb

Source

Wikipedia https://en.wikipedia.org/wiki/Pangram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

pangram_test.rb

require 'minitest/autorun'
require_relative 'pangram'

# Common test data version: 1.3.0 d79e13e
class PangramTest < Minitest::Test
  def test_sentence_empty
    # skip
    phrase = ''
    result = Pangram.pangram?(phrase)
    refute result, "Expected false, got: #{result.inspect}. #{phrase.inspect} is NOT a pangram"
  end

  def test_recognizes_a_perfect_lower_case_pangram
    skip
    phrase = 'abcdefghijklmnopqrstuvwxyz'
    result = Pangram.pangram?(phrase)
    assert result, "Expected true, got: #{result.inspect}. #{phrase.inspect} IS a pangram"
  end

  def test_pangram_with_only_lower_case
    skip
    phrase = 'the quick brown fox jumps over the lazy dog'
    result = Pangram.pangram?(phrase)
    assert result, "Expected true, got: #{result.inspect}. #{phrase.inspect} IS a pangram"
  end

  def test_missing_character_x
    skip
    phrase = 'a quick movement of the enemy will jeopardize five gunboats'
    result = Pangram.pangram?(phrase)
    refute result, "Expected false, got: #{result.inspect}. #{phrase.inspect} is NOT a pangram"
  end

  def test_another_missing_character_eg_h
    skip
    phrase = 'five boxing wizards jump quickly at it'
    result = Pangram.pangram?(phrase)
    refute result, "Expected false, got: #{result.inspect}. #{phrase.inspect} is NOT a pangram"
  end

  def test_pangram_with_underscores
    skip
    phrase = 'the_quick_brown_fox_jumps_over_the_lazy_dog'
    result = Pangram.pangram?(phrase)
    assert result, "Expected true, got: #{result.inspect}. #{phrase.inspect} IS a pangram"
  end

  def test_pangram_with_numbers
    skip
    phrase = 'the 1 quick brown fox jumps over the 2 lazy dogs'
    result = Pangram.pangram?(phrase)
    assert result, "Expected true, got: #{result.inspect}. #{phrase.inspect} IS a pangram"
  end

  def test_missing_letters_replaced_by_numbers
    skip
    phrase = '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'
    result = Pangram.pangram?(phrase)
    refute result, "Expected false, got: #{result.inspect}. #{phrase.inspect} is NOT a pangram"
  end

  def test_pangram_with_mixed_case_and_punctuation
    skip
    phrase = '"Five quacking Zephyrs jolt my wax bed."'
    result = Pangram.pangram?(phrase)
    assert result, "Expected true, got: #{result.inspect}. #{phrase.inspect} IS a pangram"
  end

  def test_upper_and_lower_case_versions_of_the_same_character_should_not_be_counted_separately
    skip
    phrase = 'the quick brown fox jumps over with lazy FX'
    result = Pangram.pangram?(phrase)
    refute result, "Expected false, got: #{result.inspect}. #{phrase.inspect} is NOT a pangram"
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 6, BookKeeping::VERSION
  end
end
module Enumerable
  def include_all?(elements)
    elements.all? { |e| include? e }
  end
end

module Pangram
  VERSION = 1
  ALPHABET = 'a'..'z'

  class << self
    def pangram?(sentence)
      sentence.downcase.each_char.include_all?(ALPHABET)
    end
    alias is_pangram? pangram?
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of remcopeereboom

A more generalized solution.

Avatar of kotp

Would reducing the enumerated character collection to the unique characters reduce the number of things to iterate? Never mind, saw the other comment.

Avatar of durrellchamorro

Why is this test written with is_pangram? Rubocop recommends renaming is_pangram? to pangram? The fact that you are using an alias here makes me think you agree with Rubocop.

Avatar of remcopeereboom

Why is this test written with is_pangram? Rubocop recommends renaming is_pangram? to pangram? The fact that you are using an alias here makes me think you agree with Rubocop.

I didn't want to submit a solution that failed, but I also feel that methods in ruby should not have prefixes. This is actually a remark by Matz and is why we can de Hash#has_key? and Hash#key? (the former being left in for backwards compatibility - way back backwards compatability).

Avatar of kotp

Sometimes we don't have choices when we name our methods. The "Ruby way" is sometimes how it is now, as opposed to how it has been. Or it is some "API" that has to agree with some other language.

Avatar of Insti

Edit: I'm wrong about all this, see apology below.

I really don't like the surprising backwards logic of include_all?

Given this is how include? works: array.include?(value)

I would expect include_all? to work like this: array.include_all?(values)

But that's not what it does.

Perhaps the solution is to rename the method.

Avatar of remcopeereboom

@Insti

I would expect include_all? to work like this: array.include_all?(values) But that's not what it does.

I'm not sure what you mean. Surely it does work like that? Do you mean that it does not take a variable argument list? You could do something like this: module Enumerable def include_all?(*args) if args[0].is_a? Enumerable args[0].all? { |e| include? e } else args.all? { |e| include? e } end end end

But you would run into problems if you want to test if a collection includes another collection. There's no real way around that except for having different methods for each. You could do something more complicated by checking if you have only 1 value, but that still does not really solve the problem. I just went for the easiest I could think of (you can always pass values as an array).

Let me know if I misunderstood.

Avatar of rbndickson

I like this solution but the include_all? method is a little tricky at first glance due to:

elements refers to the elements passed in rather than the elements of self. include? is being called on self implicitly and the include? method is not visible as it is within the original Enumerable module.

Avatar of Insti

@remcopeereboom

I said:

I would expect include_all? to work like this: array.include_all?(values)

Sorry, I was mistaken. I guess I thought it was doing values.include_all?(array) But it is obvious when I look at it today that it is working as expected.

If I had been more clear in the description of my objection I probably would have discovered this at the time.

Thanks for your alternate explanation anyway.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?