Skip to content

Instantly share code, notes, and snippets.

@sathyz
sathyz / fibanocci-closure.py
Created July 1, 2020 12:52
Find fibanocci using closures in python.
def fibonacci():
a, b = 0, 1
def f():
nonlocal a,b
a, b = b, a+b
return a
return f
if __name__ == '__main__':
f = fibonacci()
@sathyz
sathyz / index.html
Last active February 13, 2016 06:55
Average time spent by users on Internet
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>Take Two</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://dimplejs.org/dist/dimple.v2.1.6.min.js"></script>
</head>
<body>
<h3>Time Spent per Adult User per day</h3>
@sathyz
sathyz / libconfig_example.c
Created April 30, 2013 04:35
An example for libconfig
#include <libconfig.h>
/**
$ cat data.ini
position:{
x = 50;
y = 100;
}
$ gcc -lconfig libconfig_example.c
$ ./a.out
"""
Linked list implementation
"""
__author__ = "Satheesh Kumar Mohan <sathyz@gmail.com>"
class Node(object):
def __init__(self, value=None):
self.value = value
self.next = None
@sathyz
sathyz / numerology.py
Created August 25, 2011 16:10
find if the name matches the given totals for numerology
maps = [
'aijqy', #1
'bkr', #2
'cgls', #3
'dmt', #4
'ehnx', #5
'uvw', #6
'oz', #7
'fp', #8
@sathyz
sathyz / xml_to_dict.py
Created January 11, 2011 06:59
create dict from a given xml
from xml.dom.minidom import parse
def to_dict(node):
""" convert a given xmlnode to dict with its child tags as key and their
text as value"""
element = {}
for child in node.childNodes:
if child.hasChildNodes():
element[child.tagName] = child.firstChild.wholeText
#print element
@sathyz
sathyz / mydate.py
Created December 18, 2010 06:54
Date functions
import sys
import unittest
class InvalidDateException(Exception):pass
def is_leap(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
__author__ = "Satheesh Kumar Mohan <sathyz@gmail.com>"
MIN_WORD_SIZE=3
class Node(object):
"""Every node in the Trie tree. Node has a marker True/False saying if it
the end of the word or not and children will have the next successive
character of the word so on.
Say if a word is 'ipl', root['i']:False -> node['p]:False-> node['l']:True"""
#include <stdio.h>
int get_nearest_square(int n, int low, int high){
int low_sq, high_sq, mid;
if (high-low <= 1){
low_sq = low * low;
high_sq = high * high;
return (n - low_sq < high_sq - n)? low_sq: high_sq;
}
def get_max_prod3(seq):
"""Given a sequence, find the 3 numbers which has max product."""
min_seq = [1, 1]
max_seq = [-1, -1, -1]
for i in seq:
if i < 0:
if i < min_seq[0]:
min_seq[0] = i
elif i < min_seq[1]:
min_seq[1] = min_seq[0]