Esempio n. 1
0
def findWords(firstWord, lastWord):
    global dictionary

    #---------------Enter your source code here----------------#
    #Print a shortest word ladder for the firstWord and lastWord and return 1 if such a ladder can be found
    #Return -1 if there exists no word ladder for the firstWord and lastWord

    queue = MyQueue()  #declare a new queue
    existingwordlist = []  #to keep track of used words
    firtFound = False
    lastFound = False
    # if both words exist in dictionary, then posible to have ladder
    for i in range(len(dictionary)):
        if firstWord == dictionary[i]:
            firstFound = True
        if lastWord == dictionary[i]:
            lastFound = True
    if lastFound and firstFound:
        if firstWord == lastWord:
            print[firstWord]
            return 1
        elif len(firstWord) != len(lastWord):
            return -1
        else:
            existingwordlist.append(firstWord)
            refStack = MyStack()
            refStack.push(firstWord)
            whileCounter = 0
            curword = firstWord
            while lastWord not in existingwordlist and whileCounter < 100:
                queuedCount = 0
                for i in range(len(dictionary)):  #go through dictionary list
                    if len(curword) == len(
                            dictionary[i]):  #if word length matches
                        counter = 0
                        for k in range(len(curword)):
                            a = dictionary[i]
                            if a[k] == curword[
                                    k]:  #go through each character and match if they are the same
                                counter += 1

                        if counter == (len(dictionary[i]) - 1) and dictionary[
                                i] not in existingwordlist:  #do checking if only one letter different
                            existingwordlist.append(dictionary[i])
                            curStack = MyStack()
                            curStack.copyFrom(refStack)
                            curStack.push(dictionary[i])
                            queue.enqueue(
                                curStack)  #enqueue the stack into the queue
                            queuedCount += 1

                if queuedCount == 0 and queue.size() > 0:
                    refStack = MyStack()
                    firstStack = queue.dequeue()
                    refStack.copyFrom(firstStack)
                    curword = firstStack.pop()
                whileCounter += 1  # can't think of any smarter way to prevent infinite loop
            outputlist = []  #declare a list to store the word ladder
            if lastWord in existingwordlist:
                for k in range(queue.size()):
                    outputlist.append(queue.dequeue().toString())
                print(outputlist[-1])
                return 1
            else:
                return -1
    else:
        return -1
Esempio n. 2
0
def findWords(firstWord, lastWord):
    global dictionary

    #---------------Enter your source code here----------------#
    #Print a shortest word ladder for the firstWord and lastWord and return 1 if such a ladder can be found
    #Return -1 if there exists no word ladder for the firstWord and lastWord

    usedWords = []
    usedWords.append(firstWord)
    #step 1
    count = 0
    breakFirstWordIntoChars = list(firstWord)
    myQueue = MyQueue()

    for i in range(len(dictionary)):

        if len(dictionary[i]) == len(breakFirstWordIntoChars):
            breakDicObjectIntoChars = list(dictionary[i])
            for j in range(len(breakFirstWordIntoChars)):

                if breakFirstWordIntoChars[j] != breakDicObjectIntoChars[j]:
                    count += 1

            if count == 1 and dictionary[i] not in usedWords:
                myStack = MyStack()
                myStack.push(firstWord)
                myStack.push(dictionary[i])
                usedWords.append(dictionary[i])
                myQueue.enqueue(myStack)

        count = 0

    #step 2
    #print myQueue.size()

    if myQueue.size() > 0:
        count = 0

        while (True):

            if myQueue.isEmpty() is not True:
                firstStack = myQueue.dequeue()
                firstStackWord = firstStack.peek()

                print firstStack.toString()
                if firstStackWord == lastWord:
                    #return firstStack.toString()
                    break
                else:

                    firstStackWordIntoChars = list(firstStackWord)

                    for m in range(len(dictionary)):
                        if len(dictionary[m]) == len(firstStackWordIntoChars):
                            breakDicObjectIntoChars = list(dictionary[m])

                            for n in range(len(firstStackWordIntoChars)):
                                if firstStackWordIntoChars[
                                        n] != breakDicObjectIntoChars[n]:
                                    count += 1

                            if count == 1 and dictionary[m] not in usedWords:
                                myStack2 = MyStack()
                                myStack2.copyFrom(firstStack)
                                myStack2.push(dictionary[m])
                                myQueue.enqueue(myStack2)
                                usedWords.append(dictionary[m])

                        count = 0
            else:

                return -1
                break
    else:
        myStack2 = MyStack()
        myStack2.push(firstWord)
        return myStack2.toString()