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 http://www.puzzlers.org/pub/wordlists/unixdict.txt, 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.

Requirements:

 pip install requests 

Code

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

  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 = " http://www.puzzlers.org/pub/wordlists/unixdict.txt "

  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

continue

 

# 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’

  break

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 contribute.python.engineering or by posting an article contribute @ python.engineering. 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.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically