Avatar of JaeHyoLee

JaeHyoLee's solution

to Pangram in the Lua Track

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

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.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

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_spec.lua

local is_pangram = require('pangram')

describe('pangram', function()
  it('should not consider the empty string to be a pangram', function()
    assert.is_false(is_pangram(''))
  end)

  it('should not consider a sentence with 26 of the same letter to be a pangram', function()
    assert.is_false(is_pangram(string.rep('a', 26)))
  end)

  it('should consider the alphabet to be a pangram', function()
    assert.is_true(is_pangram('abcdefghijklmnopqrstuvwxyz'))
  end)

  it('should consider a sentence with one of each letter to be a pangram', function()
    assert.is_true(is_pangram('the quick brown fox jumps over the lazy dog'))
  end)

  it('should allow mixed case in pangrams', function()
    assert.is_true(is_pangram('The Quick Brown Fox Jumps Over The Lazy Dog'))
  end)

  it('should allow punctuation in pangrams', function()
    assert.is_true(is_pangram('the.quick.brown.fox.jumps.over.the.lazy.dog'))
  end)

  it('should allow underscores in pangrams', function()
    assert.is_true(is_pangram('the_quick_brown_fox_jumps_over_the_lazy.dog'))
  end)

  it('should allow numbers in pangrams', function()
    assert.is_true(is_pangram('the 1 quick brown fox jumps over the 2 lazy dogs'))
  end)

  it('should not allow letters in pangrams to be replaced by numbers', function()
    assert.is_false(is_pangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'))
  end)

  it('should not consider a sentence with only 25 unique letters to be a pangram', function()
    assert.is_false(is_pangram('a quick movement of the enemy will jeopardize five gunboats'))
    assert.is_false(is_pangram('the quick brown fish jumps over the lazy dog'))
  end)

  it('should allow non-ASCII characters in pangrams', function()
    assert.is_true(is_pangram('Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.'))
  end)

  it('should not consider non-ASCII characters to be letters', function()
    assert.is_false(is_pangram('äbcdefghijklmnopqrstuvwxyz'))
  end)
end)
return function(s)
  local normalized = s:gsub('%A',''):lower()

  local t = table.pack(normalized:byte(1, #normalized))
  table.sort(t)

  local prev
  local index = 0

  for k = 1, #t do
    if (prev or true) and prev ~= t[k] then
      if t[k] ~= index + string.byte('a') then
        return false
      else
        index = index + 1
      end
    end
    prev = t[k]
  end

  return (index == 26 and true or false)
end

Community comments

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

inspired by the @ryanplusplus 's anagram solution :)

Avatar of ryanplusplus

For your return, I think you can simplify to just return (index == 26)

Avatar of ryanplusplus

I think you can simplify the core logic a bit by taking advantage of the fact that t has been sorted. Try just counting the number of times that the current value is greater than the previous to get the number of unique values.

I did this and I was able to simplify lines 7-19 down to just one local variable for the count, one for loop, and one if (with no else) in just 4 lines. See if you can beat me :)

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?