Skip to content

Instantly share code, notes, and snippets.

@pixyj
pixyj / hero.js
Created November 28, 2013 06:48
Why you shouldn't miss new when invoking a constructor function in javascript
//taken from Object Oriented Javascript by Stoyan Stefanov
var Hero() {
this.occupation = "Ninja!"
this.say = function() {
return "I'm a " + this.occupation;
}
}
/* correct invocation of constructor function
@pixyj
pixyj / psycopg2_install_dependencies.sh
Last active December 28, 2015 08:09
Install these psycopg2 dependencies on Ubuntu before pip install psycopg2 to fix below error: In file included from psycopg/psycopgmodule.c:27:0: ./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory compilation terminated. error: command 'gcc' failed with exit status 1
sudo apt-get install libpq-dev
#I'm running postgresql-9.1. Match the version to that of your server.
sudo apt-get install postgresql-server-dev-9.1
sudo apt-get install python-dev
#Now workon your virtualenv and pip install psycopg2
@pixyj
pixyj / iptables_firewall_rules
Created November 14, 2013 01:41
Sample iptables firewall settings from http://feross.org/how-to-setup-your-linode/
*filter
# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
# Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow all outbound traffic - you can modify this to only allow certain traffic
@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"
)
@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 / 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 / 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 / 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 / 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)
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/