Skip to content

Instantly share code, notes, and snippets.

@erantapaa
erantapaa / exact-cover.md
Last active October 6, 2015 16:50
using set-exact

This is a quick tutorial on how to use the set-cover package to solve exact cover problems.

Exact Cover Problems

Given a set X and collection of subsets of X, S, the exact cover problem asks if there is a sub-collection S' of S s.t. the subsets in S' are pairwise disjoint and their union equals X.

@erantapaa
erantapaa / matching.hs
Created October 2, 2015 06:59
Gale-Shapely in Haskell
{-# LANGUAGE FlexibleContexts #-}
--- Some experiments vis-a-vis the Gale-Shapely algorithm.
import Data.List
import Control.Monad
import Data.Array
import Data.Array.MArray
import Data.Array.ST
import Data.Ord
@erantapaa
erantapaa / bibparse.py
Last active October 14, 2021 20:44
BibTeX file parsing Python
#
# Simple BibTeX file parsing in python.
#
# See `bibtest1` for an example of usage.
#
# This is a good overview of how to correctly parse a bibtex file:
#
# http://maverick.inria.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html
import string
@erantapaa
erantapaa / brotli.hs
Last active September 30, 2015 05:32
part of the brutal compression algorithm in Haskell
module Lib
where
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.ST
import qualified Data.Vector.Unboxed.Mutable as UVM
import qualified Data.Vector.Unboxed as UV
calculate_codes :: PrimMonad m => UV.Vector Int -> UV.Vector Int -> m (UV.Vector Int)
@erantapaa
erantapaa / l-systems.lhs
Last active September 30, 2015 01:02
L-systems in Haskell
L-systems in Haskell.
===
> import Data.List.Split (splitOn)
> import Data.List
>
> type Rules = [(Char, String)]
>
> -- compute the next generation
> next :: Rules -> String -> String
@erantapaa
erantapaa / modular.hs
Created September 28, 2015 17:06
modular arithmetic example using Data.Reflection
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Reflection
import Data.Proxy
data M a s = M a -- Note the phantom comes *after* the concrete
-- In `normalize` we're tying the knot to get the phantom types to align
-- note that reflect :: Reifies s a => forall proxy. proxy s -> a
@erantapaa
erantapaa / enigma.lhs
Last active May 10, 2021 15:04
enigma in Haskell
Emulating an Enigma machine
===
In this gist I'll go over the development of an Enigma machine encoder
in Haskell.
Here are some useful background info about how the Enigma machine works:
- Enigma Simulator: http://enigmaco.de/enigma/enigma.html
- "How Enigma Machines Work" http://enigma.louisedade.co.uk/howitworks.html
<html>
<body>
<script>
function toChar(ch) {
return (ch == '' ? ' ' : ch)
}
function next(grid) {
@erantapaa
erantapaa / python-2.7-unicode.md
Last active September 23, 2015 16:20
python 2.7 unicode

My quick summary of Python 2.7 strings and Unicode.

Overview

Python has two string types:

  • type str
  • type unicode

Examples:

@erantapaa
erantapaa / EightSisters.java
Created September 18, 2015 06:21
EightSisters marriage assignment problem
// Solution for [2015-09-11] Challenge #231 [Hard] Eight Husbands for Eight Sisters
//
// https://www.reddit.com/r/dailyprogrammer/comments/3kj1v9/20150911_challenge_231_hard_eight_husbands_for/
//
// NOTE: This is ugly Java - lots of global variables, etc.
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;