Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 1.87 KB

what_is_golfing.md

File metadata and controls

128 lines (91 loc) · 1.87 KB

What is golfing ?

Golfing is the art of use as few characters as possible to implement an algorithm.

If you want some inspirations about rules or if you just want to shorten your code, here are some tips.

if else side effect statement

Instead of writing

if condition:
    print(a)
else:
    print(b)

you can write

print([b,a][condition])

However, Python will eagerly evaluate every variable, if you need to lazily evaluate, you can alternatively write:

print(a if condition else b)

This also works for reasignement

For example

if condition:
    v += 5
else:
    v += 2

becomes

v+=2+3*condition

Iterate over a cartesian product of range

for i in range(n):
    for j in range(m):
        do_something(i, j)

can become

for i in range(n*m):
    do_something(i//n,j%m)

Iterate with a variable you do not need

for i in range(n):
    do_something_without_i()

can become

for i in'|'*n:
    do_something_without_i()

Star assignement for lists

l=list(map(int, input().split()))

can become

*l,=map(int,input().split())

Lists manipulations

l.append(a) => l+=[a] a=l[0] => a,_*=l a=l[-1] => _*,a=l

Floor and Ceil

math.floor(n) => n//1 math.ceil(n) => -(-n//1)

Raise an exception to terminate program

In most exercises, you can terminate your program by raising an exception.

n=int(input())
for i in range(n):
    do_stuff(input())

can become

input()
while 1:
    do_stuff(input())

Multiple statements

If you have multiple statement in a row, you can separate them by ;

for i in range(n):print(i);print(i+1)

Tabulations

Python accepts any kind of tabulations. Therefore, you can use a single space:

while True:
 pass