Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ตรวจสอบว่า String เป็น Pangram, Lipogram หรือ Pangrammatic Lipogram ด้วย Python

Pangram = ประโยคที่มีตัวอักษรทุกตัวในอัลฟาเบทอย่างน้อย 1 ครั้ง
Lipogram = ประโยคที่ขาดตัวอักษร 1 ตัวขึ้นไป
Pangrammatic Lipogram = ประโยคที่มีตัวอักษรทุกตัว ยกเว้น 1 ตัวเท่านั้น

ตัวอย่าง Input/Output

Input strings:
1. "pack my box with five dozen liquor jugs"
2. "to stay in this mortal world or by my own hand go to oblivion, that is my conundrum."
3. "the quick brown fox jumps over a lazy dog"
4. "waltz, nymph, for quick jigs ve bud"

Output:
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

อัลกอริทึม

  1. แปลง String เป็น Lowercase
  2. นับตัวอักษรที่ ไม่มี ใน String (i)
  3. ถ้า i == 0 → Pangram
  4. ถ้า i == 1 → Pangrammatic Lipogram
  5. อื่นๆ → Lipogram (อาจเป็น)

โค้ดตัวอย่าง

import string

def solve(input_string):
    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"))