java

Christian Harms's picture

facebook Hacker Cup 2012 Qualification - solution for billboards

The second problem from this year facebook hackercup sound like the classic knapsack problem. But the solution is much easier (words are in fixed sort order). Feel free to find a smarter algorithm or comment my python solution.

problem description

We are starting preparations for Hacker Cup 2013 really early. Our first step is to prepare billboards to advertise the contest. We have text for hundreds of billboards, but we need your help to design them.Read more

Nico Heid's picture

Google Code Jam - Space Emergency

Space Emergency is a nice little puzzle from Google Code Jam 2011.

The solution is quite simple. Fly as long as the boosters are built. Once they are done, reorder your remaining distance. Use the boosters on longest distances. Voila - that's it.

Let’s see the straight forward implementation for the small input.

  1.  for (int casenr = 1; casenr <= testcases; casenr++) {
  2.  
  3.             Integer time = 0;
  4.             Integer boosters = scanner.nextInt();       // L
  5.             long buildTime = scanner.nextLong();      // t
  6.             Integer finalStar = scanner.nextInt();      // N
  7.             Integer stars = scanner.nextInt();          // C
  8.             List<Integer> distances = new ArrayList<Integer>();
Read more

Nico Heid's picture

Google Code Jam - Picking Up Chicks

This is a problem I particularly enjoyed. I had to turn it around a few times in my head before I found the solution.

First I was thinking about simulation the chickens moving before noticing I am going in the totally wrong direction.
Then it dawned to me, that the solution is quite simple.

We can break the solution down into two parts.

  • Is the specific case solvable
  • How many swaps do we need

Is the specific case solvable

Leaving all other aspects aside, just check if enough chickens can make it to the barn in time. If this is not the case, it's an IMPOSSIBLE one to solve.
For this to find out, you only need to get their position, add the speed multiplied by the time you have at hands and see if they make it into safety, also known as the barn.

  1.         int finishers = 0;
  2.         boolean[] isFastEnough = new boolean[startposition.length];
Read more

Nico Heid's picture

Google Code Jam - Minimum Scalar Product

With the Google Code Jam 2011 less than four weeks away, it is time for some finger exercises.

Let's start with the Minimum Scalar Product, which should only take you a few minutes.

Here's the Java version, which will only work for the small input set. I'll explain later.

  1. public static int getMinimumScalarProduct2(int[] x, int[] y) {
  2.  
  3.         Arrays.sort(x);
  4.         Arrays.sort(y);
  5.         int sum=0;
  6.  
  7.         for (int i = 0; i < x.length; i++) {
  8.             sum += x[i] * y[x.length -1 -i];
  9.         }
  10.         return sum;
  11.     }

What we do is sort both arrays, then multiply them. One starting from the beginning, the other from the end. So our biggest numbers are multiplied with the smallest. The math is on Google's solution page, we're just looking at coding here.Read more

Nico Heid's picture

An Android SeekBar for your MediaPlayer

android logoWe all like a playback progress bar underneath our media players, so we can see how far we are and, if necesary, drag the thumb to were we want it to be.

Lets do this on Android: enter http://developer.android.com/reference/android/widget/SeekBar.html

As we will need a Runnable to update the bar regularly, let you class implement Runnable. Let's look at the run() method.

  1.    @Override
  2.     public void run() {
  3.         // mp is your MediaPlayer
  4.         // progress is your ProgressBar
  5.  
  6.         int currentPosition = 0;
  7.         int total = mp.getDuration();
  8.         progress.setMax(total);
  9.         while (mp != null && currentPosition < total) {
  10.             try {
  11.                 Thread.sleep(1000);
  12.                 currentPosition = mp.getCurrentPosition();
  13.             } catch (InterruptedException e) {
Read more

Nico Heid's picture

Android UI testing with robotium

android logo
I will base this post on the last article: Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint, where we had a TabHost and a ViewFlipper. To test that our example is still working, we have to start the app, start the tab example, click on the ViewFlipper then change to Tab2 and press the button. Of course you can do this by hand if you must, which basically always happens when you're in the middle of development -unless you're doing test driven development. But later on, you don't really want to sit there and click a lot, or even worse, enter text with your thick fingers on a tiny android device screen. So a good idea to test the UI is to get it automated in some way. That is where robotium comes into the game.Read more

Nico Heid's picture

Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint

android
This article is a follow up of Use Android ActivityGroup within TabHost to show different Activity. As you probably noticed or read in the comments, the provided solution in the last article was less than ideal. Even though it does work, it uses multiple activies and therefor has a bigger memory consumption than necessary.

What happens when you use ActivityGroup with Tabhost

In the previous example we had one Activiy per tab. One tab was even holding two Activies and using ActivityGroup to switch between them. That's a total of five Activites that end up on the activity stack. Also the hierarchy was more nested than needed.Read more

The improvement

Nico Heid's picture

Use Android ActivityGroup within TabHost to show different Activity

android

there is an updated version to this article with some improvements. please also read: Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint.

When you're using a TabHost each tab has it's own Activity. Now image you want to change the Activity for a certain tab. If you just go on and create a new Activity and display it, your Tab Layout is no longer visible.

For that reason you need a ActivityGroup within the Tab where you want to change the Activity. Read more

Nico Heid's picture

Show ProgressBar in Notification Area like Google does when downloading from Android market

android download progressWhen you download from the Market you get informed about the progress in the notification area.

That's a really nice feature, which you may want for your own download or processing progress. I will show you the basics of generating such a Notification. All you need to do is add it to your code.

I included a screenshot. You can find the code at github.

First of all, you need your Notification object:

  1.         // configure the notification
  2.         final Notification notification = new Notification(R.drawable.icon, "simulating a download", System
  3.                 .currentTimeMillis());
  4.         notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
Read more

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

Syndicate content