檢查字串是否為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

更新於:2021年1月18日

瀏覽量:156

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.