Skip to content

Instantly share code, notes, and snippets.

View sahands's full-sized avatar

Sahand Saba sahands

View GitHub Profile
@sahands
sahands / Range.java
Last active August 29, 2015 13:58
Simple example of an iterable in Java that behaves similar to Python 3's "range" iterator.
import java.util.NoSuchElementException;
import java.util.Iterator;
public class Range implements Iterable<Integer> {
private int start, end;
public Range(int start, int end) {
this.start = start;
this.end = end;
}
@sahands
sahands / HashExample.java
Last active August 29, 2015 13:57
Simple use of hashing to create a generic hash set class.
// A relatively simple example of a hash set class with support for generic
// types. Since the point was to demonstrate the use of hash functions and
// collision resolution (chaining, in this case), we implemented a hash set
// instead of a table, which has a simpler, more straight-forward
// implementation. A hash table can be implemented in almost the exact same
// way, but by storing pairs of (key, item) in the linked lists instead of just
// the items.
import java.util.LinkedList;
import java.util.ArrayList;
@sahands
sahands / python_sjt_coroutines.py
Last active June 8, 2022 19:47
Steinhaus–Johnson–Trotter (SJT) Permutation Generation Algorithm in Python using Coroutines (Generators)
from time import sleep
__author__ = "Sahand Saba"
def nobody():
while True:
yield False
@sahands
sahands / SllRemove.java
Last active October 2, 2018 03:41
Java - Singly-Connected Linked List Remove All Method
/*
* PURPOSE:
* Remove all instances of value from the list.
*
* PRECONDITIONS:
* None.
*
* Examples:
* If l is {67,12,13,12} then after l.remove(12), l is {67,13}
* If l is {1,2,3} then after l.remove(2), l is {1,3}
@sahands
sahands / cached.py
Last active December 19, 2015 23:19
A Study of Python's More Advanced Features: Part II - Closures, Decorators and functools
import time
from functools import wraps
def cached(timeout, logged=False):
"""Decorator to cache the result of a function call.
Cache expires after timeout seconds.
"""
def decorator(func):
@sahands
sahands / snowflake.js
Last active December 19, 2015 20:58
Snowflake - Fractal Rotational Symmetry
var DEPTH = 5;
var MAX_FLAKES = 8;
var MIN_FLAKES = 2;
function SnowflakeGenerator(canvasElement) {
this.element = canvasElement;
this.width = parseInt(this.element.getAttribute("width"));
this.height = parseInt(this.element.getAttribute("height"));
this.randomParameters = [];
for (i = 0; i <= DEPTH; i++) {
@sahands
sahands / mandelbrot.js
Last active December 19, 2015 20:49
HTML5 Canvas and Javascript Mandelbrot Fractal
function Complex(real, imaginary) {
this.a = real;
this.b = imaginary;
}
Complex.prototype.abs = function() {
return Math.sqrt(this.a * this.a + this.b * this.b);
};
Complex.prototype.toString = function() {
@sahands
sahands / anagrams.py
Last active December 19, 2015 19:18
Interview Question: Grouping Word Anagrams (Facebook)
import collections
def sort_hash(word):
return ''.join(sorted(word))
def count_letters_hash(word):
d = collections.defaultdict(int)
for c in word:
@sahands
sahands / collatz.py
Last active December 19, 2015 16:08
Python's More Advanced Features: Part I - Iterators, Generators, and itertools
def collatz(n):
m = n
while True:
yield m
if m == 1:
raise StopIteration
if m % 2 == 0:
m /= 2
else:
m = 3 * m + 1
@sahands
sahands / extractor.py
Last active December 19, 2015 15:38
Fair Coin from Unfair Coin - Groupon Interview Question
import random
def bernoulli_process(p):
if p > 1.0 or p < 0.0:
raise ValueError("p should be between 0.0 and 1.0.")
while True:
yield random.random() < p