Skip to content

Instantly share code, notes, and snippets.

View ehamberg's full-sized avatar
🌴
On holiday until December

Erlend Hamberg ehamberg

🌴
On holiday until December
View GitHub Profile
@ehamberg
ehamberg / msp.hs
Created August 10, 2012 09:21
Programming Praxis, 2012-08-10
import Data.List (sort)
minScalarProduct :: [Int] -> [Int] -> Int
minScalarProduct v1 v2 = sum . zipWith (*) (sort v1) $ (reverse . sort) v2
@ehamberg
ehamberg / UDPListener.hs
Created June 28, 2012 18:12
Simple UDP listener
import qualified Data.ByteString.Char8 as B8
import Control.Monad (forever)
import Control.Concurrent (forkIO)
import Control.Concurrent.Chan
import Network.Socket hiding (send, sendTo, recv, recvFrom)
import Network.Socket.ByteString
type PacketProcessor = B8.ByteString -> IO ()
-- producer: a listener listens on a given port number and appends all packets
@ehamberg
ehamberg / count_ones.hs
Created June 15, 2012 09:23
Programming Praxis, 15 June 2012
import qualified Data.MemoCombinators as Memo
f = Memo.integral f'
where f' :: Int -> Int
f' 1 = 1
f' n = (length . filter (== '1') . show) n + f (n-1)
main = (print . head . filter (uncurry (==))) $ zip [2..] $ map f [2..]
@ehamberg
ehamberg / recycled.hs
Created April 25, 2012 10:03
From Code Jam 2012 qualification
import Data.List
import Data.Digits
import Control.Monad
rotations :: Int -> [Int]
rotations n = map (unDigits 10 . (\(a,b) -> b ++ a) . (`splitAt` n')) [1..(l-1)]
where n' = digits 10 n
l = length n'
recycled :: Int -> Int -> Int -> Int
import Data.HashSet hiding (map)
import Control.Monad.State
targets = [231552,234756,596873,648219,726312,981237,988331,1277361,1283379]
hasSum :: Int -> [Int] -> State (HashSet Int) Bool
hasSum _ [] = return False
hasSum t (n:ns) = do
set <- get
if (t-n) `member` set
@ehamberg
ehamberg / gist:1875967
Created February 21, 2012 11:23
C Pointer Declarations

C/C++ Pointer Declaration Syntax – It makes sense!

I never really liked the way pointers are declared in C/C++:

int *a, *b, *c; // a, b and c are pointers to int

The reason is that I am used to reading variable declarations as MyType myVar1, myVar2, myVar3; and I always read “int*” as the type “integer pointer”�. I therefore wanted the following

int* a, b, c; // a is a pointer to int, b and c are ints
@ehamberg
ehamberg / maxsub.hs
Created February 1, 2012 14:57
Find maximum subvector
import Data.List (foldl')
maxseq :: (Num a, Ord a) => [a] -> a
maxseq = fst . foldl' f (0,0)
where f (globMax,currMax) x = let currMax' = max 0 (currMax+x)
in (max globMax currMax',currMax')
@ehamberg
ehamberg / pollard_rho.hs
Created January 8, 2012 20:23
Pollard Rho test
-- the pollard rho algorithm for finding a prime factor of a given integer
pollardRho :: Integer -> (Integer -> Integer) -> Integer -> Integer -> Integer
pollardRho 1 _ _ _ = 1 -- for checking when we're done
pollardRho n f x y =
case gcd n (abs (lx - ly)) of
1 -> pollardRho n f lx ly
d -> d
where lx = f x
ly = (f . f) y
@ehamberg
ehamberg / hac.bash
Created January 6, 2012 14:34
Script for downloading and combining the chapters of “Ηandbook of applied cryptography” to one pdf file
#!/bin/bash
# downloads all chapters of “handbook of applied cryptography”
# (<http://www.cacr.math.uwaterloo.ca/hac/>) and combines them into one pdf file.
# The resulting file can not be distributed and is only for personal use, as
# per the copyright notice at
# <http://www.cacr.math.uwaterloo.ca/hac/about/copyright-notice.html>.
# Requires ghostscript.
TEMPDIR="/tmp"
@ehamberg
ehamberg / sixdice.hs
Created January 3, 2012 09:08
Prob. of getting ≥ 1 six with six rolls of a die
import Control.Monad (replicateM)
import Text.Printf
dice = flip replicateM ([1..6] >>= \a -> return a)
probability n list = a/b
where l' = fromIntegral . length
a = l' (filter (any (==n)) list)
b = l' list