Skip to content

Instantly share code, notes, and snippets.

@shashankqv
Last active December 31, 2015 00:09
Show Gist options
  • Save shashankqv/7905528 to your computer and use it in GitHub Desktop.
Save shashankqv/7905528 to your computer and use it in GitHub Desktop.
Jumble Solver : A small command line application that takes the jumbled input string and prints out all correct english words with the same set of characters. The set of words are listed in the dictionary file.
import sys
################################################
# This fuction searches the words in dictionary#
# Input Parameters : Jumbled Word #
# Return type : A list containing valid words #
################################################
def wordsearch(jumbledWord):
matchList = [] # Creating an empty list
count = 0 # Setting counter to zero
dictionary = open('dictionary.txt') # Getting dictionary
# Using a for loop, traverse all the dictionary datas.
for item in dictionary:
unsortedWord = item.rstrip() # strip the right spaces
sortedWord = sorted(unsortedWord.lower()) # Convert the word in lower case then creating a sorted list of characters
if sortedWord == jumbledWord: # comparing the both lists ! if matches then we have a match
matchList.append(unsortedWord) # Append the word in list
count += 1
if count == 0:
sys.stdout.write('No word found in dictionary' + '\n')
dictionary.close()
return matchList
if __name__ == '__main__':
if len(sys.argv) < 2: #checking if user have provided jumbled word through command line if not show him/her a message
sys.stdout.write('No jumbled word specified, Please enter a jumbled word then run'+ '\n')
sys.exit(0)
else:
jumbledWord = sys.argv[1] # Storing the user input in jumbled Word.
if jumbledWord.isalpha():
jumbledWord = jumbledWord.lower() # convert the whole word in lowercase irrespective of the input case type
jumbledWord = sorted(jumbledWord) # This function(sorted) will create a sorted list(Alphabatically) of jumbled word.
else:
sys.stdout.write('only alphabets in jumbled word' + '\n')
sys.exit(1)
matchlist = wordsearch(jumbledWord) # Calling wordsearch function
print '\n'.join(matchlist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment