Tag Archives: Python

Optimization using Hill Climbing

The following example shows how to find the best portfolio configuration given that only the price of a single stock can be incremented by 10 cents at a time. Since we are using Hill Climbing technique, we can find the local optimal value easily but the same cannot be said for finding the global optimal […]

0  

Prefix Tree

Creating a prefix tree is quite easy in Python! root = {} def addToTree(node, word=()): if not word: return addToTree(node.setdefault(word[0], {}), word[1: ]) def main(): for word in (‘batman’, ‘bane’, ‘bale’,): addToTree(root, word) print root Output: {‘b’: { ‘a’: { ‘l’: { ‘e’: {} }, ‘n’: { ‘e’: {} }, ‘t’: { ‘m’: { ‘a’: […]

0  

N-dimensional aka N-ary Tree

N-dimensional tree is used to represent the UI of any application. Buttons, ComboBoxes and various other components are assembled together in this data structure and then rendered on the screen. I had to enhance the design of a commercial Tree UI component (add improved searching features) and thought of creating my own n-ary Tree data […]

1  

Achieving optimal performance in code

Is there any difference between the following two expressions\statements? def func1(a): return a == ‘TRUE’ def func2(a): return True if a == ‘TRUE’ else False Well, both of them do the same thing but interestingly, the former one is faster in terms of performance by atleast one CPU cycle! Let’s tear into assembly language of […]

0  

Logging in Java, C# and Python

In Java & C#, the most commonly used logging utilities are log4j and log4net. Python has it’s own inbuilt logging module which includes the exact same features as other log4X brethren! Setting up logging module in python >> import logging def initLogger(appName=’Application’, handlerType=’StreamHandler’, \ loggerLevel=’INFO’, handlerLevel=’DEBUG’): ”’ * There are many handler types available such […]

0