compresstrie.py (403B)
1 #!/usr/bin/env python3 2 3 """Read json trie in stdin, trim unneeded branches and output json dump 4 to stdout""" 5 6 import json 7 import sys 8 9 trie = json.load(sys.stdin) 10 11 def compress(trie): 12 """Compress the trie""" 13 if len(trie[0].keys()) <= 1: 14 # no need for children, there is no more doubt 15 trie[1] = {} 16 for child in trie[1].values(): 17 compress(child) 18 19 compress(trie) 20 21 print(json.dumps(trie)) 22