#RK3 not working but I swear the logic is right

def match_pattern(input_line, pattern):
    if len(pattern) == 1:
        return pattern in input_line
    elif pattern == "\d":
        return any(char.isdigit() for char in input_line)
    elif pattern == "\w": 
        return any(char.isalnum() or char == '_' for char in input_line)
    elif pattern[0] == '[' and pattern[-1] == ']':
        return any(True for char in input_line if char in pattern[1:-1])
    elif pattern.startswith("[^") and pattern.endswith("]"):
        return not any(char in pattern[2:-1] for char in input_line)
    else:
        raise RuntimeError(f"Unhandled pattern: {pattern}")

I dont see anything wrong with this line of code in terms of returning a negative character group.

    elif pattern[0:2] == "[^" and pattern[-1] == "]":
        return not any(char in pattern[2:-1] for char in input_line)

Inside the any function I have a list comprehension which I think make sense and the only thing I can think is wrong is the condition but I am truly stuck on this problem.

Any help would be appreciated. Thanks

Hey @MomentumSC, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

I found the problem it is ok. I should have switch the position of these two conditional statements and changed the any() function to all() instead. Thank you for offering your help though.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.