• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Code golf

Contents

  • Snake
  • Matrix determinant
  • Factorial
Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible code that solves a certain problem.

Source: Wikipedia

I've recently found some very interesting code golf examples on codegolf.stackexchange.com:

Snake

Task: Recreate the classic snake Game.

The shortest answer is written in Ruby in 316 characters, the longest is written in Java in 2239 characters.

Here is a Python answer with 818 characters:

import pygame as p
from random import randint as r

p.init()
l = 20
c = p.time.Clock()
dp = p.display
w = p.display.set_mode((500, 500))
C = p.Color
b = C(0, 0, 0)
g = C(0, 255, 0)
D = (0, 1)
U = (0, -1)
L = (-1, 0)
R = (1, 0)
S = [R]
d = R
n = []
O = lambda t: {U: D, R: L, D: U, L: R}[t]


def Q(e):
    print("Score: %i" % (len(S) - 1))
    p.quit()


def K(e):
    global d
    _ = {276: L, 273: U, 274: D, 275: R}.get(e.key, (0, 0))
    d = not _ == O(d) and _ or d


def N(S):
    [p.draw.rect(w, g, [x[0] * l, x[1] * l, l, l]) for x in S + n]


def M():
    n = (r(0, 24), r(0, 24))
    return n not in S and n or M()


A = lambda s, o: tuple(x + y for x, y in zip(s, o))
n = [M()]
while True:
    w.fill(b)
    [{12: Q, 2: K}.get(e.type, lambda e: e)(e) for e in p.event.get()]
    if not (0 <= S[-1][0] < 25 and 0 <= S[-1][1] < 25) or A(S[-1], d) in S:
        Q(e)
    if A(S[-1], d) in n:
        S.append(A(S[-1], d))
        n = [M()]
    else:
        S.append(A(S[-1], d))
        S.pop(0)
    N(S)
    dp.update()
    c.tick(6)

Matrix determinant

Task: Calculate the determinant of a $n \times n$ matrix.

Solution in J (61 characters):

-/>([:+/#(([{.<:@[}.])[:*//._2,\2#])])&amp;.>(|.;])".];._2[1!:1[3

Solution in Python (198 characters):

t = input()
e = enumerate
p = (
    lambda t: t
    and ((b + [a], j + i) for i, a in e(t) for b, j in p(t[:i] + t[i + 1 :]))
    or [([], 0)]
)
print(
    sum(
        reduce(lambda t, (i, r): t * r[i], e(p), 1 - i % 2 * 2)
        for p, i in p([t] + [input() for x in t[1:]])
    )
)

Factorial

Task: Find the factorial of a number.

J (12 characters):

f=:*/@:>:@i.

Python (27 characters):

f = lambda x: 0 ** x or x * f(x - 1)

By the way, the shortest Java solution is 85 characters long^^.

Published

Sep 27, 2012
by Martin Thoma

Category

Code

Tags

  • Code golf 2
  • J 1
  • Python 141

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor