Thursday, October 25, 2012

Anagram Solver jumbler.py


"""
jumbler.py Assignment 2, CIS 210, Fall 2012
Authors: Audrey Stroh, Caitlin Hennessy
10/2/2012

Finds un-scrambled word matches for scrambled words.

USAGE: python3 jumbler.py word dictionary.txt
"""

import sys

#open dictionary file from command line

def get_file():
    if len(sys.argv) != 3:
        print("Usage: python3 jumbler.py word dictionary.txt")
        exit(1)
    dfile = open(sys.argv[2])
    return dfile

src = get_file()
scrambled = sys.argv[1]

dictionary = {}

#MAIN
for word in src:
    word = word.strip()
    dictionary[word] = sorted(word)


matches = []
for word in dictionary:
    if sorted(scrambled) == dictionary[word]:
          matches.append(word)

for word in matches:
    print (word)
 
print(len(matches), 'match(es) from', len(dictionary), 'words!')

No comments:

Post a Comment