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

โปรแกรมตรวจสอบว่าสตริงที่กำหนดเป็นพังแกรมหรือไม่ใน Python

สมมติว่ามีสตริง s แทนประโยค เราต้องตรวจสอบว่าทุกตัวอักษรในอักษรอังกฤษถูกใช้อย่างน้อยหนึ่งครั้งหรือไม่

เช่น ถ้าอินพุตคือ "The grumpy wizards make toxic brew, for the evil queen and Jack" ผลลัพธ์จะเป็น True

ขั้นตอนการแก้ปัญหา

  • s := แปลงทุกตัวอักษรใน s เป็นตัวพิมพ์เล็ก
  • สำหรับแต่ละ i ในอักษรอังกฤษ a-z ถ้า i ไม่อยู่ใน s คืนค่า False
  • คืนค่า True

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

import string
class Solution:
    def solve(self, s):
        s = s.lower()
        for i in string.ascii_lowercase:
            if i not in s:
                return False
        return True

s = "The grumpy wizards make toxic brew, for the evil queen and Jack"
ob = Solution()
print(ob.solve(s))

อินพุต

"The grumpy wizards make toxic brew, for the evil queen and Jack"

เอาต์พุต

True