Avatar of avinash-anand

avinash-anand's solution

to Pangram in the Scala Track

Published at Oct 01 2019 · 0 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help 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.

PangramTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.4.0 */
class PangramTest extends FunSuite with Matchers {

  test("sentence empty") {
    Pangrams.isPangram("") should be (false)
  }

  test("recognizes a perfect lower case pangram") {
    pending
    Pangrams.isPangram("abcdefghijklmnopqrstuvwxyz") should be (true)
  }

  test("pangram with only lower case") {
    pending
    Pangrams.isPangram("the quick brown fox jumps over the lazy dog") should be (true)
  }

  test("missing character 'x'") {
    pending
    Pangrams.isPangram("a quick movement of the enemy will jeopardize five gunboats") should be (false)
  }

  test("another missing character, e.g. 'h'") {
    pending
    Pangrams.isPangram("five boxing wizards jump quickly at it") should be (false)
  }

  test("pangram with underscores") {
    pending
    Pangrams.isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog") should be (true)
  }

  test("pangram with numbers") {
    pending
    Pangrams.isPangram("the 1 quick brown fox jumps over the 2 lazy dogs") should be (true)
  }

  test("missing letters replaced by numbers") {
    pending
    Pangrams.isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog") should be (false)
  }

  test("pangram with mixed case and punctuation") {
    pending
    Pangrams.isPangram(""""Five quacking Zephyrs jolt my wax bed."""") should be (true)
  }

  test("upper and lower case versions of the same character should not be counted separately") {
    pending
    Pangrams.isPangram("the quick brown fox jumps over with lazy FX") should be (false)
  }
}
object Pangrams {

  private val listOfAlphabets: Seq[Char] = 'a' to 'z'

  def isPangram2(input: String): Boolean = {
    val lower = input.toLowerCase
    input.nonEmpty && listOfAlphabets.forall(x => lower.contains(x))
  }

  def isPangram(input: String): Boolean = input.toLowerCase.filter(_.isLetter).distinct.length == 26

}

Community comments

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

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?