How to use Cython to speed up code for Codejam

70 views Asked by At

I am trying my hand on Codejam challenges, my main programming language is Python and therefore I am also solving all problems I face using Python. I have however started hitting TLE with code that seems OK and corresponds to the solution described in the analysis. Therefore I am looking into the option of using Cython to try and speed up my code. I can use this on my local computer I more or less understand the basics of how it works. From what I have gathered on the internet, it seems like google code jam allows the use of Cython, but I don't really know how I can use it.

As an example, I recently tried solving New Elements : Part 1 from google code jam 2019 : Round 2 (see below). What practical steps should I take to make this solution run faster using Cython? I am NOT asking for any suggestions as to how to improve this code, simply how to be able to use Cython in the codejam environment.

T=int(input())

for t in range(1, T+1):
    N=int(input())
    CJ_list=list()
    possible=True
    for n in range(N):
        C, J = map(int, input().split())
        CJ_list.append((C, J))
    ls_crossings=list()
    for n1, CJ1 in enumerate(CJ_list):
        if not possible:
            break
        for n2, CJ2 in enumerate(CJ_list):
            if n1<=n2:
                continue
            if CJ1==CJ2:
                possible=False
                break
            if CJ1[0] == CJ2[0] or CJ1[1] == CJ2[1]:
                continue
            teller=(CJ2[1]-CJ1[1])
            noemer=(CJ1[0] - CJ2[0])
            if (teller * noemer < 0):
                continue
            is_duplicate=False
            for crossing in ls_crossings:
                if crossing[0]*noemer==crossing[1]*teller:
                    is_duplicate=True
                    break
            if not is_duplicate:
                ls_crossings.append((teller, noemer))

    if possible:
        print("Case #{}: {}".format(t, len(ls_crossings)+1))
    else:
        print("Case #{}: {}".format(t, 0))
0

There are 0 answers