diff options
Diffstat (limited to 'challenge-263/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-263/sgreen/python/ch-2.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-263/sgreen/python/ch-2.py b/challenge-263/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a3a6d6a181 --- /dev/null +++ b/challenge-263/sgreen/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from collections import defaultdict +import json +import sys + + +def merge_items(*arrays) -> list: + # Calculate the total of each items + totals = defaultdict(int) + for array in arrays: + for item in array: + item_id, item_qty = item + totals[item_id] += item_qty + + # Turn this into a sorted list of item_id and item_qty pairs + return [[item, totals[item]] for item in sorted(totals)] + + +def main(): + # Convert each input into a list of lists + arrays = [] + for json_str in sys.argv[1:]: + arrays.append(json.loads(json_str)) + result = merge_items(*arrays) + print(json.dumps(result)) + + +if __name__ == '__main__': + main() |
