This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
__author__ = "Sahand Saba" | |
def nobody(): | |
while True: | |
yield False | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
def sort_hash(word): | |
return ''.join(sorted(word)) | |
def count_letters_hash(word): | |
d = collections.defaultdict(int) | |
for c in word: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |