Skip to content

Instantly share code, notes, and snippets.

qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort small ++ [x] ++ qsort big
where small = [x'| x' <- xs, x' < x]
big = [x'| x' <- xs, x' >= x]
* Levels of tests
o unit test
o integration test
o system test
o acceptance test
* Why test? (acceptance test / system test)
o verify correctness: bugs always exist (but we can keep them small and easier to solve)
o save manually verification time
* Why unit test?
o help find root of bugs (save debugging time)
import Test.QuickCheck
import Data.List
--
-- aux functions for verifications
--
isOrdered :: Ord a => [a] -> Bool
isOrdered [] = True
isOrdered [x] = True
isOrdered (x:x2:xs) = x <= x2 && isOrdered (x2:xs)
--
-- wc.hs: word count
--
-- Given a file name in the first command line argument,
-- print WORD: COUNT for each WORD per line.
-- The output is ordred by COUNT.
--
import System
import Data.List
import System.IO
import Data.Char(toUpper)
main = do
inh <- openFile "input.txt" ReadMode
outh <- openFile "output.txt" WriteMode
inpStr <- hGetContents inh
hPutStr outh (map toUpper inpStr)
hClose inh
hClose outh
import System.IO
import Data.Char(toUpper)
main = do
inh <- openFile "input.txt" ReadMode
outh <- openFile "output.txt" WriteMode
inpStr <- hGetContents inh
hClose inh
hPutStr outh (map toUpper inpStr)
hClose outh
function set_hg_rep() {
hg_rep=$(pwd | awk -F/ '{ print $5 }')
if [ "$hg_rep" != "" ]; then
hg_rep="(\033[1;31m$(pwd | awk -F/ '{ print $5 }')\033[m)"
fi
export PS1="[\u@wiki \w $hg_rep]\n$ "
}
function my_cd() {
\cd $1 && set_hg_rep
#!/usr/bin/python
# -*- encoding: utf8 -*-
class A(object):
def __init__(self, s):
self.s = s
def __str__(self):
return self.s
19 function gp() {
20 if [ "$1" = "-p" ]; then
21 shift
22 \ls *.py */*.py | perl -ne 'print $_ if !($_ =~ /.*_test\.py/)' | xargs grep -i --color $1
23 elif [ "$1" = "-t" ]; then
24 shift
25 \ls *.py */*.py | perl -ne 'print $_ if $_ =~ /.*_test\.py/' | xargs grep -i --color $1
26 else
27 g "$1" *.py */*.py
28 fi
#!/usr/bin/env python
# demonstrate built-in functions' efficiency.
import timeit
import random
# prepare test data
numbers = range(2)
a = []
for n in numbers: