Categories
Exercism String manipulation

[Exercism] – Pangram

I have found another coding website where you can solve challenges in 48 languages. Besides that, there are mentors that check your solutions and provide you feedback. You can check more about Exercism here. I will use Exercism alongside Coderbyte. The problem description below is taken from Exercism:

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 first step I am taking to solve this problem is to transform the sentence into lowercase. The reason for doing so is that uppercase letters are different than lowercase letters (e.g. ‘A’ is not the same as ‘a’ regarding ASCII codes). That can easily be done by calling Python’s lower() method on the given sentence.

def is_pangram(sentence):
    # transform the sentence to lowercase
    sentence = sentence.lower()

For the next step, we have three choices. The first choice is to create manually a string containing the alphabet. The second choice is to use a generator/list comprehension (the difference between the two here – link) to generate the alphabet in lowercase letters based on their ASCII code. The last option is to use Python’s method string.ascii_lowercase which returns the alphabet in lowercase letters.

For this solution, I have decided to use a list comprehension. This list stores all the ASCII codes from 97 to 123 (which represents the letters a-z). To get the letter from the code, we use Python’s method chr() for each ASCII code. In the end, the list contains the alphabet letters in lowercase. That is done in one line (line 6).

def is_pangram(sentence):
    # transform the sentence to lowercase
    sentence = sentence.lower()

    # generate the (lowercase) alphabet from ASCII codes
    alphabet = [chr(code) for code in range(97, 123)]

The last step is to check whether the given sentence contains all the letters from the alphabet. One way to do it is to check if the alphabet is a subset of the sentence. On the other side, we can loop over the alphabet and check if each letter from the alphabet is found in the sentence. The former one is shorter.

issubset() returns a Boolean value stating whether the set is contained in the specified set or iterable. (Python Docs) The below code returns true because ‘a’, ‘z’ and ‘b’ are contained in the second set. Change the letter ‘b’ to ‘1’, and it will return false.

{'a', 'z', 'b'}.issubset({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'})

The first solution can be seen below:

def is_pangram1(sentence):
    # transform the sentence to lowercase
    sentence = sentence.lower()
 
    # generate the (lowercase) alphabet from ASCII codes
    alphabet = [chr(code) for code in range(97, 123)]
 
    return set(alphabet).issubset(set(sentence))

The second solution iterates over the alphabet and checks if each letter from alphabet is found in the sentence. If one letter from the alphabet is not found in the given sentence, it returns false.

def is_pangram(sentence):
    # transform the sentence to lowercase
    sentence = sentence.lower()

    # generate the (lowercase) alphabet from ASCII codes
    alphabet = [chr(code) for code in range(97, 123)]

    # if not every letter from the alphabet is in the sentence,
    #   return false
    for char in alphabet:
        if char not in sentence:
            return False

    return True

The Repl link with the solutions and issubset() examples: Repl.it.

Advertisements

Leave a Reply