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 in Python 2:
# 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 resultdoesn't tooutput presult
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[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 may 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 inrequires 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, say 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)
. OnlyIn Python, only False
, 0
, None
, and empty collections are Falseyfalsey, and the rest are Truthytruthy.
More on functions
A function submission maycan 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)