给一个句子。我们的任务是检查这句话是否是潘克。潘克检查的逻辑是单词或句子包含字母表中的每个字母至少一次。为了解决这个问题,我们使用set()方法和列表理解技术。
Input: string = 'abc def ghi jkl mno pqr stu vwx yz' Output: Yes //包含从'a'到'z'的所有字符 Input: str='python program' Output: No //不包含从'a'到'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