Ramblings on computers…

Monthly Archives: November 2008

When I went for interview at Imperial College, my interviewer (Dr. P. Kelly) asked me quite a nice question, which (for some unearthly reason) I decided to follow up on afterwards.

The question was a simple one: given a string (e.g.) "examplesgnome" consisting of two substrings (in this case, "examples" and "gnome"), how can we swap the two substrings in-place? In the interview, we covered the third method in more detail, but touched upon the other two. Afterwards, I calculated their computational efficiency, and determined while they all have the same upper bound, one has a lower average running time.

Method one: reversal

This is one Dr. Kelly suggested, which is really rather neat. It consists of two steps: reversing the entire string, then reversing both substrings. For example:

Method two: cycles

This one was also suggested by Dr. Kelly, and involves taking the first character of the larger substring out to a temporary location, then cyclically moving characters

b

places along into the generated space. Once the strings have been swapped (

n

swaps have taken place), we put the stored character back in the final space. For example:

examplesgnome

_xamplesgnome

gxamples_nome

gxa_plesmnome

gxamplesmno_e

gxampl_smnoee

g_amplxsmnoee

gnamplxsm_oee

gnam_lxsmpoee

gnamelxsmpoe_

gnamelx_mpoes

gn_melxampoes

gnomelxamp_es

gnome_xamples

gnomeexamples

Since each character moves to its final position in one move, the algorithm is intuitively of complexity

O(n)

— but we can go further and say it's of absolute complexity

\sim (n + 2)

, taking the steps to remove the first character and fill the last space into account.

Method three: reduction

This is the method I came up with during the interview and, with some help, made work. It basically involves swapping the first smaller substring into its final position, then applying the same algorithm to the resulting subsubstrings of the second substring. For example:

examplesgnome

gnomelesexamp

gnomeampexles

gnomeexpamles

gnomeexmaples

gnomeexamples

This is quite a neat one to implement, and will perform

O(n)

swaps in the worst case (when the smaller substring is only one character long, as one character moves to its final position with each swap).

However, what if we consider the case "foobar", where the substrings are "foo" and "bar"? In this case, method three only takes 3 swaps. Obviously, this method is limited in the upper bound to

O(n)

swaps, but it seems it can do the operation in far fewer — as few as

\Omega (a)

.

I haven't got a concrete proof for this, but I believe this is actually of complexity:

It seems to be release week for me. Here's Almanah Diary0.5.0. Of note with this release is the support for text formatting (bold, italic and underline — nothing particularly fancy), persistent window dimensions, the ability to set the encryption key, and better recovery from database corruption/errors (although it's still not perfect).

Packagers should note that the GTK+ dependency has been bumped from 2.12 to 2.14, and the GConf dependency is no longer optional — though the GtkSpell dependency now is (as spell checking support is now optional).

Automatic link types (yes, this is a stupid name), so that (for example) past Evolution appointments would be shown for each day

Documentation

Export to blog, text file, HTML, etc.

Undo/Redo support

The first three are what I'd like to definitely get done for 0.6.0, but before I can do that I need to sort out the naming for "links" and "automatic links". Obviously these are rubbish names, especially considering one can currently add a "note link". I had considered "attachments", but that's not much better. Does anyone have any ideas?

I went with a friend to see Motörhead in Leicester last night, and it was an awesome gig. Danko Jones and Saxon were supporting, and while I'll give Danko Jones a miss in future, Saxon were great, even if their intro was sickeningly patriotic.

Motörhead themselves were absolutely amazing, playing a variety of stuff: new, old and classic. Mikkey did a fairly hefty drum solo (easily the loudest part of a concert from which my right ear still hasn't stopped ringing), and they managed to fit an acoustic number in, with Lemmy playing the harmonica. I'm told by a friend (whose uncle works for the venue) that Motörhead reached 118dB before the encore, which is not bad at all.

Here's an announcement I've wanted to make for a while: I'm releasing my microcontroller simulator, MCUS. It's a fairly simple simulator designed to aid in teaching the OCR 2008 A-level electronics syllabus to UK students. As far as I know, it's the only simulator available which follows the new syllabus (for which OCR have deigned to write their own simple assembly language, obsoleting any previous simulators used in schools).

It's already had three releases (this is the fourth), but I didn't want to make the 0.1.x releases too public, as they were mainly for testing the program at school. The 0.2.0 release was a brown paper bag release, due to a bad GTK+ version dependency, and some files necessary for the Windows zip package not being included.