Day 12 - Python

Pangram Checker

Problem Description:

A pangram is a sentence containing every letter of the alphabet at least once. Your task is to implement a function isPangram(s) which takes a string s as input and determines whether it is a pangram or not. If the input string is a pangram, print "Pangram! :)", otherwise print "No :(".

Function Signature:

def isPangram(s: str) -> bool:

    pass

Input:

A string s (1 <= |s| <= 10^3), where |s| denotes the length of string s. The string may contain both uppercase and lowercase English letters, as well as non-alphabetic characters.

Output:

Print "Pangram! :)" if the input string s is a pangram, otherwise print "No :(".

Constraints:

The input string may contain spaces, punctuation, or any other non-alphabetic characters.

You can assume that the input string will contain at least one alphabet character.

Examples:

Input:

The quick brown fox jumps over the lazy dog

Output:

Pangram! :)

Input:

Pangrams and Pancakes

Output:

No :(

Input:

abcdEFGHijKLmnOPqrSTuvWXyZ

Output:

Pangram! :)

You are required to implement the isPangram function and use it to print whether the input string is a pangram or not.

Some more examples.

Mr Jock, TV quiz PhD, bags few lynx

Waltz, bad nymph, for quick jigs vex!

Sphinx of black quartz, judge my vow

The five boxing wizards jump quickly

How razorback-jumping frogs can level six piqued gymnasts!

Jinxed wizards pluck ivy from the big quilt

Fickle jinx bog dwarves spy math quiz


SOLUTION:

def isPangram(s):
    s = sentence.lower()
    
    alphaSet = set()
    
    for i in s:
        if i.isalpha():
            alphaSet.add(i)
    
    if len(alphaSet) == 26:
        return True
    else:
        return False

sentence = input()

if isPangram(sentence):
    print("Pangram! :)")
else:
    print("No :(")


Insights:
  • The function converts the input string sentence to lowercase using the lower() method. This ensures that both uppercase and lowercase letters are treated equally, making the pangram check case-insensitive.
  • The function uses a set data structure (alphaSet) to store unique alphabetic characters encountered in the input string. Sets are suitable for this purpose because they automatically eliminate duplicates, and their lookup time complexity is O(1).
  • The function iterates through each character in the lowercase version of the input string (s) using a for loop. Within the loop, it checks if the current character (i) is an alphabet using the isalpha() method. This ensures that only alphabetic characters are considered for the pangram check, ignoring spaces, punctuation, and other non-alphabetic characters.
  • If the character is an alphabet, it's added to the alphaSet using the add() method. This builds a set containing all unique alphabetic characters present in the input string.
  • After iterating through all characters, the function checks if the length of the alphaSet is equal to 26. This condition verifies if all 26 English letters are present in the input string, indicating a pangram.
  • If the pangram condition is met (i.e., alphaSet contains all 26 letters), the function returns True, indicating that the input string is a pangram. If the pangram condition is not met (i.e., alphaSet does not contain all 26 letters), the function returns False, indicating that the input string is not a pangram.
  • Based on the result of the isPangram function, the script prints either "Pangram! :)" or "No :(" to indicate whether the input string is a pangram or not.
Enjoy coding with Pangrams and Pancakes! :)

Comments