สำหรับการแก้ปัญหานี้ เราต้องการโมดูลคำขอ
สำหรับการติดตั้งโมดูลคำขอ เราต้องใช้คำสั่งนี้เพื่อดำเนินการที่บรรทัดคำสั่ง
คำขอติดตั้ง pip
ขูด
- โมดูลคำขอนำเข้า
- จากนั้น เราต้องดึงข้อมูลจาก URL
- การใช้ UTF-8 ถอดรหัสข้อความ
- จากนั้นแปลงสตริงเป็นรายการคำ
ค้นหาคำสั่ง
- สำรวจรายการคำโดยใช้การวนซ้ำ
- จากนั้นเปรียบเทียบค่า ASCII ของอักขระที่อยู่ติดกันของแต่ละคำ
- หากการเปรียบเทียบเป็นจริง ให้พิมพ์คำที่เรียงลำดับ มิฉะนั้นจะเก็บคำที่ไม่เรียงลำดับไว้
โค้ดตัวอย่าง
import requests
def Words_find():
my_url = ""#put thisurl of .txt files in any website
my_fetchData = requests.get(my_url)
my_wordList = my_fetchData.content
my_wordList = my_wordList.decode("utf-8").split()
return my_wordList
def wordordered():
collection = Words_find()
collection = collection[16:]
my_word = ''
for my_word in collection:
result = 'ordered'
i = 0
l = len(my_word) - 1
if (len(my_word) < 3):
continue
while i < l:
if (ord(my_word[i]) > ord(my_word[i+1])):
result = 'not ordered'
break
else:
i += 1
if (result == 'ordered'):
print(my_word,': ',result)
if __name__ == '__main__':
wordordered()