Friday, 10 July 2009

(0,-1,-1,0,X,Y)

The title above is actually the matrix of the transformation I was looking for and corresponds the a 90° rotation and a vertical flip... it also corresponds to a 270° rotation and a horizontal flip.

Anyway, I've spent so long trying to figure it out using any number of egg boxes that I managed to lose sight of why I was trying to transform the array... I guess that it's because I hard-coded the elements of the array in its initial iteration and wanted the new non-hard-coded to be in the same format... whereas it doesn't have to be...

Thursday, 9 July 2009

Nearly rotating a 2-dimensional array in Java

I had a 2-dimensional array in a java program and I needed to rotate the structure of that array and place the elements of the array in a specific order, I'll illustrate what I mean with the following illustration:

+----+----+----+
|1,1 |1,2 |1,3 |
|   A|   B|   C|
+----+----+----+
|2,1 |2,2 |2,3 |
|   X|   Y|   Z|
+----+----+----+

So you can see the above as an egg box. I've got six hens you see and one day those six hens lay two eggs each... the poor bloody chickens haven't got nice names but instead have the names "A", "B", "C", "X", "Y" and "Z". So I've got twelve eggs and I've placed one of each of those hens eggs in an egg box like in the illustration above, I place the remaining six eggs in another egg box like in the following illustration:

+----+----+
|1,1 |1,2 |
|   A|   X|
+----+----+
|2,1 |2,2 |
|   B|   Y|
+----+----+
|3,1 |3,2 |
|   C|   Z|
+----+----+

There we have the illustrations and I knew that I'd need to use two loops to populate the new array but how?

I spent about 3 hours thinking about it and playing with possible solutions without really knowing what I was doing... thought it might be something like SVG matrix calculations and started to get even more confused until I sat down with a bit of paper... Imagine, if you will, that you need to describe to someone the placement of the eggs in the two boxes.

Because we're human we'd spend ages saying things like, "Put the first B egg in the top middle hole in the top box and the left-hand middle hole in the bottom box." Should the boxes get bigger... something like:

+----+----+----+----+
|    |    |    |    |
|    |    |    |    |
+----+----+----+----+
|    |    |XXXX|    |
|    |    |XXXX|    |
+----+----+----+----+
|    |    |    |    |
|    |    |    |    |
+----+----+----+----+

THen things get a little more complicated and we might decide to tell the other person what we mean by saying, "Put it in the hole two down and three across".

This is how we navigate a 2 dimensional array in fact; if we had a Java array like in the big egg box above we'd get to the big red "X" egg by saying bigBox[1][2] (don't forget that we're starting counting from 0 rather than 1).

Anyway, something interesting happens if we write down the places of each egg in each box:

    | BOX1 -- BOX2
----+------------- 
A = | 1,1 -or- 1,1
B = | 1,2 -or- 2,1
C = | 1,3 -or- 3,1
X = | 2,1 -or- 1,2
Y = | 2,2 -or- 2,2
Z = | 2,3 -or- 3,2

And what do we notice? Sometimes it takes a pen and paper to get to the right answer... either that or a cycle ride with a nine-year-old who's rather smart!

So we get the following method:

  public static char[][] rotateArray(char[][] incoming){
    int width = incoming.length;
    int height = incoming[0].length;
    char[][] outgoing = new char[height][width];
    for(int x = 0; x < incoming.length; x++){
      for(int y = 0; y < incoming[0].length; y++){
        outgoing[y][x] = incoming[x][y];
      }
    }
    return outgoing;
  }

I did look at these two pages: http://blogs.msdn.com/oldnewthing/archive/2008/09/02/8918130.aspx and http://geekswithblogs.net/cwilliams/archive/2008/06/16/122906.aspx. Of the two the latter is the better I think... at least in terms of me thinking about the problem and prompting me to get my finger out.

Wednesday, 8 July 2009

First crack at 2 word permutation generator

A little while has come and I've started coding something that will work. The result is here and the final result (using command line redirection) is here.

Now to finesse the program so that it'll take command line arguments.

Prior to that though I've altered the workhorse bit where the chars are printed to instead of:

System.out.print(a1[a]);
System.out.print(a2[b]);
System.out.print(a3[c]);
System.out.print(a4[d]);
System.out.print(a5[e]);
System.out.print("\n");

We can have:

String newWord = "";
newWord = newWord+a1[a]+a2[b]+a3[c]+a4[d]+a5[e];

A lot neater and it now allows us to do this:

System.out.println(newWord);
System.out.println(newWord+"abc");
System.out.println(newWord+"123");

Rather than use all the millions of lines in the pdf above.

Java char addition does seem to be a bugger though as this will throw an error.

String newWord = a1[a]+a2[b]+a3[c]+a4[d]+a5[e];

Odd ehh?

Command line redirection

This is going to be of use in a little while when I've finished writing a program to work through all possible combinations of 2 words.

For instance, if I have a java program called from the command line and I want to collate the output of that program then I could do something like this:

C:\java someProg > someResult.txt

Then anything that gets thrown out by someProg will get sent to the text file someResult.

Another thing to take into account is that if two angle brackets (>>) are used then the output is appended to the file if it already exists... then again the single angle bracket (>) will overwrite the file.

Getting back to the program that I need to write it's a little like this http://mytexttools.com/Permutation-Generator.html in that I need to get all possible permutations of 2 different words... in both upper and lower case, the thing that makes it easy is that the letters must be in the correct order... they can however be either upper or lower case. For instance all combinations of the words "a" and "i" are: "a", "A", "i", "I". Whereas all possible combinations of the words "ab" and "cd" are: "ab", "aB", "ad", "aD", "Ab", "AB", "Ad", "AD", "ib", "iB", "id", "iD", "Ib", "IB", "Id", "ID". I guess you can see where this is going.

So there are 4 possible combinations for 2 one letter words, 16 possible combinations for 2 two letter words, 64 possible combinations for 2 three letter words, 256 possible combinations for 2 four letter words, 1024 for 2 five letter words, 4096 for 2 six letter words, 16384 for 2 seven letter words and 65536 for 2 eight letter words!

Think I'll need to use java arrays to output 'em all rather than messing about by hand using pen and paper.

Monday, 6 July 2009

jQuery Zebra Highlighting for Tables

Cheers Tom for the driving lesson.

For my dissertation I used a number of tables to illustrate the modules on offer and some normalization. In order to make 'em a little less boring I used different classes for alterate rows... which was tedious to say the least. Then I came across the Zebra Widget for TableSorter, a rather cool jQuery plugin.

This was easy to include and added the benefit of allowing me to sort the data in the table by using the tablesorter functions... it might have been easier to just have the zebra highlighting but I wasn't sure if I'd ever need the facility of sorting the data... couldn't hurt now could it?

Anyway, to get it working I placed this in the head:

    <script type="text/javascript" src="../js/jquery-1.2.3.min.js">
    </script>
    <script type="text/javascript" src="../js/jquery.tablesorter.min.js">
    </script>
    <script type="text/javascript">
      $(function(){
        $("table").tablesorter({widgets: ['zebra']});
      });
    </script>

Of course, if you've a number of tables on your page you can use the power of jQuery selectors to be a bit more choosey about which one/s you choose to have the script work on... I just had the one table on the page so just used "table".

Along with the script inclusion above you'll also need to alter your CSS to something like:

tr.odd{
 background-color:      #999999;
}

This'll allow the script/widget/plugin to alter the appearance of your rows... of course it means that the default background-color of the rows will have to be something other than gray 60 wink.

Wednesday, 7 January 2009

SDHC formating

I keep killing SDHC cards cards in my Eeepc; I keep killing 'em where I haven't got admin rights to a Windows machine so I need to keep going back to google and searching for how to do it via the command line (re-reading Snow Crash!) on the said Eeepc. I've been doing this so many times that I thought I'd write down the process and save myself some time in the future:

Insert the card (make sure it's not cold or for some reason my Eeepc doesn't recognize that a card has been inserted...


/home/user> sudo mount

This will show where the SDHC card is mounted, usually (all the times I've done this but YMMV) it'll be /dev/sdb1.


/home/user> sudo umount /dev/sdb1
/home/user> sudo fdisk /dev/sdb1

We need to delete anything that is there and then create a new primary partition (keeping hitting "m" for ideas of how to do this) we then need to write this to the disk. Then we need to eject the card and reinsert it and do this:


/home/user> sudo mkfs.vfat -c -F 32 /dev/sdb1

That should be it though it's worth noting that sometimes we'll need to delete any existing partitions before creating new ones. Odd things SDHC cards, just wish I knew how I manage to keep killing 'em.

This story about child elopers nigh on made me melt.

Oops:

The Caffeine Click Test - How Caffeinated Are You?