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();
}
}
}
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
Facebook Hacker Cup - Double Squares Solution
Here's my java solution for the Double Squares problem in the Facebook Hacker Cup
Results in less than a minute
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.
2010 | Gym | Swimming | Football | Tag Rugby | Weight (kg) |
January | 7 | 8 | 2 | 0 | 94.5 |
February | 12 | 7 | 4 | 0 | 93.8 |
March | 11 | 8 | 3 | 0 | 94 |
April | 9 | 6 | 5 | 0 | 95 |
May | 7 | 5 | 4 | 5 | 96 |
June | 5 | 6 | 4 | 7 | 94.3 |
July | 5 | 6 | 4 | 4 | 94.5 |
August | 2 | 4 | 3 | 4 | 94.6 |
September | 6 | 8 | 5 | 4 | 94.6 |
October | 5 | 6 | 3 | 4 | 94.8 |
November | 7 | 6 | 4 | 2 | 95 |
December | 1 | 3 | 0 | 0 | 95 |
Total | 77 | 73 | 41 | 30 |
Subscribe to:
Posts (Atom)