Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ktisha committed Nov 16, 2012
2 parents 13ae9e5 + 9ef7bbd commit 733fa92
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
96 changes: 96 additions & 0 deletions dudin/WordsStairway/Dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
__author__ = 'viteck.dudin'
__mail__ = '[email protected]'


class Word:
"""
Class describes any word from input dictionary
"""
text = ""
global_index = -1
bucket_number = -1
parent_index = -1

def __init__(self, text):
if not isinstance(text, str):
raise Exception("Input word is not a string (input: " + str(text) + ")")
self.text = (str(text)).upper()


class Dictionary:
check_unique_words_in_dictionary = True

def __init__(self, start_word, end_word, dictionary_filename):
self.start_word = Word(start_word)
self.end_word = Word(end_word)
if len(start_word) != len(end_word):
raise Exception("Incorrect input: start_word (" + self.start_word.text +
") and end_word (" + self.end_word.text + ") has different lengths")
if len(start_word) == 0:
raise Exception("Incorrect input: words should have non-zero lengths")
self.word_length = len(start_word)

# Add start_word into dictionary
self.words_buckets = {}
self.start_word.global_index = 0
self.words_buckets[self.__calc_words_dif(self.start_word, self.end_word)] = [self.start_word]
# Add end_word into dictionary
self.end_word.global_index = 1
self.words_buckets[0] = [self.end_word]
with open(dictionary_filename, "r") as fin:
self.__read_input_dictionary(fin)

def __read_input_dictionary(self, fin):
# in bucket with key i there will be words, which differ from end_word with i symbols
self.dictionary_size = 2

for line in fin:
for text in line.split():
# Make checks for text
if text[-1] == '\n':
text = text[:-1]
# Save only that words, which have the same length with start_word and end_word
if len(text) != self.word_length and len(text) > 0:
print "Word [" + text + "] not added into dictionary (reason: incorrect length)"
continue
# All ok, create new Word
word = Word(text)
word.global_index = self.dictionary_size
self.dictionary_size += 1
self.__add_word_into_dictionary(word)

def __add_word_into_dictionary(self, word):
bucket = self.__calc_words_dif(word, self.end_word)
Word(word).bucket_number = bucket

if bucket not in self.words_buckets:
self.words_buckets[bucket] = []

all_ok = True
# Check unique, if necessary
if self.check_unique_words_in_dictionary:
all_ok = self.__check_unique(word, self.words_buckets[bucket])
if all_ok:
self.words_buckets[bucket].append(word)

def __check_unique(self, word, word_list):
# TODO: Need to be released
raise Exception("Unsupported operation")

def __calc_words_dif(self, word1, word2):
length = len(word1)
dif = 0
for x in xrange(length):
if word1[x] != word2[x]:
dif += 1
return dif

def create_stairway(self):
# create stairway, using analog of bfs in graphs
self.words_queue = []
self.words_queue.append(self.start_word)

# TODO: need to be released
raise Exception("Unsupported operation")


27 changes: 27 additions & 0 deletions dudin/WordsStairway/Main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
__author__ = 'viteck.dudin'
__mail__ = '[email protected]'

import sys
import Dictionary


def main(start_word, end_word, dictionary_filename):
while start_word[-1] == '\n':
start_word = start_word[:-1]
while end_word[-1] == '\n':
end_word = end_word[:-1]
while dictionary_filename[-1] == '\n':
dictionary_filename = dictionary_filename[:-1]

words_dict = Dictionary.Dictionary(start_word, end_word, dictionary_filename)
words_stairway = words_dict.create_stairway()
for word in words_stairway:
print word


if __name__ == "__main__":
if len(sys.argv) != 4:
print "Wrong number of parameters"
print "Usage: start_word end_word dictionary_filename"
else:
main(sys.argv[1], sys.argv[2], sys.argv[3])
2 changes: 2 additions & 0 deletions dudin/WordsStairway/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__author__ = 'viteck.dudin'
__mail__ = '[email protected]'
1 change: 0 additions & 1 deletion dudin/__init__.py

This file was deleted.

0 comments on commit 733fa92

Please sign in to comment.