ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

How to write Python solutions

The following compilers are available for Python programs:

The syntax is checked with the py_compile.compile function. Errors like IndentationError lead to the Compilation error judging result on this stage. Then the interpreter is invoked without additional arguments: python %1. Since Python is an interpreted language, the errors like NameError are recognized by the online judge as a Runtime error but not as a Compilation error.

How to read input data until the end of stream

To solve certain problems you should know how to read data until the end of input stream. The following example shows how to process all the lines until the end of input stream.

import sys

def process(line):
    ...

for line in sys.stdin:
    process(line[:-1])

This example shows how to read all the tokens up to the end of input stream.

import sys
tokens = sys.stdin.read().split()

Using recursion

By default the depth of a recursion in Python is very limited (about 1000 of nested calls). This limit can be increased with the command:

import sys
sys.setrecursionlimit(2000)

An example of solving a problem

A sample solution for the 1000. A + B problem in Python:

print(sum(int(x) for x in input().split(' ')))

A sample solution for the 1001. Reverse Root in Python:

import sys, math
nums = []
for line in sys.stdin:
   for word in line.split():
      nums.append(float(word))
nums.reverse()
for num in nums:
   print("%.4f" % math.sqrt(num))

More efficient solution of the same problem:

from sys import stdin, stdout
from math import sqrt
tokens = reversed(stdin.read().split())
stdout.write('\n'.join(['%.4f' % sqrt(float(t)) for t in tokens]))

Earlier compilers

  • The Python 2.7.3 was used until October 3, 2014.
  • The Python 3.3.0 was used until October 3, 2014.
  • The Python 3.4.1 was used until September 1, 2017.
  • The Python 2.7.8 was used until September 1, 2020.
  • The Python 3.6.2 was used until September 1, 2020.