An ordered word is a word in which the letters appear in alphabetical order. For Example:
- abbey -> ordered
- geeks -> not ordered
Installation
Install the requests module, using the following pip command:
pip install requests
Approach
The task is divided into two main parts:
1. Scraping the Dictionary
- Fetch the content from the URL using the requests library.
- Decode the UTF-8 content into a string.
- Split the string into a list of words.
2. Finding Ordered Words
- Traverse the list of words.
- Compare ASCII values of adjacent characters in each word.
- If all characters are in alphabetical order, the word is ordered.
- Otherwise, skip it.
Implementation
Note: This article uses the public dictionary text file (unixdict.txt) available at https://www.puzzlers.org/pub/wordlists/unixdict.txt for scraping.
import requests
url = "https://www.puzzlers.org/pub/wordlists/unixdict.txt"
fd = requests.get(url)
c1 = fd.content.decode("utf-8").split()[16:]
for word in c1:
if len(word) < 3:
continue
if all(ord(word[i]) <= ord(word[i+1]) for i in range(len(word)-1)):
print(f"{word}: Word is ordered")
Output
not: Word is ordered
Jan: Word is ordered
not: Word is ordered
!==: Word is ordered
!==: Word is ordered
...: Word is ordered
...: Word is ordered
!==: Word is ordered
for: Word is ordered
for: Word is ordered
===: Word is ordered
!==: Word is ordered
!==: Word is ordered
........
Explanation:
- fd = requests.get(url): Download word list from URL.
- c1 = fd.content.decode("utf-8").split()[16:] Decode, split into words, skip first 16 entries.
- if all(ord(word[i]) <= ord(word[i+1]) for i in range(len(word)-1)): Check if letters are in alphabetical order.