Skip to content

Instantly share code, notes, and snippets.

@pixyj
pixyj / tree.py
Created October 6, 2012 16:06
Analysis of Binary Search Tree Insertion Time
class Node:
def __init__(self,val,parent=None):
self.val = val
self.parent = parent
self.left = None
self.right = None
def setLeft(self,node):
self.left = node
@pixyj
pixyj / django_one_step.sh
Created October 27, 2012 10:11
Script to create django project with virtualenv
#!/bin/bash
#Take project name as input
if [ -z "$1" ]
then
echo "Enter project name"
read proj
else
proj="$1"
fi
@pixyj
pixyj / django_log_sql.py
Created October 28, 2012 04:09
Log Sql queries in Django
#To get all sql queries sent by Django from py shell
import logging
l = logging.getLogger('django.db.backends')
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())
#For more info, http://stackoverflow.com/questions/971667/django-orm-how-to-view-or-log-the-executed-query
def ways(amount, coin_types):
"""
Python implementation for the counting_change problem
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2
Doesn't work for large numbers because we don't have tail call optimization in python
Time complexity analysis: Found an explanation at
http://wqzhang.wordpress.com/2009/06/09/sicp-exercise-1-14/
@pixyj
pixyj / validate_properties.js
Last active December 14, 2015 23:49
Check if a javascript object(instance) has the same properties of the same type with respect to a known object(schema).
/*******************************************************************************************************
Checks if an object(instance) has the same properties of the same type with respect to a known
object(schema).
validate is especially useful to test whether the server side code sends JSON as expected by the client
Usage Example:
1. Create a sample expected object (sampleSchema).
2. When data(sampleInstance) is received, call validate(sampleSchema, sampleInstance)
@pixyj
pixyj / Makefile
Last active February 10, 2023 09:48
Simple HTTP client in C
all:
gcc -Wall http_client.c -o http_client
clean:
rm *.o;rm http_client
@pixyj
pixyj / count_dom_nodes.js
Created June 14, 2013 09:15
Calculate total number of nodes in your web page.
var countNodes = function() {
var total = 1;
var count = function(node) {
var i, l;
l = node.children.length;
total += l;
for(i=0; i<l; i++) {
count(node.children[i])
}
}
@pixyj
pixyj / tour_35.go
Last active December 19, 2015 08:49
A tour of Go # 35 Straight line with slope == 1
import "fmt";
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
phew := make([][]uint8, dx)
for i := range phew {
phew[i] = make([]uint8, dy)
for j := range phew[i] {
@pixyj
pixyj / newtonraphson.go
Last active April 19, 2017 14:28
Solution to http://tour.golang.org/#24. Find the square root a number using Newton Raphson method
package main
import (
"fmt"
)
func approx(z, x float64) float64 {
z = z - (z*z - x)/2/z
return z
@pixyj
pixyj / equi.go
Created September 15, 2013 06:21
Equivalent binary trees in Go Solution to http://tour.golang.org/#70
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"math/rand"
)