Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
nsfyn55 / tree.py
Created July 31, 2013 17:20
Printable Tree
import math
from collections import deque
class Node:
balanceFactor = 0
height = 0
lchild = None
rchild = None
def __init__(self, key):
self.key = key
@nsfyn55
nsfyn55 / base_tree.py
Created July 1, 2013 14:23
Base Binary Tree with Printing
import math
from collections import deque
class Node:
lchild = None
rchild = None
def __init__(self, key):
self.key = key
class Tree:
@nsfyn55
nsfyn55 / bubble-sort.py
Last active December 19, 2015 02:48
iterative bubble sort
def sort(l):
for x in range(len(l)-1):
for i in range(len(l)-(x+1)):
if l[i] > l[i+ 1]:
swap(l, i, i+1)
return l
def swap(l, index1, index2):
buff = l[index1]
l[index1] = l[index2]
@nsfyn55
nsfyn55 / quicksort.py
Created June 27, 2013 14:20
recursive quicksort
def sort(l):
if len(l) <= 1 :
return l
left = []
right = []
pivot = l[len(l)/2]
@nsfyn55
nsfyn55 / merge-sort.py
Last active December 18, 2015 19:19
Python Merge Sort
def merge(l1, l2):
result = []
pos_left = 0
pos_right = 0
for i in range(len(l1) + len(l2)):
if(pos_left >= len(l1)):
result = result + l2[pos_right:len(l2)]
break
@nsfyn55
nsfyn55 / beachline.html
Created December 26, 2012 15:08
Visualization of Beach Line in fortune's algorithm
<!--
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"/>
<title>Page Title</title>
<script type="text/javascript" src="d3.js"></script>
<link rel="stylesheet" type="text/css" href="test.css">
</head>
@nsfyn55
nsfyn55 / compiler.py
Created December 17, 2012 23:02
Interview Question Expression Compiler
"""Expression Compiler/Evaluator
This is a simple expression compiler.
It has the following limitations
- supported operation: +,-,/,%,
- only supports binary operations enclosed in ()
- ((1+3) + (1+3)) -- valid
- (1+3) + (1+3) -- invalid
- does not support implict multiplication
- (1(1+3)) -- bad juju
@nsfyn55
nsfyn55 / compose.scala
Created November 29, 2012 18:47
Composable Functions
object App {
def main (args: Array[String]){
val fp = candidate(_)
val f = decorate(fp,decorator) // F o G
println(f("Hello World"))
println(fp("Hello World"))
@nsfyn55
nsfyn55 / byname.scala
Created November 26, 2012 19:09
By Name Parameter
/*
a by name parameter is not evaluated at the point of function application
but rather it is evaluated at each use within the function
*/
object App {
def main (args: Array[String]){
println(delayed(nano()))
}
def nano() ={
@nsfyn55
nsfyn55 / contravariance.scala
Last active October 13, 2015 04:38
Contravariance Example
/*
Invariant:
String is subclass of Object but in java
List<String> is not a subclass of List<Object>
Covariant
String in scala is a subclass of Object
List[String] is a subclass of List[Object]