Search code examples
python-3.xstringimmutabilitystring-comparison

Overlapping values of strings in Python


I am building a puzzle word game in Python. I have the correct puzzle word, and the guessed puzzle word. I want to build a third string which shows the correct letters in the guessed puzzle in the correct puzzle word, and _ at the position of the incorrect letters.

For example, say the correct word is APPLE and the guessed word is APTLE then i want to have a third string: AP_L_

The guessed word and correct word are guaranteed to be 3 to 5 characters long, but the guessed word is not guaranteed to be the same length as the correct word

For example, correct word is TEA and the guessed word is TEAKO, then the third string should be TEA__ because the players guessed the last two letters incorrectly.

Another example, correct word is APPLE and guessed word is POP, the third string should be:

_ _ P_ _ (without space separation)

I can successfully get the matched indexes of the correct and guessed word; however, I am having problems building the third string. I just learned that strings in Python are immutable and that i cannot assign something like str1[index] = str2[index]

I have tried many things, including using lists, but i am not getting the correct answer. The attached code is my most recent attempt, would you please help me solve this?

Thank you

find the match between puzzle_word and guess

def matcher(str_a, str_b):
    #find indexes where letters overlap
    matched_indexes = [i for i, (a, b) in enumerate(zip(str_a, str_b)) if a == b]

    result = []
    for i in str_a:
        result.append('_')

    for value in matched_indexes:
        result[value].replace('_', str_a[value])

    print(result)

matcher("apple", "allke")

the output result right now is list of five "_"

cases:

  1. correct word is APPLE and the guessed word is APTLE third string: AP_L_
  2. correct word is TEA and the guessed word is TEAKO, third string should be TEA__
  3. correct word is APPLE and guessed word is POP, third string should be _ _ P_ _

Solution

  • You can use itertools.zip_longest here to always make sure you pad out to the longest word provided and then create a new string by joining the matching characters or otherwise a _. eg:

    from itertools import zip_longest
    
    correct_and_guess = [
        ('APPLE', 'APTLE'),
        ('TEA', 'TEAKO'),
        ('APPLE', 'POP')
    ]
    
    for correct, guess in correct_and_guess:
        # If characters in same positions match - show character otherwise `_`
        new_word = ''.join(c if c == g else '_' for c, g in zip_longest(correct, guess, fillvalue='_'))
        print(correct, guess, new_word)
    

    Will print the following:

    APPLE APTLE AP_LE
    TEA TEAKO TEA__
    APPLE POP __P__