String comparison and sorting function

SHR

I'm designing a guess word game and I need some help with one of the functions. The function receives 2 inputs and returns true or false.
The input my_word contains letters that were guessed and match to a certain word. The input other_word is some word to compare with my_input. examples:

>>> match_with_gaps("te_ t", "tact")
False
>>> match_with_gaps("a_ _ le", "apple")
True
>>> match_with_gaps("_ pple", "apple")
True
>>> match_with_gaps("a_ ple", "apple")
False

My problem is applying it to return a False as in the last example and I'm not sure how to do it. This is what I have done so far. It works but not for a case when a one guessed letter in my_word appears 2 time in other_word. In this case I'm returning true but it should be False. The inputs must be the exactly in the format as in the example (space after underscore).

def match_with_gaps(my_word, other_word):
    myWord = []
    otherWord = []
    myWord_noUnderLine = []
    for x in my_word:
        if x != " ": # remove spaces
            myWord.append(x)
    for x in myWord:
        if x != "_": # remove underscore
            myWord_noUnderLine.append(x)
    for y in other_word:
        otherWord.append(y)

    match = ( [i for i, j in zip(myWord, otherWord) if i == j] ) # zip together letter by letter to a set
    if len(match) == len(myWord_noUnderLine): # compare length with word with no underscore
        return True
    else:
        return False


my_word = "a_ ple"
other_word = "apple"

print(match_with_gaps(my_word, other_word))
benvc

You could create a "no space" version and a "no space, no underscore" version of your string and then compare each character to see if non underscore characters matched or if characters corresponding to an underscore have already been used. For example:

def match_with_gaps(match, guess):
    nospace = match.replace(' ', '')
    used = nospace.replace('_', '')
    for a, b in zip(nospace, guess):
        if (a != '_' and a != b) or (a == '_' and b in used):
            return False
    return True

print(match_with_gaps("te_ t", "tact"))
# False
print(match_with_gaps("a_ _ le", "apple"))
# True
print(match_with_gaps("_ pple", "apple"))
# True
print(match_with_gaps("a_ ple", "apple"))
# False

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python String Comparison function()

python string comparison function

Sorting key function that uses custom comparison

Error:"invalid comparator" when sorting using custom comparison function

Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function?

Should sorting algorithm pass same element in the comparison function

Sorting a list with a comparison function that doesn't follow 'strict weak ordering'

String comparison in python not working inside the function

Function for string column values comparison in RedShift

Bash string comparison not working on function call

My function returns a string but comparison is always wrong

Convert string comparison symbol to function in R

custom string sort comparison function weird behavior

Comparison Method for sorting by float

Sorting algorithm efficiency comparison

Sorting HTML attributes for comparison

Why does sorting an associative array by a boolean value using a comparison function reverse the array order?

how to pass a string variable to multcomp::glht function in multiple comparison in r

Arduino String comparison returning false... using .readStringUntil function

How to properly write SQLite update function with string comparison in the where cluse

R - using function to create new column based on string comparison

Failure to work on string comparison using function pointer in C

sorting matrix based on columns comparison

SQL Server STRING_AGG function sorting is not working as expected

Add filenames to an array and passing it to a sorting function as a string argument

String Comparison - Object with String

Comparison String to String

JavaScript sorting function not sorting correctly

string comparison failure for xml string comparison

TOP Ranking

HotTag

Archive