Home > OS >  compare the character in 2 strings taking into account the position
compare the character in 2 strings taking into account the position

Time:03-22

I am coding for a game similar to wordle I need to compare 2 strings

  • If the character and position is the same to return -
  • If the character in guess is in the answer but wrong position to return *
  • If character completely not in answer to return .

If my answer has 2 similar characters (eg, guess: accept, answer: castle), * can only be returned once, meaning the expected output would be **.*.*

I can't seem to iterate the string taking into account the position as well

def process(guess: str, answer: str) -> str:
    output = ""
    for i,ch in enumerate(guess):
        if ch not in answer:
            output  = '.'
        elif ch != answer[i]:
            output  = '*'
        else:
            output  = '-'
    return output

CodePudding user response:

You don't track the characters that you already identified in the answer, you can add a tracker string to check for identified characters:

def process(guess: str, answer: str) -> str:
    output = ""
    already_identified_characters = set()
    for i, ch in enumerate(guess):
        if ch not in answer or ch in already_identified_characters:
            output  = "."
        elif ch != answer[i]:
            output  = "*"
        else:
            output  = "-"
        already_identified_characters.add(ch)
    return output

CodePudding user response:

If guess and answer are of equal length, this is how you could implement it:

def process(guess: str, answer: str) -> str:
    output = []
    misplaced_chars = set()
    for g,a in zip(guess,answer):
        if g == a:
            # Identical character on same location
            output.append('-')
        elif g in answer and g not in misplaced_chars:
            # Character exists in answer
            output.append('*')
            misplaced_chars.add(g)
        else:
            # Wrong guess
            output.append('.')
     return ''.join(output)

CodePudding user response:

Using Counter to keep track of the letters used in the answer, you can make sure that if letters are repeated in answer, they will work properly as well.

Basically, you keep track of each letter's count in the input, and subtract from it as you encounter matches.

from collections import Counter
def process(guess: str, answer: str) -> str:
    countAnswer = Counter(answer)
    output = ""
    for i,ch in enumerate(guess):
        if ch not in answer:
            output  = '.'
        elif countAnswer[ch]==0:
            output  = '.'
        elif ch != answer[i] and countAnswer[ch]!=0:
            output  = '*'
            countAnswer[ch]-=1
        else:
            output  = '-'
    return output

This should work out very similar to Wordle's treatment of repeated characters.

Various Inputs and their Outputs:

>>> process("crate","watch")
'*.**.'

>>> process("crate","slosh")
'.....'

>>> process("pious","slosh")
'..-.*'

>>> process("pesos","slosh")
'..***'
  • Related