简体   繁体   中英

How do you take a “backtrack” in a while loop?

I feel as if this problem requires some kind of recursion. This is for a data structures class teaching us how to deal with collisions in hash tables. We are asked to:

  • The maximum number of attempts is 11. This does not mean your g-value can't go past 11. It means you have 11 tries to place your word per time you try to place it.

  • If you are unsuccessful in placing a word, keep the g-value you reached.

  • When you are unsuccessful in placing a word and need to backtrack: first - remove the word from its current position in the hash table and second - increment the g-value of the first letter by 1 so that when you try to place it, it does not go back into the same position.

My solution is using two while loops one controlling which word we're on and another loop to count the attempts. It can be found below:

#Placing in hash table
hashTable = [None] * len(keyWords)
attempt = 0
i = 0


while(i <= len(keyWordsList) - 1 | i>= 0) :
    word = keyWordsList[i] #cycling thru list of lists picking first element which is a word
    colCount = 0

   # print("\nattempting to hash:", word[0])
    if hashTable[hash(word[0], 0)] != None: #uses hash function checked working as expected returns index
        while(colCount <= maxGVal) :        #to ensure the spot is free in the table 
            #print("\n collision occurred on word: ", word[0], "Slot: ", word.index(word[0]))
            if hashTable[hash(word[0], colCount)] == None: 
                hashTable[hash(word[0], colCount)] = word[0]
                #print("successfully hashed: ", word[0], "at slot:", hash(word[0], colCount))
            elif colCount == maxGVal:
                #print("resetting gvals:")
                #for sublist in gVals:
                #    sublist[1] = 0
                print("going back to previous word:")
                colCount = 0
                i -= 1
                break
            colCount += 1
    elif hashTable[hash(word[0], 0)] == None:
        hashTable[hash(word[0], 0)] = word[0]
        #print("Successfully hashed: ", word[0], "at slot:", hash(word[0], 0))

    
    i += 1        

I had errors with my hash functions along with overthinking the while loop. Biggest mistake was accessing the list of lists incorrectly notice two brackets in the new code. For anyone interested in the future, I have the loop I used below. The project is passed around universities with shit instructions so good luck. Comment here whenever and I might be able to post more code.

while(j <= len(keyWords)-1):
    aWord = keyWordsList[j][0]
    if hashTable[hash(aWord, colCount)] == None:
        hashTable[hash(aWord, colCount)] = aWord
        if colCount > 0: colCount = 0
        j += 1
        continue
    elif hashTable[hash(aWord, colCount)] != None:
        colCount += 1
    elif colCount == maxGVal:
        for sublist in gVals:
            sublist[1] = 0
        j -= 1
        colCount = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM