Change language

Stripping and searching ordered words in a dictionary using Python

| | |

Ordered word — it is a word in which letters are displayed in alphabetical order. For example, abbey and dirt . The rest of the words are unordered, for example, gics

The task at hand

This task is taken from the Rosetta Code, and it not as common as it seems from the above description. To get a large number of words, we will use the online dictionary available at at, which contains a collection of approximately 2500 words, and since we are going to use python, we can do this by clearing the dictionary instead of loading it as a text file and then doing some file operations on it.


 pip install requests 


The approach would be to go through the whole word and compare the values ​​of the ascii elements in pairs until we find a false result, otherwise the word will be ordered. 
So this task will be divided into 2 parts:

  1. Using the queries of the Python library, we get data from the given URL
  2. Store the content extracted from the URL as a string
  3. Decode content that is normally encoded on the web using UTF-8
  4. Transformation long line of content into a wordlist

Search for ordered words

  1. Traversing a wordlist
  2. Pairwise value comparison ASCII of each adjacent character in each word
  3. Storing a false result if the pair is unordered
  4. Otherwise, print the ordered word

# Python program to find ordered words

import requests

# Scrapes words from the URL below and saves
# in the list

def getWords ():


# contains about 2500 words

url = " "

  fetchData = requests.get (url)


# fetches web page content

  wordList = fetchData.content


# decodes the encoded in UTF-8 the text and splits

# string to turn it into a list of words

wordList = wordList.decode ( "utf-8" ). split ()


return wordList


# function to determine if a word is ordered or not

def isOrdered ():


  # retrieve wordList

collection = getWords ()


# from the first few elements

  # a dictionary of numbers, getting rid of those

# numbers by cutting out the first 17 elements

collection = collection [ 16 :]

  word = ’ ’


for word in collection:

result = ’Word is ordered’

i = 0

l = len (word) - 1


if ( len (word) & lt;  3 ): # skips 1 and 2 letter lines



# iterates over all characters of the word in pairs

while i & lt; l: 

if ( ord (word [i])" ord (word [i + 1 ]) ):

result = ’Word is not ordered’


else :

i + = 1


# print order only of these words

if (result = = ’Word is ordered’ ):

print (word, ’:’ , result)


# execute isOrdered () function

if __ name__ = = ’__main__’ :

  isOrdered ()

  Output:  aau: Word is ordered abbe: Word is ordered abbey: Word is ordered abbot: Word is ordered abbott: Word is ordered abc: Word is ordered abe: Word is ordered abel: Word is ordered abet: Word is ordered abo: Word is ordered abort: Word is ordered accent: Word is ordered accept: Word is ordered ........................... ............... ............ ........................... 

List Literature: Rosetta Code

This article is courtesy of Palash Nigam . If you are as Python.Engineering and would like to contribute, you can also write an article using or by posting an article contribute @ See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.