Archive for the ‘computing’ Category

Java Sudoku Solver in 6 lines

Monday, July 24th, 2006

After playing around with writing Sudoku Solvers in the past, I was interested to see this page which details a number of tiny sudoku solvers in a variety of languages such as Perl, Ruby, etc.

They have a few basic rules – no line over 80 bytes, etc, etc. And the examples take their input as a string of 81 digits, which represents the 9 rows of 9 digits, with “0″ being a blank square.

As I apparently have way too much time on my hands ;) , I got up the motivation to convert one of these examples into Java. The results?

  • The end program is around twice as many lines as the Perl original, and
  • Yup – you can make Java programs as unreadable as anything else out there! ;)

There may well be shorter possibilities out there – I didn’t put in a great deal of effort past converting the Perl original. But, for the record:

import java.util.*;public class S{static char[] A;static void R(){int i,j;for(i=
0;i<81;i++){if(A[i]!='0')continue;HashMap h=new HashMap();for(j=0;j<81;j++){h.
put(j/9==i/9||j%9==i%9||(j/27==i/27)&&((j%9/3)==(i%9/3))?""+A[j]:"0","1");}for(j
=1;j<=9;j++){if(h.get(""+j)==null){A[i]=(char)('0'+j);R();}}A[i]='0';return;}for
(i=0;i<81;i++){System.out.print(A[i]);}System.out.println();System.exit(0);}
public static void main(String[] a){A=a[0].toCharArray();R();}}

Assuming you have an appropriate classpath, you can run it with a line like:

java S 006070800000000000078601520030405010400000002090302060052103690000000000003020100

This code was converted from the excellent Perl example at:

http://www.ecclestoad.co.uk/blog/2005/06/02/sudoku_solver_in_three_lines_explained.html

Nice article on game console power consumption

Monday, June 19th, 2006

Following on from my posting on my new centameter power usage monitor, I came across this interesting article at www.dxgaming.com.

They did some testing of the power consumption of common consoles and made the discovery that the latest consoles use between 5 and 10 times more power in standby than their predecessors – so, an XBox uses 0.4W in standby, but the new XBox360 uses 2W. The Playstation and Playstation 2 is a near identical story. They have an interesting discussion about why these figures may make you switch off your Playstation 2 when not in use, but not your XBox360 – worth the read.

I’ve joined the Google Analytics fan club

Wednesday, April 5th, 2006

After having my name on the waiting list for Google Analytics for a good 3 or 4 months, I got an email this morning inviting me to sign up. And 15 minutes later I was up and running!

Plenty has been written about Google Analytics, much of it in a breathless “Wow!” sort of tone. And, I’m afraid I’m yet another one! The thing that gets me is the sheer depth of information. I’ve just spent the last 15 minutes discovering that (in the first few hours of stats collection) that:

  • Most of my audience has a screen resolution of 1280×1024
  • About a third of my audience is using dialup
  • All of my audience has Java enabled (well, this is a rather small sample at this stage ;) )

In addition, of course, there’s the more “standard” stuff – where in the world your clicks are coming from (although this is down to the State level, which is rare for us Aussies – plenty of tools give you US state info, but not Australian), which pages are most popular, etc.

Up till this point, I’ve been using AWStats in a static configuration – stats are generated in the form of static pages once per day. A great tool that gives me a lot of good summary information, but it just can’t touch the depth of Google Analytics stuff. That said, I’ll still be keeping it. AWStats runs on my PC, so I can always access it, even if I lose Net access. And, being static pages, its blindingly fast.

(Google Analytics is no slouch, by-the-by. I think we’ve all come to expect good performance from Google, be it search, mail, whatever, so this is probably no surprise. But there are noticeable pauses from time to time – only 2-3 seconds or so – as you navigate around)

Anyway, it looks like I can say goodbye to another 30 minutes of my life each day. So many graphs, so little time! :)

Javadoc-driven development

Friday, March 17th, 2006

Its the new development methodology thats taking the developer-world by storm…. ;)

Well, not really.

Just a cute phrase that occurred to me today when chatting to a colleague. We’d just spent 15 minutes discussing the desired behaviour of a system I was working on. Usual stuff – at first blush it seems straightforward, but plenty of edge cases, historical behaviour requirements, and other nastiness.

So we got it figured out, and its 11:45. Lunch booked at 12:00. No way am I even getting 10% of the code done in that time. But its just enough time to do the Javadoc.

So, 15 minutes later, my code has the 2 method signatures I need and around 40 lines of Javadoc describing the code’s behaviour, exceptions, and assertions. And after lunch, I was able to get straight into coding, all details of my discussion safely preserved.

And as an added bonus, when it comes time to rework it, I (or the next guy) will have some great doco to get started.

Not rocket science. Certainly not worthy of its own buzzword-esque title (although it does roll off the tongue :) ). And probably something everybody should be doing anyway as part of their actual methodology. But its a neat little anecdote worth telling!

How did I miss this? java.util.Collections

Monday, March 13th, 2006

Well, they say you learn something new everyday. And given the size of the Java API, thats not too hard to believe ;)

After doing serious Java programming for at a least a few years, and having dabbled for a few years more (all the way back to the 1.0 days!), I’m pretty happy with my breadth of Java knowledge. Until a co-worker today mentioned using Collections.shuffle() to randomize a list.

“Pffffft”, says I, “Surely you jest”, says I. “No such thing.”

Well, there is. java.util.Collections (not Collection, note – theres an “s” on the end!). A nice, chunky class, full of juicy, static methods just waiting to save you from having to re-invent the wheel when it comes to operating on Collections. There’s the obvious stuff – shuffle() and sort() to randomize and sort, for instance. And small, but useful stuff – min() and max(), swap(). And some real useful stuff, like synchronizedList() (which returns a thread safe version of your list).

Many’s the time I’ve done my own “max()” routine. Each time, I trawled through the Collection interface looking for one and was mildly surprised it didn’t exist. And I never thought to look any further afield. I’m guessing that there may be one or two Java guys out there who are in the same boat (How many library classes are there now in Java?). Seeing as my co-worker doesn’t have his own blog…yet (come on H! Get your act together! ;) ), I’m doing this posting for the other people like me!

Of course, I’ve always got the excuse for my ignorance that its a relatively recent addition. I mean, its only been around since Java 1.2….. *grin*

Will Wright’s “Spore”

Thursday, March 2nd, 2006

Came across this link on digg for a 30 minute video demonstration of the upcoming game “Spore”.

Wow!

I gather the game is the brainchild of Will Wright (of “The Sims” fame). The demo blew me away – it is sooooo open-ended. I won’t try to do it justice here – for one thing the scope seems huge, and for another, this was just a demo, so the finished product could be wildly different. In short though, you guide a species’ evolution to a certain stage, then switch to a kind of SimCity/RTS type game to build up their civilisation, then switch to a kind of “God” mode where you can fly to other planets and do it all again.

Reading over that last paragraph, it doesn’t really do justice to what the demo showed. For instance, you seem to have complete flexibility in designing your creatures – the computer figures out how they move/eat/etc based on their body architecture. And the creations of different players are made available (asynchronously) to your own universe – while exploring the universe, you might encounter a planet that has been copied from some other player.

I really recommend setting aside the time to watch the video. Its been over 2 years since I bought a computer game, but I can’t wait for this thing to be released.!

(btw – there’s a good article on the Wikipedia on “Spore” which goes into a little more detail, for those that are interested)

Being too secure with a CGI script

Monday, February 27th, 2006

Oooops. I mean, it seemed like such a good idea, right?

While working on the new Asman IT Consulting website a couple of months ago, I wrote a new “Contact Us” CGI script. I often work in the IT Security space, so I always have the security angle high on my list of priorities. So, I did the usual things:

  • I made sure that it wasn’t too flexible. Case in point – While chasing down a spam problem for a client, I discovered they had used a well known “Contact Us” script that was widely available that let you set the “to” address, such as the website administrator, as a hidden field. It worked well, until the spammers realised they could override that in their POSTs and use your Contact Us page as a spam relay…!
  • I sanity checked all fields to make sure only legal characters were present. Not, by the way, the common mistake of checking for illegal characters. Seems so tempting, as there are far fewer illegal characters to check for….. right up until you discover that you overlooked a dangerous character!
  • I checked that the script was only accessed from my “search” web page. Anything else would suggest some automated tool randomly accessing it directly
  • And a host of other checks and practices for things like rate of sending, data size, logging improper use, etc, etc

So far, so good. A few tests showed things working fine. And 2 months later the number of malicious hacking attempts made through it numbered exactly 0.

Exactly as many legitimate “contact us” messages I’ve received, as it happens. ;)

A look through the logs showed the problem. And it was all because of that second-last point.

Turns out I’ve had a few people try to send me messages this month. But a number of the more tech-savvy users (and I’m flattered they’re among those accessing my site!) turn off the “Referrer” facility in their browsers. The “Referrer” field normally allows a site to see where visitors have come from. With this disabled, my “Contact Us” facility refused to process their message as it looked like they had bypassed the appropriate web page within the Asman IT Consulting site.

All fixed now, and my apologies to those who were rejected up till now! But an interesting example of the “Security” vs “Ease of Use” balancing act that comes up time and time again. There was nothing intrinsically wrong with my initial approach. And there is nothing intrinsically wrong with people choosing to configure their browsers in that fashion. Everyone needs to pick a security stance that suits their particular situation.

And, based on their experiences and changing needs, amend that stance as required.

Java coders have it easy – crashing constructors

Friday, February 10th, 2006

I came across this entry while scanning Java Blogs yesterday about how to handle exceptions within a constructor. The essence of the article is how do you handle the situation where you couldn’t instantiate your object?

So, if the user passes invalid parameters, do you throw an exception? And, slightly more subtly, if your constructor encounters an exception during object setup, how does it handle this?

Interesting article and well worth the read.

But it struck a bit of a chord with me – I recently bought a Nokia N70 phone, and have been cruising the net looking at how you can code for these things. The N70 is a Symbian phone, and while it has J2ME support, the hardcore stuff seems to be done in C++. Now, C++ is an … interesting language, but is not really known for its in-built protection against memory leaks, and when you’re running a program on a device with limited memory, where your app may well run for the order of days or months this suddenly becomes a big deal.

OK, so you just code, really, really carefully. ;)

Well, turns out that constructors are a special case in terms of memory leaks. Symbian doesn’t use standard C++ exceptions, but it does have an analagous concept called “leaving” grafted on. And not being a C++ guru, I couldn’t tell you what difference there is between normal C++ exceptions and the Symbian variety. In any case, the issue when writing Symbian programs appears to be this:

  1. Constructor called
  2. Constructor initialises an instance variable that points to some new object
  3. On the next line, the constructor encounters an error and leaves

So, we now have allocated memory for some new object (step 2 above), but the object that owns it no longer exists – it crashed during construction. And because the object never fully formed, the destructor wasn’t run on it and so couldn’t free up the memory. Ladies and gentlemen, we have memory leak…. :(

Turns out that Symbian programs have an accepted idiom on how to handle this stuff. Objects that can suffer this way have a static construction method (like the whole getInstance() idiom you see in Java). This static constructor creates the object using a constructor that only performs trivial operations that won’t crash, then once the object exists, calls an instance method on the new object that performs 2nd-phase construction. If things turn to jelly, there is at least a concrete object in existence, so the destructor will be called as normal.

Pretty neat. And pretty eye-opening as to the kind of issues you can be exposed to without garbage collection.

(Which is not to say that Java doesn’t have its own issues with memory leaks, but thats a whole other blog post… :) )

Great site – lifehacker.com

Friday, February 3rd, 2006

www.lifehacker.com. Bookmark it. Add it to your aggregator. Print out the URL, frame it, and put in on your bedside table (marginally behind the picture of your “Significant Other” – you don’t want to go too far….)

You get the idea… ;)

I can’t even recall how I found this site now – some random google or another, I think. But I’ve subscribed to the feed for the last few weeks and I’m hooked. What is it? Well, I think they describe it best:

Computers make us more productive. Yeah, right. Lifehacker recommends the downloads, web sites and shortcuts that actually save time. Don’t live to geek; geek to live.

In the last couple of weeks, they’ve posted short reviews and links to cute Firefox plugins and Mac OS X apps, links to stories on hybrid cars, and a number of really interesting links to stories on personal organisation and time management. So far, I’ve been seeing around 10 – 20 posts a day, of which I find at least 75% are worth the read.

Kind of ironic – despite their mission statement, I’ve just lost 5-10 minutes of my life each day to their website! Here’s hoping thats offset by the efficiencies I’ll no doubt gain….. :)

End of rave. We now return you to your regularly scheduled programming…

Yet another Eclipse / NetBeans comparison

Sunday, January 29th, 2006

A week or so ago, I downloaded NetBeans 5 RC2 specifically to play with the mobility pack, but figured I might as well explore its capabilities as a J2SE IDE.

For those of you who like their blog posts short and snappy – “Neat, some cool stuff in there, but I’ll be sticking with Eclipse for now”. For those who can handle a couple more paragraphs, read on…. :)

To start with, can I join the crowd and say “Matisse rocks!”? Matisse is the visual editor for Swing apps, and it is fantastic – in 90% of cases it does what you mean: you put an object right on the left border, and it figures that that object should be anchored to the left. You place one button next to another and it keeps them together and snaps them to be level. I played with Eclipse’s Visual Editor in a recent small project, and it has a ways to go to catch up to this.

But (and there’s always a “but”, right?), the killer for me was that “90% of cases” phrase – its the other 10% that comes back to bite you. For instance, I wanted to do a a little “cross” of buttons – and up, down, left and right, laid out in a diamond. After having been spoilt with the editor doing what I meant for the rest of the UI, I found it a bit jarring when I was doing “free form” component layout – buttons were slightly off, by a pixel or two and it kind of “looked” wrong.

There’s probably a hidden “layout in a cross” option I missed :) . And I could have added a panel, set it to GridBagLayout and done it that way (Matisse has good support for GridBag). And how often do you want to have buttons in a bizarre shape like that anyway? But it kind of broke the spell for me in a way. I’m a big believer of being “in the zone”, development-wise, and switching between IDE modes (code and visual) is a real context switch for your brain – to compensate, I think I need almost perfection from the Visual Editor. Matisse is soooooo close, but isn’t quite there for the fiddly cases. (How would I have liked Matisse to handle my peculiar layout requirement? I have no idea!).

Matisse is the big ticket item – the rest of the IDE is good, but not compelling from where I sit. There are some winners, though. I love the idea that the IDE is ant based – take your development directory, zip it up and give it to someone else, and they can compile, test, build, etc. Ant seems to be the one big constant across teams – it makes so much sense to have it as the backbone of your development processes. And the fact that a test folder is created when you start a new project is a little nicety, but a good one.

On the downside, on my machine (2 Ghz with a ridiculous 512Mb of RAM), NetBeans is noticeably more sluggish than Eclipse – maybe this is an SWT thing? (I hope not, I’m one of those Swing fans ;) ). And the plugin environment for Eclipse seems so much richer at the moment – I even have a VI plugin for my Eclipse for when I’ve been doing a lot of Unix work and keep on putting the letter “i” throughout my source code (you Unix guys will know what I mean)!

So, I’m staying with my Eclipse for now. It was certainly worthwhile to check out though, and I’m still planning to do my next little J2ME project with NetBeans – I’m keen to give that Mobility Pack a proper workout.