在Python Pangram检查中使用Set()

给出一个字符串检查它是否是Pangram。pangram是包含英语字母表中每个字母的句子。小写和大写被认为是相同的。

null

例如:

Input : str = 'The quick brown fox jumps over 
               the lazy dog'
Output : Yes
// Contains all the characters from ‘a’ to ‘z’

Input : str='The quick brown fox jumps over the dog'
Output : No
// Doesn’t contains all the characters from ‘a’
// to ‘z’, as ‘l’, ‘z’, ‘y’ are missing

此问题已有解决方案,请参考 Pangram检查 链接我们将在Python中使用 Set() 数据结构和 列表()理解 .方法非常简单,

  1. 使用 下() python中字符串数据类型的方法。
  2. 现在把这句话译成英语 集合(str) 这样我们就可以得到给定字符串中所有唯一字符的列表。
  3. 现在把所有字母表(a-z)分开,如果列表的长度是26,这意味着所有的字符都存在,句子是完整的 潘格拉姆 否则就不行了。

# function to check pangram
def pangram( input ):
# convert input string into lower case
input = input .lower()
# convert input string into Set() so that we will
# list of all unique characters present in sentence
input = set ( input )
# separate out all alphabets
# ord(ch) returns ascii value of character
alpha = [ ch for ch in input if ord (ch) in range ( ord ( 'a' ), ord ( 'z' ) + 1 )]
if len (alpha) = = 26 :
return 'true'
else :
return 'false'
# Driver program
if __name__ = = "__main__" :
input = 'The quick brown fox jumps over the lazy dog'
print (pangram( input ))


输出:

true
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享