Wednesday, November 12, 2008

Programming Problem 1 - Train Times


The Problem


City transportation planners are developing a light rail transit system to carry commuters between the suburbs and the downtown area. Part of their task includes scheduling trains on different routes between the outermost stations and the metro center hub.

Part of the planning process consists of a simple simulation of train travel. A simulation consists of a series of scenarios in which two trains, one starting at the metro center and one starting at the outermost station of the same route, travel toward each other along the route. The transportation planners want to find out where and when the two trains meet. You are to write a program to determine those results.

This model of train travel is necessarily simplified. All scenarios are based on the following assumptions:

  1. All trains spend a fixed amount of time at each station.
  2. All trains accelerate and decelerate at the same constant rate. All trains have the same maximum possible velocity.
  3. When a train leaves a station, it accelerates (at a constant rate) until it reaches its maximum velocity. It remains at that maximum velocity until it begins to decelerate (at the same constant rate) as it approaches the next station. Trains leave stations with an initial velocity of zero (0.0) and they arrive at stations with terminal velocity zero. Adjacent stations on each route are far enough apart to allow a train to accelerate to its maximum velocity before beginning to decelerate.
  4. Both trains in each scenario make their initial departure at the same time.
  5. There are at most 31 stations along any route.
  6. The meeting time of both trains will never be at the departure of one of the trains from a station.

Input

All input values are real numbers. Data for each scenario are in the following format:



d1 d2...dn 0.0

For a single route, the list of distances (in miles - there are 5,280 feet in a mile) from each station to the metro centre hub, separated by one or more spaces. Stations are listed in ascending order, starting with the station closest to the metro centre hub (station 1) and continuing to the outermost station. All distances are greater than zero. The list is terminated by the sentinel value 0.0.


v

The maximum train velocity, in feet/minute.

s

The constant train acceleration rate in feet/minute2.

m

The number of minutes a train stays in a station.

The series of runs is terminated by a data set which begins with the number -1.0.


Output

For each scenario, the program should determine the following:

  1. The number of the scenario (numbered consecutively, starting with Scenario #1).
  2. The time when the two trains meet in terms of minutes from starting time. All times must be displayed to one decimal place. Also, if the trains meet in a station, the station number where they meet.
  3. The distance in miles between the metro centre hub and the place where the two trains meet. Distances must be displayed to three decimal places.

Sample Input

15.0 0.0
5280.0
10560.0
5.0
3.5 7.0 0.0
5280.0
10560.0
2.0
3.4 7.0 0.0
5280.0
10560.0
2.0
-1.0



Sample output



Scenario #1:

Meeting time: 7.8 minutes

Meeting distance: 7.500 miles from metro centre hub



Scenario #2:

Meeting time: 4.0 minutes

Meeting distance: 3.500 miles from metro centre hub, in station 1



Scenario #3:

Meeting time: 4.1 minutes

Meeting distance: 3.400 miles from metro centre hub, in station 1

Tuesday, November 11, 2008

SpringSource acquires G2One

SpringSource acquires G2One - the company behind Groovy and Grails. I hope it augurs well for Groovy and helps in more shops adopting it for commercial development.

Wednesday, September 3, 2008

Language Remix

I stumbled across an interesting paper on polyglot programming here.

I am convinced more than ever that in the war of programming languages, it is impossible for one language to outshine others. So should we start looking at things differently? Should we move our focus away from trying to pick a winner?

A few trends are worth taking note of.

- Developers are not scared anymore to experiment with different programming languages and styles. While they still have favourites, they are increasingly using the most suitable language or tool for a given job. This is a welcome change from the days when we were 'shoehorning' every solution into one tool or technology.

- IDEs allow one to seamlessly integrate different tools and technologies into one platform.

- Virtual machines have brought the languages closer to each other in such a way that the language has become merely a tool for expression. Code written in one or mixed languages produces the same byte code so the run time environment has become language agnostic.

These trends suggest that the time is ripe to try out a polyglot approach to development and see if it has any real benefits or not. The paper above makes a convincing argument in favor of polyglot programming.


Friday, August 29, 2008

Fluent Interfaces

A Fluent Interface is a design construct that makes your interfaces read like natural language instructions.

I kinda like how a little refactoring of method signatures can make an interface much more "interesting", shall we say ? The idea is to make the interface read more like English. So, for ex., instead of

//<>
Aggregator a = new Aggregator();
List l = dao.getfeedFromEurope();
List l1 = dao.getfeedFromAPAC();
List l2 = dao.getfeedFromAmericas();
a.aggregate(l);
a.aggregate(l1);
a.aggregate(l2);
a.setFilterPolicy(FilterPolicy.NonPayingCountries);
a.filter();
List l3 = a.getFeeds();
...


you'll write

Aggregator a = new Aggregator();
...
List l3 = a.aggregate(l).and_aggregate(l1).and_aggregate(l2).filterWith(FilterPolicy.NonPayingCountries);



Neat, isn't it? The Aggregate class methods are self-referential. In the above example, and_aggregate() just calls aggregate() internally. But the goal here is to improve readability of the interface. Of course, like everything else, fluent interfaces can be overused or misused. You can, for ex. make a very verbose interface or worse make all the interfaces fluent.

I had a difficult time explaining this construct to the code reviewers last time. I guess it takes some time getting used to this style.

The first class

The snippet below prints the first class in the stacktrace. Might be useful for logging in some situations.

public vid test ()
{
Throwable th = new Throwable();
stackTraceElement[] ste = th.getStackTrace();
System.out.println(ste[ste.length -1].getClassName());
}

I live inside Eclipse

I have a fetish for Eclipse plugins. I use
  1. Mylyn for tasks management. Super-cool productivity plugin.
  2. Database Development Plugin. Replaces crappy DBArtisan on my desktop. Lets me perform basic database operations easily. Lightweight and super-fast.
  3. Azzuri clay Database Modelling plugin to create ER diagrams and generate DDLs from them; a basic feature that Micro$oft decided not to provide in Visio Professional edition.
  4. Beyond-CVS to integrate beyond-compare (my favourite comparison tool) with Eclipse.
  5. QuickREx for those esoteric regular expressions. I don't miss RegEx buddy now.
  6. Remote System Explorer for connecting to linux machines and running telnet/ssh/ftp shells.
  7. Eclipse web browser. Limited in features but helps during basic debugging or investigation when I want to search for an inexplicable ibatis error or how to convert dates to varchar and vice versa in the database (i always keep forgetting that). Also for keeping an eye on the latest on reddit :-). Wish it offered tabbed browsing.

My wish List

  1. A Mind map plugin to let me gather my thoughts.
  2. More powerful text editor. I use block-editing heavily.
  3. A plugin for MS Outlook that lets me attach emails to Mylyn tasks.
  4. A plugin for MS Excel
  5. A powerful desktop indexing and search plugin.

Amen.

Wednesday, August 27, 2008

Random Thoughts

Xtreme Programming is not a synonym for planning-less programming. It is not a euphemism that poor management can hide behind.

Xtreme is an ideological change and should be a conscious decision.