How to resolve the algorithm Pangram checker step by step in the Python programming language
How to resolve the algorithm Pangram checker step by step in the Python programming language
Table of Contents
Problem Statement
A pangram is a sentence that contains all the letters of the English alphabet at least once. For example: The quick brown fox jumps over the lazy dog.
Write a function or method to check a sentence to see if it is a pangram (or not) and show its use.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pangram checker step by step in the Python programming language
The provided Python code determines whether a given sentence is a pangram or not. A pangram is a sentence that contains every letter of the alphabet at least once.
Here's a step-by-step explanation of the code:
-
Importing Modules: The code starts by importing two modules:
string
: This module contains various string-related constants and functions.sys
: This module provides information about the current Python environment and is used to check if the code is running in Python 2 or Python 3.
-
Checking Python Version: The code checks if the Python version is less than 3 (i.e., if it's Python 2). If it's Python 2, it modifies the
input
function toraw_input
, which is used in Python 2 for getting user input. -
Defining the
ispangram
Function: The code defines a function namedispangram
that takes two parameters:sentence
: The sentence to be checked for pangram status.alphabet
(optional): The alphabet to use. By default, it uses the lowercase English alphabet (string.ascii_lowercase
).
-
Converting the Sentence to Lowercase: Inside the
ispangram
function, the sentence is converted to lowercase usingsentence.lower()
. This ensures that the code is case-insensitive when checking for pangram status. -
Creating a Set of the Alphabet: The code creates a set of the alphabet characters using the
set
function. A set is a data structure that stores unique elements, so it ensures that each alphabet character appears only once. -
Checking Subset Relationship: The code then checks if the set of the alphabet characters (
alphaset
) is a subset of the set of characters in the lowercase sentence (set(sentence.lower())
). Ifalphaset
is a subset, it means that every alphabet character is present in the sentence, making it a pangram. -
Getting User Input: The code prompts the user to enter a sentence using
input('Sentence: ' )
. The user's input is passed as an argument to theispangram
function. -
Printing the Result: The code prints the result of the
ispangram
function, which is a boolean value indicating whether the sentence is a pangram (True) or not (False).
Source code in the python programming language
import string, sys
if sys.version_info[0] < 3:
input = raw_input
def ispangram(sentence, alphabet=string.ascii_lowercase):
alphaset = set(alphabet)
return alphaset <= set(sentence.lower())
print ( ispangram(input('Sentence: ')) )
You may also check:How to resolve the algorithm Egyptian division step by step in the Nim programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the PL/I programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the Julia programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the Racket programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the Lua programming language