Apr.15
So, I had signed up for this years Google CodeJam… but then found out that I wouldn’t be able to participate because of my exams (I have been studying all week and more). I was kinda sad, but luckily for me, Google keeps ALL the CodeJam’s online for you to replay and practice.
Last night after I’d done my days worth of study, I decided to attempt some. Challenge 1, or Problem A, is called Speaking in Tongues and requires the programmer to convert letters into other letters. For example A – Z, B – Y etc… I ended up using Perl for this problem since it only required few lines of code. This is what we are given:
Problem
We have come up with the best possible language here at Google, called Googlerese. To translate text into Googlerese, we take any message and replace each English letter with another English letter. This mapping is one-to-one and onto, which means that the same input letter always gets replaced with the same output letter, and different input letters always get replaced with different output letters. A letter may be replaced by itself. Spaces are left as-is.
For example (and here is a hint!), our awesome translation algorithm includes the following three mappings: ‘a’ -> ‘y’, ‘o’ -> ‘e’, and ‘z’ -> ‘q’. This means that “a zoo” will become “y qee”.
Googlerese is based on the best possible replacement mapping, and we will never change it. It will always be the same. In every test case. We will not tell you the rest of our mapping because that would make the problem too easy, but there are a few examples below that may help.
Given some text in Googlerese, can you translate it to back to normal text?
Solving this problem
Usually, Google Code Jam problems have 1 Small input and 1 Large input. This problem has only 1 Small input. Once you have solved the Small input, you have finished solving this problem.
Input
The first line of the input gives the number of test cases, T. T test cases follow, one per line.
Each line consists of a string G in Googlerese, made up of one or more words containing the letters ‘a’ – ‘z’. There will be exactly one space (‘ ‘) character between consecutive words and no spaces at the beginning or at the end of any line.
Output
For each test case, output one line containing “Case #X: S” where X is the case number and S is the string that becomes G in Googlerese.
Limits
1 ≤ T ≤ 30.
G contains at most 100 characters.
None of the text is guaranteed to be valid English.
Input
3
ejp mysljylc kd kxveddknmc re jsicpdrysi
rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd
de kr kd eoya kw aej tysr re ujdr lkgc jv
Output
Case #1: our language is impossible to understand
Case #2: there are twenty six factorial possibilities
Case #3: so it is okay if you want to just give up
This is probably one of the easiest ever challenges Google has given for a CodeJam. Anyway, lets get to work. We can solve this either two ways. Give both sentences to different arrays, split character by character, and make it so that each character in say array1, is equal to the character in array2. Or, we could use the tr/ function in Perl to make everything very simple.
I did option 2 as so:
#!/usr/bin/perl -w
use strict;
## Open file and get the scrambled sentences
open (FILE, '<', shift);
my @contents = <FILE>;
## T = Amount of Cases
my $T = shift @contents;
my $case = 1;
## Conversion loop
foreach my $line (@contents) {
chomp $line;
$line =~ tr/a-z/yhesocvxduiglbkrztnwjpfmaq/;
print "Case #$case: $line\n";
$case++;
}
close (FILE);
Simple ![]()
I will do the rest when I have any free time.
Peace.
CodeJam 2012,

No Comment.
Add Your Comment