In responce to excercise https://bitbucket.org/dbuy/workspace/snippets/rnB4an .
main.py -- produces optimal output order on default string, contains json_output_optimal_order
optimal_order.py -- has the functions optimal_output_order, optimal_output_order2 the memory efficient solution.
test_optimal_order.py -- testcases for both functions.
To execute solution:
from where_ever_solution.main import json_output_optimal_order
json_output_optimal_order(json_input_string)