Skip to main content
Minor rewordings
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131

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)

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)

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, doesn't output result
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 [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 requires 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, 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). In Python, only False, 0, None, and empty collections are falsey, and the rest are truthy.

More on functions

A function submission can 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)
deleted 1 character in body
Source Link

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)

YourYou 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)

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)

Your 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)

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)
added 35 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131

PythonPython

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:

General I/O

# 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)

Your 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)

Python

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

General I/O

# 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)

Your 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)

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)

Your 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)
fix a couple of minor typos
Source Link
ETHproductions
  • 50.1k
  • 30
  • 27
Loading
Clarified wording
Source Link
DLosc
  • 39.2k
  • 33
  • 49
Loading
deleted 1 character in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
deleted 59 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
added 2098 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
added 2098 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
added 2098 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
added 2098 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
added 2098 characters in body
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
Source Link
xnor
  • 146.6k
  • 3
  • 93
  • 131
Loading
Post Made Community Wiki by xnor