检查字符串是否为Pangrammatic Lipogram(Python)
假设我们得到了三个字符串,我们需要找出哪些字符串是Pangram、Lipogram和Pangrammatic Lipogram。Pangram是一个字符串或句子,其中包含字母表中的每个字母至少一次。Lipogram是一个字符串或句子,其中缺少一个或多个字母。Pangrammatic Lipogram是一个字符串或句子,其中包含字母表中的所有字母,只缺少一个。
所以,如果输入是这样的:
pack my box with five dozen liquor jugs to stay in this mortal world or by my own hand go to oblivion, that is my conundrum. the quick brown fox jumps over a lazy dog waltz, nymph, for quick jigs ve bud,
那么输出将是:
The String is a Pangram The String isn't a Pangram but might be a Lipogram The String is a Pangram The String is a Pangrammatic Lipogram
为了解决这个问题,我们将遵循以下步骤:
- 将字符串中的所有字母转换为小写字母。
- i := 0
- 对于小写字母表中的每个字符,执行以下操作:
- 如果在输入字符串中找不到该字符,则
- i := i + 1
- 如果在输入字符串中找不到该字符,则
- 如果 i 等于 0,则
- 输出 := "该字符串是一个Pangram"
- 否则,如果 i 等于 1,则
- 输出 := "该字符串是一个Pangrammatic Lipogram"
- 否则,
- 输出 := "该字符串不是Pangram,但可能是Lipogram"
- 返回输出
示例
让我们看下面的实现来更好地理解:
import string def solve(input_string): input_string.lower() i = 0 for character in string.ascii_lowercase: if(input_string.find(character) < 0): i += 1 if(i == 0): output = "The String is a Pangram" elif(i == 1): output = "The String is a Pangrammatic Lipogram" else: output = "The String isn't a Pangram but might be a Lipogram" return output print(solve("pack my box with five dozen liquor jugs")) print(solve("to stay in this mortal world or by my own hand go to oblivion,that is my conundrum.")) print(solve("the quick brown fox jumps over a lazy dog")) print(solve("waltz, nymph, for quick jigs ve bud"))
输入
pack my box with five dozen liquor jugs to stay in this mortal world or by my own hand go to oblivion, that is my conundrum. the quick brown fox jumps over a lazy dog waltz, nymph, for quick jigs ve bud
输出
The String is a Pangram The String isn't a Pangram but might be a Lipogram The String is a Pangram The String is a Pangrammatic Lipogram
广告