Fast Python!
Coding competitions
with CPython and PyPy

Alessandro Amici <a.amici@bopen.eu> - @alexamici

B-Open, Rome - http://bopen.eu (now hiring!)

Competition languages stats

Python wins competitions! (1/3)

Python wins competitions! (2/3)

Python wins competitions! (3/3)

The plan

Intro to Google Code Jam

Solve a practice problem live!

Theoretical analysis of coding competitions

Programming language comparison

How do I start or get better at competing?

What do I learn for everyday programming?

Life, the universe and everything

Intro to Google Code Jam

https://code.google.com/codejam

Once per year, several on-line rounds a on-site round and $20k prizes

Solve three to five problems with known-correct solution

All programming language allowed:

     download input, run code on your computer, upload output

Competition is timeboxed to a few hours, code runtime to a few minutes

A small and a large input datasets with different scores

Ranking by problem score and submission time

Problem definition anatomy

Problem statement:

    problem universe with actors, parametric rules and a question

Limits:

    range for parameters and initial conditions for small and large inputs

Sample test cases:

    the set of parameters and initial conditions along with the solutions

Test cases:

    the set of parameters and initial conditions to solve

Problem solving anatomy

Constraints:

    CPUs, RAM, storage space

    code running time, wall-clock time

Coding phases:

    modeling

    coding

    performance-tuning (optional)

    debugging (optional)

    runtime

GCJ training session, live!

Problem A "Steed 2: Cruise Control" Round 1B 2017: https://goo.gl/QHxDXs

Solution analysis: "simple solvable" problem

Constraints:

    CPUs, RAM, storage space

    code running time, wall-clock time

Coding phases:

    modelling: 2m

    coding: 2m

    performance-tuning: 0

    debugging: 1m

    runtime: 1m

"Generalised solvable" problem

Constraints:

    CPUs, RAM, storage space

    code running time, wall-clock time

Coding phases:

    modelling: 2m ⎯ 20m

    coding: 2m ⎯ 20m

    performance-tuning: 0 ⎯ 20m (PyPy beats CPython)

    debugging: 1m ⎯ 60m

    runtime: 1m ⎯ 8m

"Simple unsolvable" problem

Constraints:

    CPUs, RAM, storage space

    code running time, wall-clock time

Coding phases:

    modelling: 20m ⎯ ∞

    coding: 2m ⎯ 20m

    performance-tuning: 0 ⎯ 20m

    debugging: 1m ⎯ ∞

    runtime: 1m ⎯ 8m

"Generalized unsolvable" problem

Constraints:

    CPUs, RAM, storage space

    code running time, wall-clock time

Coding phases:

    modelling: ∞

    coding: ∞

    performance-tuning: ∞

    debugging: ∞

    runtime: 4m ⎯ 8m

Programming language comparison

C++ JAVA CPython PyPy
modelling Good Good Excellent
(Libraries)
Very good
​(Libraries)
coding Very good Good Excellent Excellent
performance-tuning Excellent Very good
(JIT)
Not so good
(RAM, loops)
Very good
debugging Bad Not so good Excellent Excellent

Strategic thinking

Wall-clock time is the paramount asset

    time management is the key

Problem selection

    go for simpler problems first

    go for simple (even brute force) solutions for small inputs first 

    keep an eye on the scoreboard

        check what the pros are doing

        address problems that can make you pass the round

Focus and stress management

Focus management:

    round strategy and problem strategy

    do not make mistakes while coding (KISS)

    be ready to question all your assumptions

Stress management:

    keep calm in the face of a ticking wall-clock

    keep calm in the face of the risk of public shaming

    never give up

How do I start or get better at competing?

1. Practice

2. Practice

3. Practice

How do I start or get better at competing?

Practice:

    practice real world competitions, from start to end. With a timer!

    go for volume first, solve a lot of "simple solvable" problems

    then go for "generalised solvable" problems

    learn from the code of other competitors

    learn to use libraries (networkx, pulp, gmpy2, scipy, etc.)

    build your library of custom code

Learn data structures and algorithms:

    "Learn To Think Like A Computer Scientist" by Tim Roughgarden

What do I learn for everyday programming?

Wall-clock time management:

    modelling: days

    coding: days

    performance-tuning: weeks

    debugging: weeks

Strategic thinking

    solve simpler problems first

    keep an eye on the scoreboard

Life, the universe and everything

Focus and stress management:

    a bug in production

    client-facing problem solving

    job interviews

    ...

    you are trapped in a fire!

Strategic thinking:

    wall-clock time management

    keep an eye on the scoreboard

Thank you!

Alessandro Amici

<a.amici@bopen.eu>

@alexamici

 

B-Open - http://bopen.eu

Satellites, Python, Rome, now hiring!

Fast Python! Coding competitions with CPython and PyPy - EuroPython 2017

By Alessandro Amici

Fast Python! Coding competitions with CPython and PyPy - EuroPython 2017

In a coding competition you want to solve problems “fast”… why would you choose a “slow” interpreted language like Python? Because the “slow” Python wins competitions more often that most people think. We will show how coding competitions work, what are the resources and constraints that competitors need to take into account, and we will find out that, like very often in real life, the actual processing time is only a small term in the complex equation that describe a competition. The “faster” PyPy may help in gaining raw speed, but that is not the real advantage it gives you in a competition.

  • 996

More from Alessandro Amici