munch2024 / munch

2 stars 11 forks source link

Task 2.3: Understanding Complex Code #104

Closed StacL closed 8 months ago

StacL commented 8 months ago

Description

This code is a solution found online as one of the public solutions for Leetcode #20. Valid Parentheses. The code checks if a sequence of parenthesis, brackets, and braces are valid in a single line. However, condensing the code into a single line makes it difficult to read.

Goals to Fix:

Code

class Solution:
    def isValid(self, s: str) -> bool:
        return (x := []) or not (b := {'(': ')', '{': '}', '[': ']'}) or (not (sum([1 for c in s if (c not in b or x.append(b[c])) and not (x and c == x.pop())]) or x))