Google Code Jam - Africa Qualification Round 2010

Nico Heid's picture

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. }

Reverse Words

read problem description
Just reverse a list of words? Guess that's doable. Well, the code is not elegant, but it works.

  1. for (int i = 1; i <= cases; i++) {
  2.                         out.writeBytes("Case #"+i+": ");
  3.                         String[] words = in.readLine().split("\\ ");
  4.                         List<String> wordList = Arrays.asList(words);
  5.                         Collections.reverse(wordList);
  6.                         for(Iterator it = wordList.iterator(); it.hasNext();){
  7.                                 out.writeBytes(it.next()+" ");
  8.                         }
  9.                         out.writeBytes("\n");
  10. }

T9 Spelling

read problem description
The first task with a little challenge. You have to insert a break when you have letters on the same numeric button. So I checked if the previous button is the same as the recent on. If yes, insert a break. So you can separate AB (2 22) from BA (22 2). That's all.

  1. public class T9Spelling {
  2.  
  3.         /**
  4.          * @param args
  5.          */
  6.  
  7.         static HashMap<String, String> t9Map;
  8.  
  9.         public static void main(String[] args) throws NumberFormatException,
  10.                         IOException {
  11.  
  12.                 FileInputStream inFile = new FileInputStream(new File(
  13.                                 "C-large-practice.in"));
  14.                 DataInputStream in = new DataInputStream(inFile);
  15.                 FileOutputStream outFile = new FileOutputStream(new File("out.txt"));
  16.                 DataOutputStream out = new DataOutputStream(outFile);
  17.  
  18.                 int cases = Integer.parseInt(in.readLine());
  19.  
  20.                 t9Map = new HashMap<String, String>();
  21.                 fillMap();
  22.  
  23.                 for (int i = 1; i <= cases; i++) {
  24.                         out.writeBytes("Case #" + i + ": ");
  25.                         String t9, t9last = "";
  26.  
  27.                         String text = in.readLine();
  28.                         for (int j = 0; j < text.length(); j++) {
  29.  
  30.                                 t9 = t9Map.get(String.valueOf(text.charAt(j)));
  31.                                
  32.                                 // do we need a break? (letters are on same button)
  33.                                 if (j > 0 && (t9.charAt(0)) == t9last.charAt(0)) {
  34.                                         out.writeBytes(" ");
  35.                                 }
  36.  
  37.                                 out.writeBytes(t9);
  38.                                 t9last = t9;
  39.                         }
  40.                         out.writeBytes("\n");
  41.                 }
  42.  
  43.         }
  44.  
  45.         public static void fillMap() {
  46.                 t9Map.put("a", "2");
  47.                 t9Map.put("b", "22");
  48.                 t9Map.put("c", "222");
  49.                 t9Map.put("d", "3");
  50.                 t9Map.put("e", "33");
  51.                 t9Map.put("f", "333");
  52.                 t9Map.put("g", "4");
  53.                 t9Map.put("h", "44");
  54.                 t9Map.put("i", "444");
  55.                 t9Map.put("j", "5");
  56.                 t9Map.put("k", "55");
  57.                 t9Map.put("l", "555");
  58.                 t9Map.put("m", "6");
  59.                 t9Map.put("n", "66");
  60.                 t9Map.put("o", "666");
  61.                 t9Map.put("p", "7");
  62.                 t9Map.put("q", "77");
  63.                 t9Map.put("r", "777");
  64.                 t9Map.put("s", "7777");
  65.                 t9Map.put("t", "8");
  66.                 t9Map.put("u", "88");
  67.                 t9Map.put("v", "888");
  68.                 t9Map.put("w", "9");
  69.                 t9Map.put("x", "99");
  70.                 t9Map.put("y", "999");
  71.                 t9Map.put("z", "9999");
  72.                 t9Map.put(" ", "0");
  73.         }
  74.  
  75. }

Round 1 was quite simple, wasn't it? There's still time to register.
See you in the contest.

Comments

Christian Harms's picture

What is considered cheating?

Collaborating with anyone else during the contest is strictly prohibited and will result in your disqualification. This includes discussing or sharing the problem statements or solutions with others. Creating or participating with multiple accounts is also prohibited. If we believe that you have undermined the integrity of the contest, we reserve the right to disqualify you. Use your best judgment. If you have a question about whether something is allowed, please ask an administrator.

Hoping your solution will not kick you out of the normal contest in europe ...

Nico Heid's picture

They displayed EVERY solution in any language for Africa 2010.
The even discussed the solution for Round 2, and how to solve it.

So I doubt that this has any relevance to future Code Jams.

And we are not "during" the contest.

Christian Harms's picture

Ok, I should read the first sentence carefully: ... during the contest ... see you in round 2!