Problem solution · Python

Basic Calculator IV

Basic Calculator IV: a Python solution using sorting and greedy selection. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sorting and greedy selection
Source
walkccc LeetCode Solutions
Length
147 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sorting and greedy selection

For Basic Calculator IV, the implementation first exposes a useful order, then scans that order while making locally justified choices.

  1. Choose the key that reveals the greedy or grouping structure.
  2. Sort the relevant records by that key.
  3. Scan in order, maintaining the invariant that makes each local choice safe.

Code notes

  • 147 lines of Python from the credited upstream file 770.py.
  • The implementation visibly relies on sequence storage, hash lookup.
  • No explicit loop blocks detected.

Complexity

Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeBasic Calculator IV · PythonPython
Use this to learn the idea, then write your own version.
class Poly:  def __init__(self, term: str = None, coef: int = None):    if term and coef:      self.terms = collections.Counter({term: coef})    else:      self.terms = collections.Counter()   def __add__(self, other):    for term, coef in other.terms.items():      self.terms[term] += coef    return self   def __sub__(self, other):    for term, coef in other.terms.items():      self.terms[term] -= coef    return self   def __mul__(self, other):    res = Poly()    for a, aCoef in self.terms.items():      for b, bCoef in other.terms.items():        res.terms[self._merge(a, b)] += aCoef * bCoef    return res   # Def __str__(self):  #   res = []  #   for term, coef in self.terms.items():  #     res.append(term + ': ' + str(coef))  #   return '{' + ', '.join(res) + '}'   def toList(self) -> list[str]:    for term in list(self.terms.keys()):      if not self.terms[term]:        del self.terms[term]     def cmp(term: str) -> tuple:      # the minimum degree is the last      if term == '1':        return (0,)      var = term.split('*')      # the maximum degree is the first      # Break ties by their lexicographic orders.      return (-len(var), term)     def concat(term: str) -> str:      if term == '1':        return str(self.terms[term])      return str(self.terms[term]) + '*' + term     terms = list(self.terms.keys())    terms.sort(key=cmp)    return [concat(term) for term in terms]   def _merge(self, a: str, b: str) -> str:    if a == '1':      return b    if b == '1':      return a    res = []    A = a.split('*')    B = b.split('*')    i = 0  # A's index    j = 0  # B's index    while i < len(A) and j < len(B):      if A[i] < B[j]:        res.append(A[i])        i += 1      else:        res.append(B[j])        j += 1    return '*'.join(res + A[i:] + B[j:])  class Solution:  def basicCalculatorIV(      self,      expression: str,      evalvars: list[str],      evalints: list[int],  ) -> list[str]:    tokens = list(self._getTokens(expression))    evalMap = {a: b for a, b in zip(evalvars, evalints)}     for i, token in enumerate(tokens):      if token in evalMap:        tokens[i] = str(evalMap[token])     postfix = self._infixToPostfix(tokens)    return self._evaluate(postfix).toList()   def _getTokens(self, s: str) -> Iterator[str]:    i = 0    for j, c in enumerate(s):      if c == ' ':        if i < j:          yield s[i:j]        i = j + 1      elif c in '()+-*':        if i < j:          yield s[i:j]        yield c        i = j + 1    if i < len(s):      yield s[i:]   def _infixToPostfix(self, tokens: list[str]) -> list[str]:    postfix = []    ops = []     def precedes(prevOp: str, currOp: str) -> bool:      if prevOp == '(':        return False      return prevOp == '*' or currOp in '+-'     for token in tokens:      if token == '(':        ops.append(token)      elif token == ')':        while ops[-1] != '(':          postfix.append(ops.pop())        ops.pop()      elif token in '+-*':  # isOperator(token)        while ops and precedes(ops[-1], token):          postfix.append(ops.pop())        ops.append(token)      else:  # isOperand(token)        postfix.append(token)    return postfix + ops[::-1]   def _evaluate(self, postfix: list[str]) -> Poly:    polys: list[Poly] = []    for token in postfix:      if token in '+-*':        b = polys.pop()        a = polys.pop()        if token == '+':          polys.append(a + b)        elif token == '-':          polys.append(a - b)        else:  # token == '*'          polys.append(a * b)      elif token.lstrip('-').isnumeric():        polys.append(Poly("1", int(token)))      else:        polys.append(Poly(token, 1))    return polys[0] 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗