Skip to main content
Source Link

Python Solution - Pangram Checker

First I downloaded and saved the provided pangrams.json file in the same directory.

Here's the python code for categorizing the perfect pangrams, pangrams and non pangrams.

Python Code - Solution

# Pangram checker

import json

class Pangram:
    def check(self, s:str):
        count = 0
        overall = 0
        alphabet_dictionary = {'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0, 'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0, 'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0, 'v':0, 'w':0, 'x':0, 'y':0, 'z':0}
        for letter in s:
            if letter in alphabet_dictionary.keys():
                if alphabet_dictionary[letter] == 0:
                    count += 1
                alphabet_dictionary[letter] += 1
                overall += 1

        if count == 26 and overall == 26:
            return 1
        if count == 26:
            return 0
        if count != 26:
            return -1
        return -2

    def main(self):
        # Pangram object
        P = Pangram()
        
        # read list of strings
        with open("pangrams.json", "r", encoding="utf-8") as file:
            L = json.load(file)
        file.close()
        
        nonPangrams = 0
        pangrams = 0
        perfectPangrams = 0

        for line in L:
            result = P.check(line.lower())
            if result == 1:
                perfectPangrams += 1
            elif result == 0:
                pangrams += 1
            elif result == -1:
                nonPangrams += 1
            else:
                print("There's some error in logic.")

        print("Perfect Pangrams:", perfectPangrams)
        print("Pangrams:", pangrams)
        print("Non Pangrams:", nonPangrams)

if __name__ == "__main__":
    P = Pangram()
    P.main()

Result

Perfect Pangrams: 49
Pangrams: 132
Non Pangrams: 819

The program effectively calculated the pangrams.