I am trying to test my code by calling the lint() method and passing '{[()]}' as the text argument. I am not familiar with classes, and I am getting TypeError: lint() missing 1 required positional argument: 'text'.
class Stack: def __init__(self): self.stack = [] def push(self, element): self.stack.append(element) def pop(self): if len(self.stack) == 0: return None else: return self.stack.pop() def read(self): if len(self.stack) == 0: return None else: return self.stack[-1] class Linter: def __init__(self): # We use a simple array to serve as our stack: self.stack = Stack() def lint(self, text): # We start a loop which reads each character in out text: for t in text: braces = {'(': ')', '[': ']', '{': '}'} # If the character is an opening brace: if t == '(' or t == '[' or t == '{': # We push it onto the stack: self.stack.push(t) # If the character is a closing brace: elif t == ')' or t == ']' or t == '}': # Pop from stack: popped_opening_brace = self.stack.pop() # If the stack was empty, so what we popped was None, # it means that an opening brace is missing: if not popped_opening_brace: return f'{t} doesnt have opening brace' # If the popped opening brace doesn't match the # current closing brace, we produce and error: if t != braces[popped_opening_brace]: return f'{t} has a mismatched opening brace' # If we get to the end of line, and the stack isn't empty: if self.stack.read(): # It means we have an opening brace without a # corresponding closing brace, so we produce an error: return f'{self.stack.read()} does not have a closing brace' # Return true if line has no errors: return True Linter.lint('{[()]}') Linter.lint(self, '{[()]}') doesn't work either.
mylinter = Linter()mylinter.lint('{[()]}')self.stack = Stacklikeself.stack = Stack()@classmethoddecorator. The implication of that is that your method will not be able to accessself, so you will need to update your method to not need that.