Skip to content

Instantly share code, notes, and snippets.

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
unsigned int sharedPrefixLength(const char *a, const char *b);
std::string longestRepeatedString(const std::string s);
int main()
{
import qualified Data.DList as DL
import Control.Monad (forever)
base62Encode :: Integer -> String
base62Encode = map ((base62Alphabet!!) . fromIntegral) . DL.toList . digits
where base62Alphabet = ['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z']
digits n | n < 62 = DL.singleton n
| otherwise = digits (n `div` 62) `DL.snoc` (n `mod` 62)
readUuid :: String -> Integer
{-# LANGUAGE LambdaCase #-}
import Graphics.HsExif (parseFileExif, getGpsLatitudeLongitude)
import System.Environment (getArgs)
import System.FilePath (takeFileName)
import System.IO (hPutStrLn, stderr)
import Data.List (intercalate)
printGeoLine :: FilePath -> IO ()
printGeoLine filename = do
@ehamberg
ehamberg / getbg.c
Created January 6, 2011 12:26
Quick hack. Reads the current X background image and writes it to a JPEG file
// compile with: gcc -Wall -ansi -pedantic getbg.c -o getbg -lX11 -ljpeg
// usage: ./getbg mybg.jpg
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
@ehamberg
ehamberg / temp.bash
Created January 11, 2011 15:26
temperature for trondheim
#!/bin/bash
URL=http://www.yr.no/sted/Norge/Sør-Trøndelag/Trondheim/Trondheim/varsel.xml
TEMP=$(wget -q -O - $URL | awk -F\" '/<temperature/ {print $4;exit}')
if [ $TEMP -gt "20" ]; then
echo "<fc=#ee4444>${TEMP}</fc>"
elif [ $TEMP -lt "0" ]; then
echo "<fc=#3bb9ff>${TEMP}</fc>"
else
@ehamberg
ehamberg / geoiptemp.bash
Created January 13, 2011 00:03
Get temperature for current location (hopefully)
#!/bin/bash
GEOIP_API_KEY="" # http://geoio.com/signup.php
WEATHER_API_KEY="" # http://www.worldweatheronline.com/register.aspx
IP=$(curl -s "http://whatismyip.org/")
GEOIP_URL="http://api.geoio.com/q.php?key=$GEOIP_API_KEY&qt=geoip&d=comma&q=$IP"
LAT_LONG=$(curl -s "$GEOIP_URL"|awk -F, '{print $(NF-1)","$NF}')
WEATHER_URL="http://www.worldweatheronline.com/feed/weather.ashx?q=${LAT_LONG}&format=csv&num_of_days=2&key=$WEATHER_API_KEY"
@ehamberg
ehamberg / solitaire_ciper.hs
Created January 19, 2011 10:17
Implementation of the Solitaire Ciper from “Cryptonomicon”
import Data.Char (ord, chr)
import Data.List (elemIndex)
import Data.Maybe (fromJust)
import Control.Arrow
data Card a = Card a | JokerA | JokerB deriving (Show, Eq)
type Deck = [Card Int]
-- we start with a deck in bridge order
startDeck :: Deck
-- lazily produce the binary representation of a number as a list of [0,1]
bin ∷ Integer → [Integer]
bin n = bin' n ((floor ∘ logBase 2 ∘ fromIntegral) n)
where bin' 1 0 = [1]
bin' 0 0 = [0]
bin' a b = if a ≥ 2^b
then 1:bin' (a-2^b) (b-1)
else 0:bin' a (b-1)
popCount :: Integer -> Integer
@ehamberg
ehamberg / forward_backward.hs
Created February 17, 2011 13:19
forward-backward examle from AIMA ch. 15
{-# Language FlexibleContexts #-}
import Data.Packed.Matrix
import Numeric.Container
normalize :: (Container Vector e) => Matrix e -> Matrix e
normalize m = let scaleFactor = sumElements m
in (fromLists . map (map (/scaleFactor)) . toLists) m
forward :: (Product e, Container Vector e) =>
// by alex evans, 2011. released into the public domain.
// based on a first ever reading of the png spec, it occurs to me that a minimal png encoder should be quite simple.
// this is a first stab - may be buggy! the only external dependency is zlib and some basic typedefs (u32, u8)
//
// more context at http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
//
// follow me on twitter @mmalex http://twitter.com/mmalex
//
u32 crc_table[256]={0};
void make_crc_table(void)