Skip to content

Instantly share code, notes, and snippets.

@Ming-Tang
Ming-Tang / wc.pl
Last active September 24, 2015 13:27
Simplest word count implementation
#!/usr/bin/perl
while (<>) { $hash{$_} ++ foreach split /\s+/, $_; }
print "$_: $hash{$_}\n" foreach sort { $hash{$a} <=> $hash{$b} } keys %hash;
@Ming-Tang
Ming-Tang / markov.fs
Created December 20, 2010 19:45
Generates quasi-nonsense words by learning from a sample file.
(*
Compilation:
$ fsc markov.fs
Usage:
$ [mono] markov.exe
Load a file and output in pauses
$ (echo "init <FILE>.txt"; for i in `seq 1 100000`; do echo $i; sleep 0.2; done; echo exit) | [mono] markov.exe
*)
open System
@Ming-Tang
Ming-Tang / minesweeper.hs
Last active January 31, 2022 13:40
Basic minesweeper implementation in Haskell
module Minesweeper where
import Data.Set (Set)
import qualified Data.Set as Set
import List(transpose)
import Random
data Flag = Flag | Close | Open
type Number = Int
@Ming-Tang
Ming-Tang / MainPage.html
Created November 28, 2010 20:24
node.js wiki
<p>Hello, world!</p>
@Ming-Tang
Ming-Tang / sqrt_newton.py
Created November 24, 2010 04:17
Square root using Newton's Method
#!/usr/bin/python
def sqrt_newton(x):
if abs(x) > 1e16: raise ValueError("Too large to be calculated accurately.")
f = lambda a: a ** 2 - x
f1 = lambda a: 2.0 * a
x1 = x / 2.0
dx = 1e-16 * 10.0 ** (len(repr(x)) * 1.0)
while abs(f(x1)) > dx:
x1 = x1 - (f(x1) / f1(x1))
@Ming-Tang
Ming-Tang / toposort.fs
Last active September 23, 2015 16:48
Topological sort in F#
module DirectedGraphs
open System
open System.IO
open System.Text.RegularExpressions
type Conn<'a when 'a : comparison> = 'a * 'a
type Graph<'a when 'a : comparison> = 'a Set * 'a Conn Set
let eq = (=)
#if INTERACTIVE
#r "OpenTK.dll"
#endif
open System
open System.Drawing
open System.Collections.Generic
open OpenTK
open OpenTK.Graphics
@Ming-Tang
Ming-Tang / unit.py
Created May 30, 2010 17:57
Dimensional analysis
#!/usr/bin/python
class Unit(object):
def __init__(self, value=0.0, unit={}):
self.value = value
self.unit = unit
@staticmethod
def check(unit1, unit2):
p1 = [ (name, unit1[name]) for name in sorted(unit1.keys()) ]
import java.io.*;import javax.tools.*;class Quine1{static final char Q=34;static final int N=1;public static void main(String[]a)throws Exception{System.out.println("This is the program "+N);String fn="Quine"+(N+1)+".java";BufferedWriter out=new BufferedWriter(new FileWriter(fn));String s="import java.io.*;import javax.tools.*;class Quine%d{static final char Q=34;static final int N=%d;public static void main(String[]a)throws Exception{System.out.println(%cThis is the program %c+N);String fn=%cQuine%c+(N+1)+%c.java%c;BufferedWriter out=new BufferedWriter(new FileWriter(fn));String s=%c%s%c;out.write(String.format(s,N+1,N+1,Q,Q,Q,Q,Q,Q,Q,s,Q,Q,Q,Q,Q));out.close();JavaCompiler c=ToolProvider.getSystemJavaCompiler();c.run(null,null,null,fn);Class.forName(%cQuine%c+(N+1)).getMethod(%cmain%c,a.getClass()).invoke(null,new Object[]{new String[]{}});}}";out.write(String.format(s,N+1,N+1,Q,Q,Q,Q,Q,Q,Q,s,Q,Q,Q,Q,Q));out.close();JavaCompiler c=ToolProvider.getSystemJavaCompiler();c.run(null,null,null,fn);Class.forName("Q
#!/usr/bin/perl
no warnings;
%abcdef=('g','rr','&&','x','e','g','E','E','$','li','::','n','ll',12,
'()','to','Z',chr(1<<5),'w','a','z','
',':::','at ','@','s','x','y',
'tyt', eval eval '\'"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
. '\\\\\\\\\\\\\\\\\\\\\\\\'.'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
.'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
.'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`"\'','y','z','&','t','[]','u','ax','ax','**',
'e','o','o','wt','r ','lx','il','b','c','c','d','ff','f',',',',','.','.','Ion',