Skip to main content
12 of 13
deleted 1 character in body

Python

General I/O

Your submission should be a program or a function. It should print the output or return it. These example submissions compute the factorial:

# Program that prints
n=input();p=1
for i in range(1,n+1):p*=i
print p

# Defined function that outputs or prints
def f(n):
 p=1
 for i in range(1,n+1):p*=i
 print p

# Lambda function, no name needed
lambda n:reduce(int.__mul__,range(1,n+1))

# Lambda function, named to use recursive call
f=lambda n:1 if n==0 else n*f(n-1)

You may not expect input pre-written to a variable.

# Invalid, expects input in n
p=1
for i in range(1,n+1):p*=i
print p

Nor may you output just by saving the result to a variable.

# Invalid, saves result to p
n=input();p=1
for i in range(1,n+1):p*=i

Nor can the output just be the value of an expression like in the interactive shell.

Inputs

We're liberal about input formats. For example, if a challenge says to take a list of numbers, you may expect a Python list like l=[1,2,3], not like "1 2 3". So, a Python 2 program can do

l=input()

rather than

l=map(int,raw_input().split())

Likewise, when a program takes string input, you expect it in quotes to use input() in Python 2 rather than raw_input().

Version

Either Python 2 or Python 3 is fine. If the code only works correctly in a specific version, include the version number in the header.

Libraries

You may import libraries. The import statement counts as part of the code length. If it's a non-default library like scipy, call the language "Python with scipy" in the header.

Truthy/Falsey

Some challenges ask for a output to be truthy or falsey, which is determined by the value of bool(x). Only False, 0, None, and empty collections are Falsey, and the rest are Truthy.

More on functions

A function submission may include helper code outside the function, for example

import re;r=range
lambda l: ...

Functions may use extra optional arguments. For example, this factorial function submission expects a single number, but uses the optional input i to help recurse:

f=lambda n,i=1:1 if i>n else i*f(n,i+1)

You may not, however, require the function to be called with a specific extra argument, like "call this with 2nd argument 1" for

f=lambda n,i:1 if i>n else i*f(n,i+1)
xnor
  • 146.6k
  • 3
  • 93
  • 131