Thursday, January 13, 2011

Facebook Hacker Cup - Studious Student Solution

Here's my solution to the Studious Student problem in the Facebook Hacker Cup in Java


package com.facebook;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class StudiousStudent {

public StudiousStudent(String wordLine) {

ArrayList strings = new ArrayList();
StringTokenizer st = new StringTokenizer(wordLine);
int wordCount = Integer.parseInt(st.nextToken());
while (st.hasMoreTokens())
strings.add(st.nextToken());

String[] stringArray = (String[]) strings.toArray(new String[strings.size()]);
for (int index1 = 0; index1 < wordCount; index1++) {

for (int index2 = 0; index2 < wordCount; index2++) {

if ((stringArray[index1] + stringArray[index2]).compareTo(stringArray[index2] + stringArray[index1]) > 0 && (index1 < index2)) {

String tmp = stringArray[index1];
stringArray[index1] = stringArray[index2];
stringArray[index2] = tmp;

}

}

}
for (int counter = 0; counter < stringArray.length; counter++)
System.out.print(stringArray[counter]);

System.out.println();

}

public static void main(String[] args) {

BufferedReader input = null;
try {

input = new BufferedReader(new FileReader("studiousStudent.txt"));
String inputLine = input.readLine();
Integer wordLineCount = Integer.parseInt(inputLine);
for (int counter = 0; counter < wordLineCount; counter++) {

new StudiousStudent(input.readLine());

}
input.close();

} catch (Exception e) {

System.out.println("e.getMessage():" + e.getMessage());
e.printStackTrace();

}

}

}

Facebook Hacker Cup - Double Squares Solution

Here's my java solution for the Double Squares problem in the Facebook Hacker Cup

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;

public class Squares {

public Squares(int square) {

double squareRoot = Math.sqrt(square);
int squareCount = 0;
for (int counter1 = 0; counter1 <= squareRoot; counter1++) {

for (int counter2 = counter1; counter2 <= squareRoot; counter2++) {

if((counter1 * counter1) + (counter2 * counter2) == square)
squareCount++;

}

}
System.out.println(squareCount);
}

public static void main(String[] args) {

BufferedReader input = null;
try {

input = new BufferedReader(new FileReader("squares.txt"));
String inputLine = input.readLine();
Integer squares = Integer.parseInt(inputLine);
for(int counter = 0; counter < squares; counter++) {

new Squares(Integer.parseInt(input.readLine()));

}
input.close();

}
catch(Exception e) {

System.out.println("e.getMessage():" + e.getMessage());
e.printStackTrace();

}

}
}

Results in less than a minute

Sunday, January 09, 2011

2010 - A Year In Fitness

Finished the year more or less as I started. Went to the gym more in February than I did in November and December combined.
2010GymSwimmingFootballTag RugbyWeight (kg)
January782094.5
February1274093.8
March1183094
April965095
May754596
June564794.3
July564494.5
August243494.6
September685494.6
October563494.8
November764295
December130095
Total77734130