Posts

Showing posts with the label Algorithm

Python Algorithm: create Object from JSON

As shown in the following example, you can use the @wrap (which return another wrapper) to to transfer JSON to object in the python. This blog written conjunction with the Python Algorithm to flattening JSON 1 . from functools import wraps def json_to_object(func): @wraps(func) def wrapper(self, d): for name, value in d.iteritems(): setattr(self, name,value) return func(self, d) return wrapper class Person(object): @json_to_object def __init__(self, d): pass a = Person({'firstName':'Tom', 'lastName':'Hanks', 'age':50}) a.firstName a.lastName a.age More advanced version from functools import wraps def json_to_object(func): @wraps(func) def wrapper(self, d): for name, value in d.iteritems(): if type(value) == dict: print(value) setattr(self,name,Person.fromJson(value)) else: setattr(self, nam...

Python Algorithm to flattening JSON

Algorithm to flatten the deep JSON structure: def flat(root, **args): d = {} for k, v in args.iteritems(): if type(v) == dict: d.update(flat((k if root == None else '{}.{}'.format(root,k)),**v)) elif type(v) == list: for idx, item in enumerate(v): d.update(flat('{}.{}'.format(k if root == None else '{}.{}'.format(root,k),idx), **item)) else: if root == None: d['{}'.format(k)] = v else: d['{}.{}'.format(root,k)] = v #print ('key: {}, val: {}'.format(k,v)) return d for example, if you flatten the following JSON structure: tt ={'name':{'firstName':'Tom', 'lastName':'Hanks'}, 'orderlineitems':[{'rice':{'qty':2,'price':10}},{'bread':{'qty':1,'price':2}}], 'age':20, 'location'...

Algorithm Analysis

Algorithm Analysis measure the speed of the task algorithm suppose to accomplished. Here the necessary maths back ground to analyse the algorithmic complexity. CONTENTS 12 -fold way Order of the Growth Algorithm Analysis of Sorting Algorithm Analysis of Searching Appendix There are two basic rules in the Algorithm analysis: Rule of sum: if an action is performed making A choices or B choices, then it can be performed \(A+B\) ways. Rule of product: If an action can be performed by making A choices followed by B choices, then it can be performed \(A\times B\) ways. Permutation: an ordered list where every object appears exactly once \[ _{n}P_{r} = \frac {n!} {(n - r)!} \text{ where } !0 = 1 \] combinations: When order is not important and the repetition is not allowed, the number of ways to choose k from the distinct n is as for \(n \ge k \ge 0\): \[ {n \choose k }= \frac {n!}{ k!(n-k)! } \] in general, choosing k out of n is same as not cho...

Recommender System Notes.

These are the notes I've achieved certificate from the well known Recommender System course .