Advertisement

Anyone heard of codility? (Automated Programming Assessment)

Started by February 20, 2010 04:13 PM
35 comments, last by janta 14 years, 8 months ago
Got a 94/100 on the same test, took 10 minutes to plan it out and then write it.
int total(const vector<int> &A){    int ret = 0;    for (size_t i = 0; i < A.size(); ++i)        ret += A;    return ret;}int equi ( const vector<int> &A ) {    if (A.empty()) return -1;    if (A.size() == 1) return 0;    int right = total(A);    int left = 0;    for (size_t i = 0; i < A.size(); ++i)    {        right -= A;        if (left == right)            return i;        left += A;    }    return -1;}


I got a 94 because it failed one of the tests:
Quote:
extreme_large_numbers
Sequence with extremly large numbers testing arithmetic overflow. 0.004 s. WRONG ANSWER
got 2, but it is not equilibrium point, sum[0..1]=4294967294, sum[3..3]=-2


I didn't think to test for overflow, that is such an obscure case [wow]... Python doesn't have that issue, does it?
Quote: Original post by nullsquared
I didn't think to test for overflow, that is such an obscure case [wow]... Python doesn't have that issue, does it?


No, int types are converted to bignums as needed, implicitly.
Advertisement
Quote: Original post by nullsquared
I didn't think to test for overflow, that is such an obscure case [wow]... Python doesn't have that issue, does it?
I guess not, I also failed that test.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Quote: Original post by benryves
Quote: Original post by nullsquared
I didn't think to test for overflow, that is such an obscure case [wow]... Python doesn't have that issue, does it?
I guess not, I also failed that test.


What score did you get? I'm curious if the elapsed time has any effect on the score.
Quote: Original post by nullsquared
Quote: Original post by benryves
Quote: Original post by nullsquared
I didn't think to test for overflow, that is such an obscure case [wow]... Python doesn't have that issue, does it?
I guess not, I also failed that test.


What score did you get? I'm curious if the elapsed time has any effect on the score.
69; I completely misinterpreted the question the first time around and had got so annoyed by the editor by that point I just kludged together a stupid O(n2) implementation that failed the tests with very long sequences of numbers (timed out). I don't think time matters as long as it doesn't time out.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Quote: Original post by Sneftel
EDIT: I think I actually prefer crusadingknight's approach, since it doesn't involve any special-casing for the last iteration.


Actually, after seeing yours it occurred to me that the approaches could be combined to simplify things further:
def equi(A):    total = sum(A)    accum = 0    for idx in range(len(A)):        if accum == total - accum - A[idx]:            return idx        accum += A[idx]    return -1
Advertisement
Quote: Original post by benryves
I don't think time matters as long as it doesn't time out.


Yeah I just tried it again, it doesn't matter. And this time I got 100/100, added an ugly hack to pass their big numbers test [grin]:
#include <numeric>int equi ( const vector<int> &A ) {    if (A.empty()) return -1;    if (A.size() == 1) return 0;    int right = std::accumulate(A.begin(), A.end(), 0);    int left = 0;    for (size_t i = 0; i < A.size(); ++i)    {        right -= A;        if (left == right)            return i;        if (left + A < left && A > 0)            return -1;        left += A;    }    return -1;}
Quote: Original post by crusadingknight

Ah, very nice.
Just something I found funny:
lol
This was my Python attempt. It looks pretty similar to Sneftel's except that it doesn't special case the last iteration.
def equi ( A ):  a = 0  b = sum(A)  for i in range(len(A)):    b -= A    if a == b:      return i    a += A  return -1

This topic is closed to new replies.

Advertisement