Reputation: 45
For a school project I have to create a wordle in Python. The program I created compares the letters of the target word and the guessed word. An example: the target word is "compulsory" and the guessed word is "submission". The output should be "-O-O--X-O-".
"X" meaning the letter in the guessed word is also in the target word and is at the same spot. "O" meaning the letter in the guessed word is also in the target word but isn't at the same spot. "-" means that it's the wrong letter.
But I can't seem to find a way to cope with duplicate letters. For example, if the target word is "teethe" and the guessed word is "health" it should give me the output: "OX--O-". Though my program returns the output: "OX--OO".
My code:
def compare(guess, target):
output = ""
for i in range(target.__len__()):
if guess[i] == target[i]:
output = output + "X"
elif guess[i] in target:
output = output + "O"
else:
output = output + "-"
return output
print(compare("health", "teethe"))
I hope I provided enough information for you to understand my question/problem.
Thank you in advance for your help!
Upvotes: 0
Views: 2573
Reputation: 497
Here is a compact solution:
def compare(guess, target):
output = ["X" if target[i]==guess[i] else "-" for i in range(len(guess))]
for tLetter in [letter for (k,letter) in enumerate(target) \
if letter != guess[k]]:
idx = next((j for (j,gLetter) in enumerate(guess) if gLetter==tLetter\
and output[j]=="-"),None)
if idx != None:
output[idx]="O"
return "".join(output)
Upvotes: 0
Reputation: 21
eccentricOrange - thanks for the Code Help. I was able to utilize the Python code and convert it to Visual Basic. So for those that need a VB version here's the code I came up with.
Function Compare(Guess As String, Target as String) As String
Dim Output As String = "-----"
'Match all of the Exact Match Characters first
For i = 1 To 5
If Guess.Substring(i - 1, 1) = Target.Substring(i - 1, 1) Then
Output = Output.Remove(i - 1, 1).Insert(i - 1, "X")
Target = Target.Remove(i - 1, 1).Insert(i - 1, "-")
End If
Next
'Next Match Characters in the word but in the wrong spot
For i = 1 To 5
For j = 1 To 5
If (Guess.Substring(j - 1, 1) = Target.Substring(i - 1, 1)) And Output.Substring(j - 1, 1) = "-" Then
Output = Output.Remove(j - 1, 1).Insert(j - 1, "O")
Target = Target.Remove(i - 1, 1).Insert(i - 1, "-")
End If
Next
Next
Compare = Output
End Function
Upvotes: 1
Reputation: 1232
This took me a while to solve, but I got it I think.
Here's the solution the way I implemented it:
def compare(guess: str, target: str):
output = ["-"] * len(target)
for index, (guess_letter, target_letter) in enumerate(zip(guess, target)):
if guess_letter == target_letter:
output[index] = "X"
target = target.replace(guess_letter, "-", 1)
for index, (guess_letter, target_letter) in enumerate(zip(guess, target)):
if guess_letter in target and output[index] == "-":
output[index] = "O"
target = target.replace(guess_letter, "-", 1)
return ''.join(output)
print(compare('health', 'teethe'))
print(compare('submission', 'compulsory'))
Here's an identical solution that uses simpler (but less "Pythonic") tools:
def compare(guess, target):
length = len(target)
output = ["-"] * length
for index in range(length):
if guess[index] == target[index]:
output[index] = "X"
target = target.replace(guess[index], "-", 1)
for index in range(length):
if guess[index] in target and output[index] == "-":
output[index] = "O"
target = target.replace(guess[index], "-", 1)
return ''.join(output)
print(compare('health', 'teethe'))
print(compare('submission', 'compulsory'))
Output:
OX--O-
-O-O--X-O-
Here are my key techniques:
'X'
. I'm initially maintaining this as a list, then converting it to a string when returning.'-'
.'X'
) first.Upvotes: 3
Reputation: 1
It is probably not the most beautiful way to solve it. But one way you could to, it is to save the letter that are in the target an extra string, so you can "remember" them. After that you can add the the string into your elif, if the guess letter is not in the array, it adds an "O", if it is, the else will be computed and "-" will be added. This should process your wished out come. Hope that helped
def compare(guess, target):
output = ""
isGuessInTarget = ""
for i in range(target.__len__()):
if guess[i] == target[i]:
output = output + "X"
elif (guess[i] in target) and not (guess[i] in isGuessInTarget):
output = output + "O"
isGuessInTarget = isGuessInTarget + guess[i]
else:
output = output + "-"
return output
print(compare("health", "teethe"))
Upvotes: 0