Skip to content

Instantly share code, notes, and snippets.

@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)
@ehamberg
ehamberg / photo_blog.perl
Created April 15, 2011 11:14
Old perl script for dumping JPEG images from an mbox
#!/usr/bin/perl
use MIME::Base64;
use Encode 'from_to';
open(MBOX, $ARGV[0]);
@mailbox = <MBOX>;
$b = join('', @mailbox);
@a = split(/\nFrom /, $b);
\usepackage{varioref}
\usepackage{prettyref}
\usepackage{prettyref}
\def\reftextcurrent {}
\newrefformat{fig}{Fig.~\ref{#1}\vpageref{#1}}
\newrefformat{par}{Section~\ref{#1}\vpageref{#1}}
\newrefformat{sec}{Section~\ref{#1}\vpageref{#1}}
\newrefformat{sub}{Section~\ref{#1}\vpageref{#1}}
\newrefformat{table}{Table~\ref{#1}\vpageref{#1}}
import qualified Data.ByteString.Char8 as B
import qualified Data.MemoCombinators as Memo
findRPI :: Int -> [B.ByteString] -> String
findRPI games matches = unlines $ map (\t -> show $ rpi t matches) [0..(games-1)]
rpi :: Int -> [B.ByteString] -> Double
rpi team matches = 0.25 * wp' + 0.50 * owp' team matches + 0.25 * oowp' team matches
where wp' = wp (matches !! team) Nothing
owp' = Memo.integral owp