Home > Enterprise >  Changing how letters show when there are duplications in recreating Wordle
Changing how letters show when there are duplications in recreating Wordle

Time:03-21

I have just starting learning python and am trying to make Wordle. This is what I have done so far with 'gWs' being the variable for the guessed word split into a list with each letter of the word being an item in the list. 'cWs' is the same as 'gWs' but for the correct word which is randomly selected. The code compares whether each letter of both words are the same, in the word or not in the word. I can't figure how to make it so that if the word guessed has 2 or more of the same letter and the correct word only has one of the those letters, then it outputs that one letter is in the word or in the correct spot, and then the other is not in the word.

    if gWs[0] == cWs[0]:
        print('['   gWs[0]   ']', end=' ')
    elif gWs[0] in cWs:
        print('|'   gWs[0]   '|', end=' ')
    elif gWs[0] in cWs:
        print('|'   gWs[0]   '|', end=' ')
    else:
        print('{'   gWs[0]   '}', end=' ')
    
    if gWs[1] == cWs[1]:
        print('['   gWs[1]   ']', end=' ')
    elif gWs[1] in cWs:
        print('|'   gWs[1]   '|', end=' ')
    else:
        print('{'   gWs[1]   '}', end=' ')
    
    if gWs[2] == cWs[2]:
        print('['   gWs[2]   ']', end=' ')
    elif gWs[2] in cWs:
        print('|'   gWs[2]   '|', end=' ')
    else:
        print('{'   gWs[2]   '}', end=' ')
    
    if gWs[3] == cWs[3]:
        print('['   gWs[3]   ']', end=' ')
    elif gWs[3] in cWs:
        print('|'   gWs[3]   '|', end=' ')
    else:
        print('{'   gWs[3]   '}', end=' ')
    
    if gWs[4] == cWs[4]:
        print('['   gWs[4]   ']')
    elif gWs[4] in cWs:
        print('|'   gWs[4]   '|')
    else:
        print('{'   gWs[4]   '}')

Thanks.

CodePudding user response:

First of all, use better variable names - there is no negative to use words instead of 3-letter-acronyms.

Then change your code so it uses loops and enumerate for the checks.

Put the checking into a function to reuse - return the build string. Don't print with end="" - instead collect parts in a list and .join() them before returning.

def yours_fixed(correctWord, guess):
    # loop instead of if - elif - elif - elif for each letter position
    result = []
    for pos,letter in enumerate(guess):
        if letter == correctWord[pos]:
            result.append(f"[{letter}]")
        elif letter in correctWord:
            result.append(f"|{letter}|")
        else:
            result.append(f"{{{letter}}}")

    return " ".join(result)

Then think about what you want:

  • somehow count letters
  • if letter resolves, remove 1 from the count and delete if counts are 0
  • do the check and print with the counting dict not against the word
def better_checkWords(correctWord, guess):
    # use collection.Counter to get the same but more effective
    letterCount = {a: correctWord.count(a) for a in set(correctWord)}
    def removeLetter(l):
        letterCount[letter] -= 1
        if letterCount[letter] == 0:
            del letterCount[letter]

    result = []
    for pos,letter in enumerate(guess):

        if letter == correctWord[pos]:
            result.append(f"[{letter}]")
            removeLetter(letter)
        elif letter in letterCount:  # check if letter still in
            result.append(f"|{letter}|")
            removeLetter(letter)
        else:
            result.append(f"{{{letter}}}")

    return " ".join(result)

correctWord = "aabcd"
guess = "abaax"
print( yours_fixed(correctWord, guess))
print( better_checkWords(correctWord, guess))

Output:

# "aabcd" correct
# "abaax" guessed

[a] |b| |a| |a| {x}  # yours
[a] |b| |a| {a} {x}  # better one

CodePudding user response:

Let's say gWs has more than one of letter l, and cWs has exactly one l. You can do something like

flag = [0] * len(gWs)
for i, x in enumerate(gWs):
    if x == l:
        if i == cWs.index(x):
            flag[i] = 2
        else:
            flag[i] = 1

Now according to flag:

0 - not in cWs

1 - in cWs but not at the right position

2 - at the right position

  • Related