Strange, using eval worked just fine for me.
Here's my code:
import sys, collections, re
output_line = "Case #{X:d}:"
openmatcher = re.compile(r"\(\s+")
closematcher = re.compile(r"\s*\)")
spacematch = re.compile(r"\s+")
wordmatch = re.compile(r"([a-zA-Z]+)")
if __name__ == "__main__":
infile, outfile = sys.argv[1:]
with open(infile, "r") as inhandle, open(outfile, "w") as
outhandle:
N = int(inhandle.readline())
for n in range(N):
L = int(inhandle.readline())
grammar = ""
for l in range(L):
grammar += inhandle.readline()
grammer = grammar.strip()
grammar = openmatcher.sub(r"(", grammar)
grammar = closematcher.sub(r",)", grammar)
grammar = spacematch.sub(r", ", grammar)
grammar = wordmatch.sub(r"'\1'", grammar)
tree = eval(grammar)
tree = tree[0]
print(output_line.format(X=n+1), file=outhandle)
A = int(inhandle.readline())
for a in range(A):
animal, n, *features = inhandle.readline().split()
features = set(features)
loc = tree
probability = loc[0]
while len(loc) > 1: # we haven't hit a leaf node yet
if loc[1] in features:
loc = loc[2]
else:
loc = loc[3]
probability *= loc[0]
print("{0:0.7f}".format(probability), file=outhandle)
NOTE: This is python 3.1
On Sep 12, 11:34 am, "[email protected]" <[email protected]>
wrote:
> Missed it by 23 spots =[[[[.
> I solved A-small and then began to solve A-large.
> I was using eval() in python to parse the tree.
> On the large input it gave me a memory error!
>
> Is there anything I could have done besides parsing the tree manually
> or do I deserve this since I was lazy and used eval()?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---