I was hoping these would generate more interest. On the other hand MD is probably not the best forum to try to get up a discussion on programming. Now if that algorithms forum had ever appeared as discussed...

Anyway I will continue for now and post the second part for Programming Diversion 2. If you recall in

Programming Diversion 2a we discussed producing a cipher by applying a key to it. Admittedly that was fairly trivial. It gets a little more complicated (and therefore more fun to program) when we apply a

*hat* to cipher to get a new one. The purpose of the hat is to re-order the cipher to jumble it up some more.

Like a key, a hat is simply a

word. We place our cipher under this word, wrapping to the next row every n characters, where n is the length of the hat. After this we will end up with n columns of letters. We then derive a numeric sequence from the hat by giving each letter in the hat a number which equates to it's rank if the hat were sorted. Duplicate letters are kept, being ranked according to which appears first in the hat.

This is kind of confusing so let me show you what I mean. Let's say our hat is "ranch" and we want to get a sequence from it. If we sorted "ranch" we would get "achnr", so assigning a rank to each letter means a=1, c=2, h=3, n=4, and r=5. Looking back at our unsorted hat and applying these ranks, we see the sequence for the hat "ranch" is 51423. We can see this better if we write the sequence over the hat.

<pre>

51423

ranch

</pre>

If the hat were "java", our sequence would be 3142, with the first "a" that appears being given a rank of 1, and the second "a" that appears being given a rank of 2.

Remember I said we that to apply the hat we need to place our cipher (which we got from applying a key to the plaintext alphabet) uner it. Let's assume for now that our cipher is just the plaintext alphabet "abcdefghijklmnopqrstuvwxyz", and our hat is "ranch", we will now arrange our cipher under our hat like this:

<pre>

51423

ranch

=====

abcde

fghij

klmno

pqrst

uvwxy

z

</pre>

To get our final cipher, we pull each column of letters in rank order and

string them together to form our cipher. So we will first pull the column of letters under the "a" in "ranch", then we will pull the column under the "c", etc... This gives us: bglqv + dinsx + ejoty + chmrw + afkpuz = bglqvdinsxejotychmrwafkpuz.

If we applied a key of "java" to the plaintext alphabet our resulting cipher is "javbcdefghiklmnopqrstuwxyz". If we then apply a hat of "ranch" to that cipher we end up with a new cipher "aekpubgmrxchnsyvflqwjdiotz". It's actually pretty simple once you work out a couple for yourself.

=============

The Challenge

=============

Given a String representing an input alphabet/cipher, apply a hat to it, returning the new cipher.

Here's our shell of a class as we layed out in Diversion 2a, adding an accessor and mutator for the "hat" attribute, and making slight changes to encrypt() and main(). I also included my applyKey() method although if you did 2a you can use your own. You will need to complete the applyHat() method.