java

Nico Heid's picture

Google Code Jam - Rotate

It's time for some basic finger exercise. The Google Code Jam Rotate is very trivial, so relax and fire up your IDE.

I was a bit lazy, so there is no reading of the input sets, just a two-dimensional array and two functions

Rotating

As the Google solution pointed out, there is actually no need to really rotate the 2dim array. Just push everything to the right, as if gravity would be to the right. That's the same as rotating everything and keeping gravity towards the bottom. But we save a few lines this way.

So here is the "gravity from the right" code

  1. public static void fakeRotate(char[][] board) {
  2.  
  3.     for (int i = 0; i < N; i++) {
  4.         for (int j = N - 1; j >= 0; j--) {
  5.             if (board[i][j] != '.') {
  6.                 // push to right
  7.                 int m = 1;
  8.                 while ((j + m) < N && board[i][j + m] == '.') {
Read more

Nico Heid's picture

Google Code Jam - Theme Park

Theme Park is another good example of a problem which can be solved with a fairly simple algorithm. The simple version will solve the small input set without a problem, but will run almost infinitely with the large dataset.

You can read the problem on the Google Code Jam site: Theme Park

simple solution

The simple solution is to put the groups in a static array, use a pointer that "wraps around" using modulo and fill the roller coaster until it's so full, that the next group does not fit in, then let it ride earning income accordingly to the seats taken.

This is a correct solution, but unfortunately a bit slow. The input set can have a roller coaster with up to 109 seats and 108 rides and groups as large as 107. Read more

Nico Heid's picture

Google Code Jam - The Snapper Chain

The Google problems are always nice, sometimes even a bit too tricky. This years entry problem was quite nice with some difficulties you could run in.

You need to read the problem first, to follow the article, so here's the link: http://code.google.com/codejam/contest/dashboard?c=433101#s=p0&a=0

If you're a hands on programmer first you might just want implement the chain without looking at the problem more extensively. So you might end up with something like this:

  1.                         // snapping
  2.                         boolean toggle = true;
  3.                         for (int j = 0; j < snaps; j++) {
  4.                                 toggle = true;
  5.                                 for (int k = 0; k < snapperCount; k++) {
  6.                                         if(toggle == false){
  7.                                                 break;
  8.                                         }
  9.                                         if (toggle == true) {
  10.                                                 snapper[k] = !snapper[k];
  11.                                         }
  12.                                         if (snapper[k] == true) {
  13.                                                 toggle = false;
  14.                                         }
  15.  
  16.                                 }
  17.                         }
  18.  
  19.                         // we need power on all snappers
  20.                         boolean power = true;
  21.                         for (int j = 0; j < snapperCount; j++) {
  22.                                 if (snapper[j] == false) {
Read more

Nico Heid's picture

Google Code Jam - Africa Qualification Round 2010

The Google Code Jam is an interesting option to work on some challenging problems.
With the first qualification round coming up at May 7th, it's time to look at the Africa qualification round, to see what to expect.

You have 24 hours to solve three rather simple problems. Solving two problems brings you into the next round.

I will present my Java solutions. The Code Jam site provides the code of every contestant.

Store Credit

read problem description
You just need two nested loops to find the matching articles. Just a finger exercise.

  1. for (int i = 0; i < items.size(); i++) {
  2.                         for (int j = i + 1; j < items.size(); j++) {
  3.  
  4.                                 // is this the right shopping?
  5.                                 int newSum = items.get(i) + items.get(j);
  6.  
  7.                                 if (newSum == credit) {
  8.                                         return "Case #" + caseNr + ": " + (i + 1) + " " + (j + 1);
  9.                                 }
  10.                         }
  11.  
  12. }
Read more

Phillip Steffensen's picture

Developing a simple SOAP-webservice using Spring 3.0.1 and Apache CXF 2.2.6

In the past few years many techniques have been developed to help applications interact with each other. One of them are webservice-interfaces. These interfaces are extremly popular in the world of Java software development. One Framework that can be used to build such interfaces is Apache CXF. Apache CXF delivers a toolset to develop interfaces using different protocols like XML/HTTP, RESTful HTTP, Corba and SOAP. In this article i'd like to show how easy it could be to develop a simple SOAP-webservice based on Apache CXF 2.2.6 and the Spring Framework 3.0.1. You can download the full example at the bottom of this article.

Read more

Phillip Steffensen's picture

Spring Module OXM – A new feature of Spring Framework 3.0

Since a few days Spring 3.0 is out. The frameworks core APIs (e.g. the BeanFactory) have been updated for Java 1.5. But there are also some new features in Spring 3.0. Today I will take a look on Springs new OXM-feature and see how it can be used. Naturally all features added to the Spring framework are easy to use. Let's see if Springs simplicity still exists...

Read more

Matthias Reuter's picture

The art of escaping

Escaping is the art of transforming a text into a transport format from which it can be extracted again without any modification.

That's something every developer does - to a certain level. For example take the sentence

Matthias says: "I love Javascript".

Now put this sentence in a Java source code:

String s = "Matthias says: "I love Javascript".";

If you don't complain about this, your compiler will. In Java (any many other programming languages) the quotation mark " has a special meaning, it defines a string. So if you have a string containing the quotation mark, you need to escape it:

String s = "Matthias says: \"I love Javascript\".";

If you print out s, the original text is shown:

System.out.print(s); // Matthias says: "I love Javascript".
Read more

Nico Heid's picture

Java Challenge: Dropping Balloons in Java

Recently I read about a nice article in one of my monthly magazines. I won't give you the author right now, otherwise googling for the solution would be too easy.
He stated it as a possible interview question, but I really like it for the algorithm side of it. It reminded my strongly of some of the
examples given in the book How Would You Move Mount Fuji? Which is excellent by the way.

So your task is to write a Java function which computes the following:Read more

Nico Heid's picture

Regular Expression examples in Java

When I first had to use regular expressions in Java I made some fairly common mistakes.
Let's start out with a simple search.

simple string matching

We want to search the string: asdfdfdasdfdfdf for occurences of dfd. I can find it four times in the String.
Let's evaluate what our little Java program says.

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexCoding {
  5.  
  6.         public static void main(String[] args) {
  7.  
  8.                 String source = "asdfdfdasdfdfdf";
  9.                 Pattern pattern = Pattern.compile("dfd");
  10.                 Matcher matcher = pattern.matcher(source);
  11.                 int hits = 0;
  12.                 while (matcher.find()) {
  13.                         hits++;
  14.                 }
  15.                 System.out.println(hits);
  16.  
  17.         }
  18. }

The result should be 2. So either our code is wrong, or the logic works differently than expected. And indeed, it does.Read more

Phillip Steffensen's picture

Maven 2 (Part 3): Configuring eclipse for Apache Maven 2 projects

Today we're going on with the third part of our Maven 2 tutorial series. Because of the comment of Enrico I decided that this article will focus on how to configure eclipse for the usage of Maven 2 projects and how to generate the eclipse-specific files by using Maven 2. I will show these things by using the example project of part 1 and part 2 of our Maven 2 tutorial series.

Read more

Syndicate content