Monday, 30 June 2008

Coding Challenge - II

Following Cedric's post that generated much interest. I guess most of us are bored (or unemployed), my "final solution", using the theory of permutations: What Cedric asked is effectively the number of permutations of K objects from a set of N objects, usually written as nPk = n!/(n-k)!
My code:

package beust.codingchallenge;

/**
* Challenge: http://beust.com/weblog/archives/000491.html
*
* Theory: It is the number of permutations of K objects from a set of N objects.
* Usually written as nPk = n!/(n-k)!
*
* @author Thierry Janaudy - thierry@janaudy.com
*/
public final class Challenge {
/**
* In the formula above, represents the set of n objects: 0, 1, 2, ..., 9
*/
private static final int n = 10;

/**
* Number
*/
private int count = 0;

/**
* Challenge
*/
public Challenge() {
final long t0 = System.currentTimeMillis();
for(int k=1; k <= 4; k++) { // k represents the max (10 = 1, 100 = 2, ...)
// System.out.println("-----------------");
solve(k);
}
final long tf = System.currentTimeMillis() - t0;
System.out.println("Total number of numbers: " + count);
System.out.println("Done in " + tf + " ms");
}

/**
* @param select
*/
private void solve(int select) {
final int[] value = new int[select];
final int[] array = new int[select];

for(int i=0; i < select; i++)
array[i] = i + 1;

while(true) {
for(int n=0; n < select; n++)
value[n] = array[n];

output(value, select);
while(nextPermutation(value, select)) output(value, select);

int i = select - 1;
while (i >= 0 && array[i] == (n - select + i + 1))
--i;

if (i < 0) break;
++array[i];

for (int j = i + 1; j < select; j++)
array[j] = array[i] + j - i;
} // while
}

/**
* @param k
* @return
*/
private boolean nextPermutation(int[] val, int k) {
int i = k - 1;
if (i <= 0)
return false;
while(i > 0 && val[i-1] >= val[i])
i--;

if (i < 1)
return false;

int j = k;
while(val[j-1] <= val[i-1])
j--;

swap(val, i-1, j-1);

i++;
j = k;
while(i < j) {
swap(val, i-1, j-1);
i++;
j--;
}

return true;
}

/**
* @param val
* @param a
* @param b
*/
private void swap(int[] val, int a, int b) {
int tmp = val[a];
val[a] = val[b];
val[b] = tmp;
}

/**
* @param val
* @param k
*/
private void output(int[] val, int k) {
if (val[0] == 10) return;

for(int i=0; i < k; i++) {
// System.out.print(" "+val[i]);
}
// System.out.println();
count++;
}

/**
* @param args
*/
public static void main(String[] args) {
new Challenge();
}
}



For N = 10,000, 5,274 is found in 2 ms
For N = 10,000,000,000, 8,877,690 is found in 700 ms or so.

Saturday, 28 June 2008

E-Trading User Interface - II

Instrumentarix

Coding Challenge

Cedric posted this coding challenge, here is a solution, but not a clever one:

package beust.codingchallenge;

import java.util.HashSet;
import java.util.Set;

public class CC {
static final int MAX = 10 * 1000;
static Set set = new HashSet();
static int nbn = 0;
static int jump = 1, biggestJump = 0;

/**
* @param args
*/
public static void main(String[] args) {
long t0 = System.currentTimeMillis();
test();
long tf = System.currentTimeMillis() - t0;
System.out.println(tf + " ms");
}

public static void test() {
for(int i=1; i < MAX; i++) {
final String str = "" + i;
final int strLength = str.length();
set.clear();
for(int j=0; j < strLength; j++) {
set.add(str.charAt(j));
}
if (strLength == set.size()) {
System.out.println("> " + str);
nbn++;
if (jump > biggestJump) {
biggestJump = jump;
}
jump = 1;
} else {
jump++;
System.out.println("< " + str + ", jump = "+ jump);
}
} // for
System.out.println("nbn="+nbn);
System.out.println("jump="+jump);
}
}



It runs in 20 ms, the biggest jump is 124 and the count is 5274

Tuesday, 24 June 2008

E-Trading User Interface

Hello,

I have been working on my spare time (now full-time) on an Electronic Trading User Interface entirely developed in Java Swing:




I will make a web-started version available soon.

The UI has the notion of grids (via xml config), plugins (Java interface), etc. It is quite performant (low cpu and memory usage).
If you are interested in this UI, please get in touch.

Monday, 23 June 2008

10 Java Docking Frameworks

Java Docking Frameworks

Sustainable Energy - Without the Hot Air

A must to read by David J.C. MacKay

Longest Drive So Far

I am really happy with my new driver: a Cobra Speed LD F.
I can really hit the ball far and straight with this one.
I drove the ball 300 yards, damn straight, in the middle of the fairway last week: I was amazed!

Wednesday, 18 June 2008

Will code for food

My contract has come to an end. I have become an offshore by-product.
Please let me know if you have a position in London:
Telco, Finance and Java if possible. Server-side or front-end, it does not matter.
Look for the name 'Janaudy' on LinkedIn for my profile.

Monday, 16 June 2008

Ideas Worth Spreading

Craig Venter: On the verge of creating synthetic life

Swing Table with frozen columns part III

Jeanette from SwingX pointed out that I forgot to keep the selection in sync.
Follows a snapshot of my implementation, both tables are kept in sync whether you click on the right table or the left(frozen) table.
Not sure how robust this is, but I relied on the Focus gained or lost events.

Saturday, 14 June 2008

Thursday, 12 June 2008

Swing Table Flashing Cell

Another feature of an Electronic Trading User Interface is the notion of flashing cells on data update. The way I have decided to implement this feature is by using a custom renderer (TableCellRenderer) and what I call a DelayedCellOff class. Each cell in the grid is implemented as a Cell object. This Cell object has a flag that says if the cell should be flashed or not (if the cell value is for example different than the previous one).
When a cell is updated, fireTableCellUpdated is called on the model (DefaultTableModel), and if its flash status is true, a DelayedCellOff object referencing the cell object is added to a DelayQueue with a timeout.
Initially the cell is rendered with its flash foreground and background colours, and when the timeout expires, the cell flash state is reset; another fireTableCellUpdated is called, the renderer redraws the cell with its default background and foreground colours.

This implementation has two good qualities in my view: simplicity and efficiency.

Fan

Will you be a fan of Fan?
Some comments about Fan by Cedric and by Stephen.

I am going through the list of features, and I like the syntax.. operators like '===' are great!

Swing Table with frozen columns part I

I have long wanted to have the feature of frozen columns in a Swing grid. I have been working on my spare time on an electronic trading user interface lately, and from my experience in the investment banking industry, I have noticed the simple following points:


  • If the user interface sucks, the user (trader) usually thinks that the whole system sucks

  • People (managers, developers) always under-estimate the user interface

  • The user interface is the most complex part of the system: it has to perform, look good and be flexible



In this series, I will show you my personal interpretation of what an E-Trading User Interface should look like.

Whether you go for NetBeans Modules, or Eclipse RCP, or write your own small framework that meets your needs, keep in mind the word “simplicity”.

Now… Collin Fagan gave us the code to hack Swing in order to display a JTable with frozen columns. You can find his code for the NonScrollingColumnsDemo here

I have used the ideas from his demo and the JXTable component from SwingX to use the column control (top-right hand corner of the table) to add the ability to add, remove frozen columns and visible columns to a grid. I call this new grid a SuperGrid because it is composed of two JXTables, the left one being the frozen grid, the right one being the normal grid where the horizontal scroll is displayed.

Follows a snapshot of the super grid (note the button on the top-right hand corner, the one with the question-mark as an icon):



There is nothing exciting here, but now if you click on the button, you should see something like:



There are two menus, a “Visibility” menu and a “Frozen” menu.
By selecting a column in the menus, you can make a column visible or not, frozen or not (moved to the left or to the right).

This is really useful as you can decide at runtime which columns to display. Each column is backed up by a config that is persisted with the column state: visible, frozen, width so that next time you start the application, the previous layout is restored.

The code is available on demand. A Web-started version will be there for you soon. The most interesting part will be the trading application!

Blog Archive