Python all right, 72 74 75 chars
p=lambda i,j,k,l,m:j-i!=k-j and(k==j*j==j*i*i and m*m or m*l/k)or m+j-i
Thx to xnor for showing me the if
/else
to and
/or
trick and golfing off a char :)
Thx to flornquake for fixing the k==j*j==j*i*i
and saving another two chars :)
Actually, it gets one wrong... but that's a bug in the test patterns which was acknowledged yesterday but still not fixed.
Testing code, hopefully useful for all competitors to test with:
def check(func,title):
right = 0
print "===", title, "==="
for i,test in enumerate(open('patterns.txt')):
test = map(int,test.split(','))
try:
got, expected = func(*test[:5]), test[5]
except Exception as e:
got = e
if got != expected:
print "ERROR on line %d: %s != %d" % (i, got, expected)
print " test :", ", ".join(map(str,test))
plus = multi = cube = str(test[0])
for i in range(1, 6):
plus += ", %d" % (test[i-1]+(test[1]-test[0]))
multi += ", %s" % ((test[i-1]*(test[1]/test[0])) if test[0] else "DivZero")
cube += ", %d" % (test[i-1]**2)
print " as + :", plus
print " as * :", multi
print " as ^2:", cube
else:
right += 1
print right, "out of", i+1, "right!"
says:
ERROR on line 19: 23283064365386962890625 != 3273344365508751233
test : 5, 25, 625, 390625, 152587890625, 3273344365508751233
as + : 5, 25, 45, 645, 390645, 152587890645
as * : 5, 25, 125, 3125, 1953125, 762939453125
as ^2: 5, 25, 625, 390625, 152587890625, 23283064365386962890625
1834 out of 1835 right!
3It seems fairly easy to get 100%. Is there a tiebreak? – xnor – 2014-09-18T05:57:57.893
The tiebreaker should be the length of the code because any solution can easily be O(n) time, i'll edit the question. Well this is just a duplicate of the other question then, should it be closed? – rodolphito – 2014-09-18T06:43:12.837
In that case, why don't you make it code golf and require getting all the test cases right? – xnor – 2014-09-18T06:46:24.003
I wouldn't consider it a duplicate. In the other question, different operations are applied cyclically, which makes it a lot harder than a single operation applied repeatedly in this one. – xnor – 2014-09-18T06:47:59.563
Thanks for the advice, edited. I was considering using doubles and decimals instead of integers for this question, but then I would have to get tangled in all the rounding and accuracy check and such. – rodolphito – 2014-09-18T06:51:57.140
What's the story with
4,16,256,65536,4294967296,0
in the pattern? Is it a bug due to you not using an arbitrary precision type or what? – Will – 2014-09-18T06:54:47.140Most likely. Sorry about that. I will change the link in a minute. – rodolphito – 2014-09-18T06:57:15.567
Heh, I had used a java long to hold the number, it couldn't fit 2^64... – rodolphito – 2014-09-18T07:03:02.580
We also seem to disagree on what
152587890625 * 152587890625
is. – Will – 2014-09-18T07:10:44.320Fixed that too, thanks for spotting those. I'll be off to bed now haha its 3am. I will fix anything else that is found when I wake up. Thanks again :) – rodolphito – 2014-09-18T07:19:41.670
Now that code length matters: Is the code required to open the given file and run on the test cases? – xnor – 2014-09-18T07:25:57.013
1Its pretty disappointing there are no trick rows; contestants are getting a perfect score by checking just the first three ints, and there's no divide-by-zeros being thrown either. – Will – 2014-09-18T07:53:06.987
2
-1 for doing big changes in the scoring system. Please use the sandbox the next time
– John Dvorak – 2014-09-18T08:39:55.690@JanDvorak ok I will use that, thanks for telling me :) (I had no idea it existed haha) – rodolphito – 2014-09-18T14:38:39.357
@Rodolvertice most sandbox questions get a great response on the main site. However, I think some people like to get a head start on questions while they are in the sandbox, so if you want people on an equal footing for whatever reason, you aren't required to post it there. It's a useful tool but not necessary in every case. – hmatt1 – 2014-09-18T20:03:43.267