english

Solitaire Cipher

0

I finally read Neal Stephenson’s book Cryptonomicon this summer which – besides being a great read – introduced an interesting cipher called Pontifex. This cipher is based on the Solitaire cipher by Bruce Schneier and the idea is that one simply needs a deck of card in order to communicate securely if a list of keys has been exchanged. When seeing that yesterday’s challenge on Programming Praxis was to implement this cipher I simply had to do it, and here’s the resulting implementation, in Haskell, of the Solitaire cipher:

Read current X background image to a JPEG file

2

Hack 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

0

Sometimes 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

0

Having 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} \$ "

Showing grub menu after hibernating in openSUSE

0

When hibernating in openSUSE and starting the computer again the grub boot menu is not shown and you are instead taken directly to openSUSE. This is most often what you want, but if you have a dual boot system like me and want to be able to switch between Linux and Windows without ending the session in either end, this is annoying. Luckily there is a way to show the grub menu after hibernating: If you edit the file /usr/lib/pm-utils/sleep.d/99Zgrub and comment out the last two if blocks, grub will be shown as usual. This solution is taken from http://forums.opensuse.org/english/get-help-here/install-boot-login/435012-suspension-hibernation-grub.html.

A caveat: make absolutely sure that your Linux file systems are not mounted in Windows and vice versa. This can lead to data loss!

How Neurons do Differentiation

0

Nature is simply amazing.

Today I learnt…

…how neural networks (think brains) can do differentiation by using temporal inhibition – i.e. by using a delayed signal. In the figure below, the node α will send a signal to two nodes. One of them – β – will pass on an inhibitory signal of the same strength as its input signal, but with a delay. Thus, when β‘s signal gets sent to the final node, α will at the same time be sending its “next” output signal to the final node.

“Differentiation” by using delayed inhibition. Solid lines indicate excitatory signals and the dotted line an inhibitory signal.

Therefore, the final node will receive two signals: the current output of α and the inverted previous output of α. If the final node sums these together its output will therefore be α‘s current value minus its old value – i.e. positive if α‘s output signal is increasing and negative if it is decreasing. Simple and beautiful!

Game of Life

0

While 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.

Conway's Game of Life

(more…)

Code Reading as a Team Activity

1

I 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.

monkey_tutor

(more…)

Wish: Options for Kate’s Indenting Scripts

1

At the start of Kate’s indenting script for C/C++, the following options are available:

// BEGIN USER CONFIGURATION
var cfgIndentCase = true; // indent ‘case’ and ‘default’ in a switch?
var cfgIndentNamespace = true; // indent after ‘namespace’?
var cfgAutoInsertStar = false; // auto insert ‘*’ in C-comments
var cfgSnapSlash = false; // snap ‘/’ to ‘*/’ in C-comments
var cfgAutoInsertSlashes = false; // auto insert ‘//’ after C++-comments
// END USER CONFIGURATION

To set these options one would first have to actually find the indenting scripts under /usr/share, then copy it to one’s home directory to be able to modify it, and then modify the javascript source. This could – of course – be done in a much better way. In an ideal world options like these should be available in Kate’s settings as check boxes. It should also be possible to have non-boolean options, like choosing a value from a list of possible values for a setting.

I would love to see something like the following:

registerSetting(“Indent ‘case’ and ‘default’ in switch statements”, “cfgIndentCase”,  “boolean”);

produce

☑ Indent ‘case’ and ‘default’ in switch statements

in Kate’s settings.

I don’t have the time myself, but I don’t think it would be very hard, and I’m sure that many people would be a bit happier. Me and the other Kate developers would glady be of assistance. ☺

Kate’s Vi Input Mode — What will KDE 4.4 bring?

24

Please see http://kate-editor.org/kate-vi-mode/ for an updated overview of the Kate VI mode project.

Dear “Katevim” users.  Kate’s Vi Mode is steadily improving and I want to take a moment to tell what’s on the horizon for KDE 4.4. There has been quite a few bugs fixed since 4.3, but some major new features have also been introduced:

(more…)

Go to Top