coding
Read current X background image to a JPEG file
2Hack of the day:
- Problem: I wanted to be able to fade to a new background image. But how could I get the current background image so that the two images can blended together?
- Solution: I dived into Xlib for the first time since 2003 and came up with a small C program that reads the current background image and writes it to a JPEG file.
I now have a script that downloads a new background image every hour and nicely fades to this new background by getting the current background and gradually blending the new image over this.
[As Johannes points out below, Gnome can already do this. KDE, too, will always fade in new wallpapers whether you change manually or with a wallpaper plug-in.]
Code:
Prüfer Sequence – Compact Tree Representation
0Sometimes you stumble over some cool things you really wish you could use right away – like the Prüfer sequence. The Prüfer sequence is a way of uniquely representing trees of n nodes with a sequence of n-2 node labels. The tree shown below, which has six nodes can thus be represented by a string of four labels, namely “3334“.

The tree represented by "3334".
To come up with the Prüfer sequence for the tree you first have to come up with an order for the node labels. For the tree above this is simply 0, 1, 2, …. You then start with the “smallest” label, remove it and add the node it is connected to to the sequence. Here, that would be to remove node 0 and add 3 to the sequence. The next nodes would be node 1 and 2, which are also connected to node 3, yielding the sequence “333” after three steps. You would then remove node 3 and add 4 to the sequence. When only two nodes are left you stop. The final result is thus “3334“.
To go from a Prüfer sequence to a tree you first find the degree of each node, this is simply done by counting its occurrences in the sequence and add one, meaning that node 0 has degree 0+1=1, while node 3 has degree 3+1=4. You can then do the reverse of the encoding process: for each node in the sequence, add an edge from it to the first node with degree 1 and reduce both nodes’ degree. This is done until you have only two nodes left which should be connected.
Python code for decoding a Prüfer sequence into an array of node pairs (connection):
def prufer_to_tree(a): tree = [] T = range(0, len(a)+2) # the degree of each node is how many times it appears # in the sequence deg = [1]*len(T) for i in a: deg[i] += 1 # for each node label i in a, find the first node j with degree 1 and add # the edge (j, i) to the tree for i in a: for j in T: if deg[j] == 1: tree.append((i,j)) # decrement the degrees of i and j deg[i] -= 1 deg[j] -= 1 break last = [x for x in T if deg[x] == 1] tree.append((last[0],last[1])) return tree
Show current vi mode in your zsh prompt
0Having a brain damaged by prolonged use of vim I even use a vi input mode for my shell. I recently switched from bash to zsh and among other niceties zsh can show which vi mode I’m currently in and update this in real time as I switch between normal and insert mode. I found out how to do this from Aaron Toponce’s blog.
To have a VIMODE variable which is updated as you switch modes, add this to your ~/.zshrc:
# set VIMODE according to the current mode (default “[i]”) VIMODE='[i]' function zle-keymap-select { VIMODE="${${KEYMAP/vicmd/[n]}/(main|viins)/[i]}" zle reset-prompt } zle -N zle-keymap-selec
You can then add $VIMODE to your prompt:
# Set the prompt to “[user]@[host[ [vi mode] $ ” PROMPT="%n@%m ${VIMODE} \$ "
Game of Life
0While reading about cellular automata in preparation for an essay it struck me that I have never actually written Conway’s Game of Life. No, really!
To correct this embarrassing fact I quickly wrote a version in Haskell using the GLUT bindings.
Code Reading as a Team Activity
1I am currently reading “Coders at Work” by Peter Seibel, a really interesting read for a code monkey like me. The book is full of good tips and experiences from actual coders who learnt by doing. Peter Seibel’s questions are really good – no doubt because he himself is a programmer – and all his interview subject have different and interesting stories to tell.
HsUnixCompat.hs on Debian
2Just so that a others (hopefully) will be spared the annoying and time-consuming work of tracking down the source of the following error message:
* Missing header file: HsUnixCompat.h
On my Debian system (Debian 5.0.3 – “Lenny”) the missing package was libbsd-dev:
$ cabal unpack unix-compat
Unpacking unix-compat-0.1.2.1…
$ cd unix-compat-0.1.2.1/
$ runhaskell Setup.lhs configure -v3
[…]
/usr/bin/gcc returned ExitFailure 1 with error message:
In file included from include/HsUnixCompat.h:1,
from /tmp/18515.c:1:
/usr/lib/ghc-6.10.4/unix-2.3.2.0/include/HsUnix.h:79:21: error: libutil.h: No such file or directory
$ apt-file search libutil.h
libbsd-dev: /usr/include/libutil.h

