Skip to content

Instantly share code, notes, and snippets.

@kflu
kflu / binary_tree_find_path.py
Created August 4, 2012 18:18
Find all paths in a binary tree that the nodes on the path sums up to a specified vale.
"""Find all paths in a binary tree that the nodes on the path sums up to a specified vale.
"""
from collections import deque
def find_path(T,X):
"""
On each node it visits, it marks the node's parent. This is for tracing
upwards.
"""
res = []
@kflu
kflu / checkip.py
Created August 22, 2012 05:53
check my IP address
import logging
import urllib2
import re
from contextlib import closing
def get_ip(url = "http://checkip.dyndns.org/"):
'''Get local machine (or router)'s external IP from a IP checking web
service.
url: the URL of the IP checking service. Default to
Clear-Item -Path alias:dir
function dir
{
Invoke-Expression "cmd /c dir $args"
}
Clear-Item -Path alias:cd
New-Alias -Name cd -Value Push-Location
$env:Path = ".\;$env:Path"
public class WebClientEx : WebClient
{
public WebClientEx(CookieContainer container)
{
this.container = container;
}
private readonly CookieContainer container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
@kflu
kflu / ExpressionTreeLambdaToAssembly.cs
Last active November 28, 2018 14:49
Dump expression tree lambda into assembly for easy IL inspection using ildasm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Linq.Expressions;
using System.Reflection.Emit;
using System.IO;
@kflu
kflu / LinqSlowerThanLoop.cs
Last active August 29, 2015 14:02
This test shows you that Linq to object is significantly slower than traditional for loop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Linq.Expressions;
using System.Reflection.Emit;
using System.IO;
using System.Diagnostics;
@kflu
kflu / spinlock.cs
Created July 9, 2014 21:21
Compares spinlock and regular lock to understand under which circumstances does spinlock provides perf benefit.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication27
{
@kflu
kflu / perf.cs
Created August 30, 2014 17:07
Performance comparison: Node, Python, C#
using System;
using System.Diagnostics;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
int N = 10000000;
@kflu
kflu / p1.js
Created September 5, 2014 19:03
var i = 0;
while (true) {
i += 1;
process.stdout.write("" + i + " ");
}
@kflu
kflu / livestream
Last active August 29, 2015 14:06 — forked from deandob/livestream
// Live video stream management for HTML5 video. Uses FFMPEG to connect to H.264 camera stream,
// Camera stream is remuxed to a MP4 stream for HTML5 video compatibility and segments are recorded for later playback
var liveStream = function (req, resp) { // handle each client request by instantiating a new FFMPEG instance
// For live streaming, create a fragmented MP4 file with empty moov (no seeking possible).
var reqUrl = url.parse(req.url, true)
var cameraName = typeof reqUrl.pathname === "string" ? reqUrl.pathname.substring(1) : undefined;
if (cameraName) {
try {
cameraName = decodeURIComponent(cameraName);