Tuesday, October 15, 2024
STEM

Pangram Verifier (Python)

Objective

Write a script that takes a user-input and checks if it’s a pangram. A pangram is a sentence/phrase/string that contains all letters of the alphabet (in this case, English) at least once. However, technically, a pangram doesn’t necessarily have to be a grammatically correct sentence. The goal of a pangram is to include every letter of the alphabet at least once. Nevertheless, creating a grammatically correct and meaningful pangram can be more memorable, challenging and interesting.

In an earlier post, I shared another program that creates Pangram on the fly from a dictionary of English words. See Related section below to check it out.

Some example pangrams (grammatically correct sentences)

A quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over a lazy dog.
Two driven jocks help fax my big quiz.
My girl wove six dozen plaid jackets before she quit.
Dr. Zhivago saw the X-ray before jumping quickly.
The lazy dog and the quick brown fox jumped over the fences.
Mr. Jock, TV quiz PhD, bags few lynx.
Pack my box with five dozen liquor jugs. (from Copilot)
The five boxing wizards jump quickly. (from Copilot)
How vexingly quick daft zebras jump!
Bright vixens jump; dozy fowl quack.
Sphinx of black quartz, judge my vow.
Jackdaws love my big sphinx of quartz.
Waltz, nymph, for quick jigs vex Bud.
Glib jocks quiz nymph to vex dwarf.
Jinxed wizards pluck ivy from the big quilt.
Crazy Fredrick bought many very exquisite opal jewels.
Amazingly few discotheques provide jukeboxes.

Solution

We'll use set objects to convert a string to set of individual letters. Sets can be used for addition and subtraction. A set automatically removes duplicates from a string and retains each character of the string as a separate element. 
We'll Use .join() to populate the list of missing letters, use sorted() to sort them for output. With set(string.ascii_lowercase) we can create a set containing all English letters in the alphabet without duplicates.

What's special about set objects:
  1. Uniqueness: Sets in Python only contain unique elements. Duplicate values are automatically removed. This makes sets useful for tasks where you need to keep track of distinct items.
  2. Unordered: Unlike lists or tuples, sets are unordered. The order of elements doesn’t matter, which can be advantageous for certain algorithms or data structures.
  3. Membership Testing: Checking whether an element is in a set is very efficient. Sets use hash-based indexing, so membership tests are faster than searching through a list.
  4. Set Operations: Sets support various operations like union, intersection, and difference.
    For example:
    A|B (union): Combines elements from sets A and B.
    A&B (intersection): Finds common elements between sets A and B.
    A-B (difference): Removes elements from set B in set A. WARNING: set A’s number of elements must be >=B otherwise the result may be incorrect.
    The difference always returns elements in the first set but NOT in the second set.
    A^B (symmetric difference): A^B gives elements present in either set but NOT both.
    So, if set A = {“apple”, “banana”, “cherry”} and set B = {1, 2, “apple”} then A^B = {1, 2, ‘cherry’, ‘banana’} [because “apple” exists in both sets]
  5. Immutable Elements: While a set itself can be modified, the elements within it must be of an immutable type (e.g., strings, numbers, or tuples).

We’ll take it a step further by also showing in the output the letters that are missing in a user-input if the input is not a pangram. That way, we can clearly see why the input did not qualify as a pangram, and thereby help create a well-formed pangram. This is done by Set subtraction, that is, if we subtract the user-input from the full alphabet set as stated above, and the result is not zero, then there are letters missing and the subtraction result set will contain the letters that are missing. Deceptively simple!

The Code

I hope this was educational and interesting. You can get the full source code from my Patreon shop here (secure transaction).

Try It Now

You can run the script right here from this below. Just click Run on the widget below and follow the instructions. Can you create some of your own pangrams?


Related:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
+