检查句子是否为全套字母句的 Python 程序。
给定一个句子。我们的任务是检查这个句子是否为全套字母句。全套字母句的逻辑是,单词或句子至少包含字母表的每个字母一次。要解决这个问题,我们使用 set()方法和列表推导技术。
示例
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'
算法
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.
示例代码
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")
输出
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP