Google Treasurehunt: Question 4 (Solution)
Posted by admin on June 14th, 2008 filed in google, treasurehuntComment now »
Although I unluckily at the moment do not have the time to solve the question myself I googled a little and found following excellent solution: http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/
It shows that all of the questions can be solved using the Unix-SHELL as well as any other programming language. But there’s always plenty of choice …
Google Treasurehunt: Question 3 (Solution)
Posted by admin on June 14th, 2008 filed in google, treasurehuntComment now »
First of all: I have to apologize for not posting anything (especially the answer for question 3 or even question 4), but unluckily i have been quite busy in the last weeks. I haven’t even had to look for a solution for question 4 and wasn’t able to post my answer to question 3 which I had already figured pretty soon after the question was published. I hope I’ll find enough time to solve question 4, but we’ll see
The solution for question 3 is even easier than the one for the question 2: You simply need to follow these simple rules as you go along the routing-path the packet takes:
- If there is an entry in the routing table containing the target IP follow the static route. Example: If there is a routing table entry “248.48.133.230 => 101.13.39.120″ and the target IP is 248.48.133.230 go to the Node with the IP 101.13.39.120
- If there is no static route take the default route (i.e. the default gateway)
Fore reference a question and the corresponding answer:
Google Treasurehunt: Question 2 (Solution)
Posted by admin on May 29th, 2008 filed in google, treasurehuntComment now »
Answering Question 2 doesn’t seem to be too difficult, but be aware: Most programming languages will place the first line at the index 0 of an array if you read any of the matching files into an array. Building the regular expression to match each of the path against is pretty much straight-forward:
- Before the first string there may be any number of characters
- Afterwards too
- Before the extension there has to be a “.”
- The extension has to be the end of the string
In Python this would be:
-
import re
-
r = re.compile(".*%s.*%s$" % (contains,ext))
-
if not r.match(path) is None:
-
[more code]
In PHP you’d have to put delimiters around the expression:
-
[more code]
-
}
A function to get the right solution in Python would be:
-
def get_solution(line,contains,ext,d=None):
-
r = re.compile(".*%s.*%s$" % (contains,ext))
-
ret = 0
-
if d is None:
-
d=startdir
-
for f in os.listdir(d):
-
f = os.path.join(d,f)
-
if os.path.isdir(f):
-
ret += s(line,contains,ext,f)
-
else:
-
if not r.match(f) is None:
-
f=file(f).readlines()
-
try:
-
ret+=int(f[line-1])
-
except:
-
pass
-
return ret
Where line is the Line to be added to the sum for each matching file and d the directory where the (unpacked) contents of the archive are located. The global startdir is another way to tell the function where the contents of the archive are. Using it the result for all files whose path contains “ghi” with the extension “.xml” or whose path contains “yz1″ with the extension “.pdf” can be determined like this:
-
print get_solution(4,"ghi","\\.xml")*get_solution(1,"yz1","\\.pdf")
Google Treasurehunt: Question 1 (Solution)
Posted by admin on May 20th, 2008 filed in google, treasurehuntComment now »
Recently google started a quiz called “treasurehunt” which is hosted on their new “App Engine”-service.
The first question is on combinatorics. I have seen plenty of approaches on the web for figuring out the number of unique paths the robot can take.
What surprised me most was the fact, that only very few people tried a mathematical approach. Most used different kinds of iterative algorithms to determine the correct result. This is of course possible, but most likely slower than the mathematical solution:
Looking at a 3×3 square you’ll notice, that the number of unique paths in each field form part of Pascal’s triangle:

Thus any of the fields is equal to the binomial coefficient of the line and the column index.
Knowing this a python program to compute the possible paths can be as easy as this:
-
def binomial(n,k):
-
out=1
-
for i in range(1,k+1):
-
out = (out*(n-i+1))//i
-
return out
-
def robot_unique_paths(x,y):
-
x-=1
-
y-=1
-
return binomial(x+y,x)
-
#get user-input
-
print "There are %d unique paths the robot can take!" % robot_unique_paths(
-
int(raw_input("Please enter width of the field: ")),
-
int(raw_input("Please enter the height of the field: "))
-
)
-
-
#everything after this line is only for convenience
-
import sys
-
from time import sleep
-
print "Exiting in 30 secods"
-
for i in range(1,30):
-
sys.stdout.write(".")
-
sleep(1)
-
Output:
… which is the correct answer:


