Sunday 26 July 2009

Perfect Water Filtration System for a Live-A-Board

In Thursday's Guardian IT supplement there was a fantastic article about Dean Kamen. I sort of remember a little about his water filtration system, which is apparently now code-named Slingshot, on an online video and I was fascinated by it, not least because the water on the boat was so undrinkable at the time! Try as I might I couldn't find out anything about it but it now seems as though the reason was is that it was a prototype and not available. The idea of having something with about the same mass as the water tank on the boat but that would create unlimited water from a source as oft-times filthy as the Cam was cool! It didn't seem as though it'd take much in the way of electricity either!

Then After looking at his website I came across his Sterling Engine design. All of my power and water requirements met in two handy packages... except... again it's a prototype!

There are plenty of cowpats on Stourbridge Common now that we've a load of bullocks grazing (It's why I have to leave my shoes outside). How cool would it be to get them all drying out in the beautiful Cambridge summer sun and burn 'em to reduce greenhouse gas emissions from the methane... not to mention saving the diesel I use going up to the end of Stourbridge common or Jesus Green every fortnight to fill up with fresh, potable, water. Could even use the Sterling Engine to provide for my limited electrical needs? I guess, what with it providing heat, I could cook with it and get rid of the Calor gas? Though we're doing pretty well on that front as it goes, at least until it goes up in price again.

Tuesday 21 July 2009

Shadowed A

I'm trying to do a drop-shadow in Inkscape, but not any old drop-shadow... it's be easy to use Gimp... or perhaps not for the effect I'm looking for. This tutorial comes close but what I'm after is something like narrow boat sign-writing; An almost 3D effect where each letter is flat face towards us but has been extruded from below - if you see what I mean?

I've bought a lovely typeface called Modesto Expanded that would work a treat but just now I've no joy finding an example of the effect I want except the Grace Darling image on the Vimart Signwriting site.

Sunday 19 July 2009

Install Flash to Portable Firefox without Admin Privileges

I run Portable Firefox from Portable Apps on a fairly regular basis but when I find myself on a machine where I haven't got Admin privileges I need to run another program (in this instance 7-zip), again from Portable Apps.

I download http://fpdownload.macromedia.com/get/flashplayer/xpi/current/flashplayer-win.xpi, open the flashplayer-win.xpi with 7-zip and extract the flashplayer.xpt and NPSWF32.dll files to the \FirefoxPortable\Data\plugins folder. Start up Firefox again and all should be well and you can browse YouTube to your hearts content.

I've learnt this trick from a number of sources and used to use it to get the now defunct Adobe SVG plugin to work nicely with Firefox... I did however refresh my memory using the answer posted on Acid-Labs: Installing Flash in Portable Firefox with no installer... particularly the one by Vishwesh Sharma (No. 12).

This got me thinking about installing the Adobe plugin in Firefox 3.5 and, despite following the instructions from 2006, I couldn't get it to work... such a shame Adobe stopped updating it as it's scripting support was excellent.

Wednesday 15 July 2009

Brute force TrueCrypt when you sort of know the password.

So a friend asked me to generate all possible combinations of two words, he uses two words as passwords and mixes up the capitalisation of those words so that if he has the two words "apple" and "pears" as possible passwords then he could have something like "aPpRe"... there are loads of possibilities but not as many as there could be because each letter position would only have 4 possible letters. For example the third letter can only be "p", "P", "a" or "A". Mathematically we're looking at four possible outcomes for a one letter password, sixteen for a two letter word, 64 for a three letter word... 256... 1024... 4096... 16384... 65536... 262144... 1048576 for a ten letter password.

Doing it by hand is just about possible if you're only looking at a 3 letter combination but otherwise...

Seeing as I'm relearning Java I thought I'd have a crack (after getting lots and lots of help from comp.lang.java.help - I did say relearning!) and after trying to get my head around recursion and realising I'd done something with it years ago using JavaScript to parse an OWL file representing the management structure of an organisation I came up with the following file:

WordComb.java:

public class WordComb {
  public static void main (String[] arguments ){
    if (arguments.length != 2) {
      System.out.println("This program requires two arguments, please try again.");
    }
    else {
      if (arguments[0].length() != arguments[1].length()){
        char spaceChar = ' ';
        int argOne = arguments[0].length();
        int argTwo = arguments[1].length();
        if (argOne < argTwo) {
          for (int a = argOne; argOne < argTwo; argOne++){
            arguments[0] = arguments[0]+spaceChar;
          }
        }
        else {
          for (int a = argTwo; argTwo < argOne; argTwo++){
            arguments[1] = arguments[1]+spaceChar;
          }
        }
      }
      char[][] words  = {arguments[0].toLowerCase().toCharArray(),
                         arguments[0].toUpperCase().toCharArray(),
                         arguments[1].toLowerCase().toCharArray(),
                         arguments[1].toUpperCase().toCharArray()};
      char[] combo = new char[words[0].length];
      fill(combo, words, 0);
    }
  }
  public static void fill(char[] combo, char[][] words, int col) {
    if (col < combo.length) {
      for (char[] row : words) {
        combo[col] = row[col];
        fill(combo, words, col+1);
      }
    }
    else {
      System.out.println(makeString(combo));
    }
  }
  public static String makeString(char... word) {
    return String.valueOf(word).trim();
  }
}

All that needs is a little command-line redirection and you've a nice file to feed into your TrueCrypt cracker of choice...

Monday 13 July 2009

Caravan Boat

A bit of a break here while I cogitate Java arrays.

Being a boater I'm always interested in these types of images:

Caravan Boat image
Caravan Boat image

The first links to the original article, I'm afraid that I've lost the source for the 2nd but I think I saw it on a LJ image trawl.

Saturday 11 July 2009

for looping to infinity

I've an a 2-dimensional array that is best represented in the following tables; it has an arbitrary number of "columns" but only ever 4 "rows".

I need to produce the following "Possible" results programmatically.

+-+     Possible: a, A, x, X 
|a|               (4 * 1 = 4 possibles)
+-+
|A|
+-+
|x|
+-+
|X|
+-+

+-+-+   Possible: ab, aB, aX, aY, Ab, AB, AX, AY, 
|a|b|             xb, xB, xX, xY, Xb, XB, XX, XY
+-+-+             (4 * 4 = 16 possibles)
|A|B|
+-+-+
|x|y|
+-+-+
|X|Y|
+-+-+

+-+-+-+ Possible: abc, abC, abz, abZ, aBc, aBC, aBz, aBZ,
|a|b|c|           ayc, ayC, ayz, ayZ, aYc, aYC, aYz, aYZ,
+-+-+-+           Abc, AbC, Abz, AbZ, ABc, ABC, ABz, ABZ,
|A|B|C|           Ayc, AyC, Ayz, AyZ, AYc, AYC, AYz, AYZ,
+-+-+-+           xbc, xbC, xbz, xbZ, xBc, xBC, xBz, xBZ,
|x|y|z|           xyc, xyC, xyz, xyZ, xYc, xYC, xYz, xYZ,
+-+-+-+           Xbc, XbC, Xbz, XbZ, XBc, XBC, XBz, XBZ,
|X|Y|Z|           Xyc, XyC, Xyz, XyZ, XYc, XYC, XYz, XYZ
+-+-+-+           (16 * 4 = 64 possibles)

I've been trying this now for about 2 days and I think about it and think about it and finally think I've got there but then try it out and it crashes and burns... I've tried rotating the array as well to no joy (perhaps that warrants further investigation?)

Help solicited here.

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.