The Question
is_match:
(str, str) -> bool
The first parameter is a puzzle and the second is a view. Return True iff the view could be a view of the given puzzle.
My Answer
I came up with this:
def is_match(puzzle, view):
if len(puzzle) != len(view):
return False
if len(puzzle) == len(view):
return True
I also found this online:
def is_match(given_puzzle, view):
if len(given_puzzle) != len(view):
return False
unique_letters = set(ch for ch in view if ch != '^')
for (a, b) in zip(given_puzzle, view):
if a in unique_letters and a != b:
return False
return True
This issue I am having is that if I input:
is_match('blah', 'tr^^')
It will return True for both of the given codes. Which must be wrong due to the fact that the characters don’t even match only the length of the string does
What could i do to fix this?
Based on a previous string of questions about this “puzzle” game, I infer that you want something like:
*Note that this assumes that
puzzleandviewhave the same length.What this does is iterate over puzzle and view at the same time, yielding the next character from each as
pandv(the magic ofzip). I check to make sure thatvis not'^'(the placeholder character). If it isn’t a placeholder, I check if it equalsp(the character frompuzzle). If at any time this latter check fails, then the function returnsFalse. Otherwise, it returnsTrue.