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
<?php | |
$range_count = array_fill(0, 100, 0); | |
$start_time = microtime(true); | |
for($i = 0;$i < 1000000;++$i) { | |
$randint = mt_rand(0, 999); | |
$range_count[$randint/10]++; | |
} | |
$end_time = microtime(true); | |
$duration1 = $end_time - $start_time; |
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.*; | |
/* | |
* Example of Observer Pattern: | |
* Student is the model | |
* View1 and View2 display information of the student | |
* View3 changes information of the student | |
* Whenever the student is changed, the views displaying it should be updated | |
*/ |
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.LinkedList; | |
import java.util.List; | |
/* | |
* A solution using Proxy Pattern to limited instances problem | |
* Requirement: | |
* class RareResource must have no more than 3 instances at any run time. | |
* Note: | |
* Black magic like reflection is out of consideration as you can never prevent it. | |
*/ |
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
/** | |
* What's the average status length in current Fanfou page? | |
* Author: @kavinyao | |
* MIT-licensed. | |
*/ | |
(function(document) { | |
function map(op, seq) { | |
var result = [] | |
for(var i = 0;i < seq.length;i++) | |
result.push(op(seq[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
#include <stdio.h> | |
const char* my_strstr(const char* haystack, const char* needle) { | |
if (!*needle) | |
return haystack; | |
const char* h = haystack; | |
const char* n = needle; | |
while (*h) { | |
const char* hp = h; |
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
for f in (find . -name '*.js') | |
head -1 $f | xxd | grep 'efbb bf' > /dev/null | |
# if matched a line, $status == 0 | |
if test $status -eq 0 | |
echo $f | |
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
# Demo code in coffeescript for Callbacks are imperative, promises are functional: Node’s biggest missed opportunity | |
# http://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/ | |
# the original version uses rsvp.Promise | |
# since I'm a control freak, I'd prefer Deferred | |
Deferred = require 'deferred' | |
slice = Array.prototype.slice | |
# transform a callback-based function to return a promise | |
# fn == function(param..., function(error, data...)) |
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
# This piece of code is a rebound of https://github.com/ForbesLindesay/curry | |
# Author: Kavin Yao <[email protected]> | |
# License: MIT | |
import inspect | |
def curry(func, num_params=-1): | |
"""Simple, unlimited curry. | |
The curried function is not called until desired number of arguments is passed. | |
Note: **kwarg is not supported | |
""" |
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 sys | |
import math | |
import time | |
def progress_bar(progress, col_width=80): | |
""" | |
Text progress bar in command line. | |
Pre-condition: the cursor is at a new line. | |
Post-condition: the cursor is at the end of the same line. |
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
class Node(object): | |
"""A node in BST.""" | |
def __init__(self, value, left_node=None, right_node=None): | |
self.value = value | |
self.left_node = left_node | |
self.right_node = right_node | |
def set_left(self, left_node): | |
self.left_node = left_node |
OlderNewer