All problems
CRDT PN-Counter Merge
Problem
Merge two PN-Counters from different replicas.
A PN-counter tracks positive and negative increments separately, and merging takes the element-wise maximum of both sides. a and b are dictionaries with "pos" and "neg" totals.
Return [merged_pos, merged_neg, merged_value].
Hint
Taking the maximum of each component makes the merge idempotent and commutative.
Test cases
case 1
in: [{"pos":3,"neg":1},{"pos":2,"neg":4}]
expected: [3,4,-1]
case 2
in: [{"pos":0,"neg":0},{"pos":5,"neg":2}]
expected: [5,2,3]
case 3
in: [{"pos":2,"neg":2},{"pos":2,"neg":2}]
expected: [2,2,0]
case 4
in: [{"pos":7,"neg":0},{"pos":1,"neg":1}]
expected: [7,1,6]
Starter codePython
def pn_counter_merge(a, b):
# Your code here
pass4 test cases · run your code against them in the editor