Created
January 31, 2015 17:34
-
-
Save shutej/2c327545dfcfe6a138ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
def query(c): | |
return sum(c.itervalues()) | |
def merge(c1, c2): | |
return {k: max(c1.get(k, 0), c2.get(k, 0)) for k in set(c1).union(c2)} | |
class tx(object): | |
def __init__(self, id): | |
self.id = id | |
def add(self, c, n=1): | |
assert n >= 0 | |
c = dict(c) | |
c.setdefault(self.id, 0) | |
c[self.id] += n | |
return c | |
c1 = dict(tx1=7) | |
print "c1", query(c1) | |
c2 = tx(2).add(c1) | |
print "c2", query(c2) | |
c3 = tx(3).add(c1) | |
print "c3", query(c3) | |
c4 = merge(c2, c3) | |
print "c4", query(c4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment