mercredi 2 septembre 2020

Python - find matching characters in two strings with same and different positions

I want to return a score for if a character in the user's guess is the same as a character in the substring of the word that they're guessing. There are different scores for vowels and consonants being guessed and whether they are in the correct positions or not.

So for example:

# vowel in wrong index
>>> def compute_value_for_guess('aecdio', 3, 4, 'ix')
5
# vowel in correct index
>>> def compute_value_for_guess('aecdio', 3, 4, 'xi')
14
# multiple matches
>>> def compute_value_for_guess('aecdio', 1, 4, 'cedi')
36

This is my attempt so far:

VOWELS = "aeiou"
CONSONANTS = "bcdfghjklmnpqrstvwxyz"

def compute_value_for_guess(word, start_index, end_index, guess):
    """
    word(str): word being guessed
    start_index, end_index(int): substring of word from start_index up to and including 
    end_index
    guess(str): user's guess of letters in substring
    """
    score = 0
    for ia in range(len(word[start_index:end_index+1])):
        for ib in range(len(guess)):
            if word[ia] in VOWELS and guess[ib] in VOWELS:
                if guess[ib] in word[start_index:end_index+1]:
                    if ia == ib:
                        score += 14
                    elif ia != ib:
                        score += 5
                else:
                    score += 0
            elif word[ia] in CONSONANTS and guess[ib] in CONSONANTS:
                    if guess[ib] in word[start_index:end_index+1]:
                        if ia == ib:
                            score += 12
                        elif ia != ib:
                            score += 5
                    else:
                        score += 0

I'm getting some values for vowels but they are not correct and it keeps returning 0 for consonants.

Aucun commentaire:

Enregistrer un commentaire