Skip to content

Commit

Permalink
Update basic-calculator-iv.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored May 24, 2021
1 parent d7ba824 commit d5cac7c
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions Python/basic-calculator-iv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,35 @@
import operator


def clear(result):
to_remove = [k for k, v in result.iteritems() if v == 0]
for k in to_remove:
result.pop(k)
return len(to_remove)

class Poly(collections.Counter):
def __init__(self, expr=None):
if expr is None:
return
if expr.isdigit():
self.update({(): int(expr)})
if int(expr):
self.update({(): int(expr)})
else:
self[(expr,)] += 1

def __add__(self, other):
self.update(other)
return self
result = Poly()
result.update(self)
result.update(other)
clear(result)
return result

def __sub__(self, other):
self.update({k: -v for k, v in other.iteritems()})
return self
result = Poly()
result.update(self)
result.update({k: -v for k, v in other.iteritems()})
clear(result)
return result

def __mul__(self, other):
def merge(k1, k2):
Expand All @@ -51,6 +64,7 @@ def merge(k1, k2):
for k1, v1 in self.iteritems():
for k2, v2 in other.iteritems():
result.update({tuple(merge(k1, k2)): v1*v2})
clear(result)
return result

def eval(self, lookup):
Expand All @@ -63,13 +77,13 @@ def eval(self, lookup):
else:
key.append(var)
result[tuple(key)] += c
clear(result)
return result

def to_list(self):
return ["*".join((str(v),) + k)
for k, v in sorted(self.iteritems(),
key=lambda x: (-len(x[0]), x[0]))
if v]
key=lambda x: (-len(x[0]), x[0]))]


class Solution(object):
Expand Down

0 comments on commit d5cac7c

Please sign in to comment.