This file contains 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
puts "Solution to 1:" | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each { |num| puts num} | |
puts | |
puts "Solution to 2:" | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each { |num| puts num if num > 5 } | |
puts | |
puts "Solution to 3:" | |
odds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].select {|num| num % 2 == 1 } |
This file contains 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.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Demo of exception handling in ExecutorService. | |
*/ | |
public class ExecutorServiceExceptionDemo { | |
/** |
This file contains 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
#!/usr/bin/env python3 | |
from enum import Enum | |
from random import choice | |
def 做(事情): | |
事情() | |
def 如果(条件成立, 那么做, 动作1, 不然就做, 动作2): | |
if 条件成立: |
This file contains 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 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 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 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 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 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 <kavinyao@gmail.com> | |
# 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 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. |
OlderNewer