Skip to content

Instantly share code, notes, and snippets.

View tommyli's full-sized avatar

Tommy Li tommyli

  • Melbourne, Australia
View GitHub Profile
@tommyli
tommyli / coin.groovy
Created December 18, 2012 23:17
Coin changer in Groovy
def aud = [200, 100, 50, 20, 10, 5]
def usd = [25, 10, 5, 1]
def hkd = [1000, 500, 200, 100, 50, 20, 10]
def change(denoms, amount) {
if (denoms.empty) return []
def denom = denoms.head()
[denom] * (amount / denom) + change(denoms.tail(), amount % denom)
}
@tommyli
tommyli / percentageBreakdown.hs
Created December 17, 2012 14:09
Haskell version of percentageBreakdown.groovy
percentageBreakdown :: (RealFrac b) => [Integer] -> [b]
percentageBreakdown xs = pb xs 1
pb :: (RealFrac b) => [Integer] -> b -> [b]
pb [] _ = []
pb [0] _ = [0]
pb (x:xs) remaining = [proportion] ++ pb xs (remaining - proportion) where
total = sum (x:xs)
proportion = fracRound (fromInteger x / fromInteger total * remaining) 2
@tommyli
tommyli / percentageBreakdown.groovy
Created December 17, 2012 00:41
Percentage Breakdown problem from @marsbomber
// Fun exercise with @marsbomber, his solution at https://gist.github.com/4291868
List<BigDecimal> percentageBreakdown(List<Integer> input) {
return pb(input, 1.0000)
}
List<BigDecimal> pb(List<Integer> input, BigDecimal remaining) {
if (input.empty) return []
def head = input.head()
if (head == 0) return [0] + pb(input.tail(), remaining)
@tommyli
tommyli / coin.hs
Created December 3, 2012 12:08
The classical coin and denominations problem
import Data.List
denomComparator d1 d2
| d1 > d2 = LT
| d1 < d2 = GT
| d1 == d2 = EQ
denomComparator _ _ = EQ
coin :: [Int] -> Int -> [Int]
coin ds n = coin' ds n []
@tommyli
tommyli / gitconfig.sample.txt
Created March 17, 2012 12:35
Sample user git config file (placed under ~/.gitconfig)
[user]
name = Tommy Yim Li
email = tommy.li@firefire.co
[color]
ui = true
diff = auto
status = auto
branch = auto
[branch]
autosetuprebase = always
@tommyli
tommyli / String Split in Haskell
Created December 8, 2010 07:50
Haskell version of Guy Steele's "String Split" talk at YOW Australia (Melbourne) 2010
-- Tommy Li (tommyli@me.com)
-- 2010-12-08
-- This is a Haskell version of Guy Steele's "String Split" in Fortress
-- His talk was presented at YOW Australia (Melbourne) 2010 conference
-- I saw a tweet someone did it in Python (https://gist.github.com/724905) so I decided to try it in Haskell
data WordState = Chunk String | Segment String [String] String
deriving Show