This is a solution to FonnD's Puzzle Involving Squares, written in Python 3.
You can run it like this: python3 puzzle.py
def validate_inputs(func):
# This is a decorator to check the function's
# arguments for negative values. In case of a
# negative value, a ValueError will be raised.
def validator(*args):
for param in args:
if param < 0:
raise ValueError("parameter %d is smaller than 0!" % param)
return func(*args)
return validator
@validate_inputs
def square(x):
# Doing it like this is probably not the most
# resource-efficient way, but I wanted to
# demonstrate the elegance of Python.
return sum([(x-i)**2 for i in range(x)])
@validate_inputs
def rectangle(x, y):
return sum([(x-i)*(y-i) for i in range(min(x,y))])
@validate_inputs
def cube(x):
return 3*(x+1)*square(x)
@validate_inputs
def cuboid(x,y,z):
return (z+1) * rectangle(x,y) + (y+1) * rectangle(x,z) + (x+1) * rectangle(y,z)
print(square(3))
print(rectangle(3, 4))
print(cube(3))
print(cuboid(3,4,5))
Made by strata, 03feb18 10:36am. Written in the Editor of the Beast.