Computer >> Máy Tính >  >> Lập trình >> Python

Chương trình Python để kiểm tra một câu có phải là pangrams hay không.

Đưa ra một câu. Nhiệm vụ của chúng ta là kiểm tra xem câu này có phải là pan gram hay không. Logic của việc kiểm tra Pan gram là các từ hoặc câu có chứa mọi chữ cái trong bảng chữ cái ít nhất một lần. Để giải quyết vấn đề này, chúng tôi sử dụng phương thức set () và kỹ thuật hiểu danh sách.

Ví dụ

Input: string = 'abc def ghi jkl mno pqr stu vwx yz'
Output: Yes
// contains all the characters from ‘a’ to ‘z’
Input: str='python program'
Output: No
// Does not contains all the characters from ‘a’ to 'z'

Thuật toán

Step 1: create a string.
Step 2: Convert the complete sentence to a lower case using lower () method.
Step 3: convert the input string into a set (), so that we will list of all unique characters present in the sentence.
Step 4: separate out all alphabets ord () returns ASCII value of the character.
Step 5: If length of list is 26 that means all characters are present and sentence is Pangram otherwise not.

Mã mẫu

def checkPangram(s):
   lst = []
   for i in range(26):
      lst.append(False)
   for c in s.lower(): 
      if not c == " ":
         lst[ord(c) -ord('a')]=True
   for ch in lst:
      if ch == False:
         return False
   return True
# Driver Program 
str1=input("Enter The String ::7gt;")
if (checkPangram(str1)):
   print ('"'+str1+'"')
   print ("is a pangram")
else:
   print ('"'+str1+'"')
   print ("is not a pangram")

Đầu ra

Enter The String ::abc def ghi jkl mno pqr stu vwx yz
"abc def ghi jkl mno pqr stu vwx yz"
is a pangram
Enter The String ::> python program
"pyhton program"
is not a pangram