Modifying a compiled executable

Oh no! Your boss, Harry, is leaving the company. He was a good guy and a good boss, who appreciated the simple things in life. One of those simple things was a little program called “who-the-boss” which, when run, printed out “Harry the boss!”. His replacement is called Larry. Larry is a short-tempered man with little patience for anything he considers wrong, which is everything. One day, he finds Harry’s program on a machine everyone forgot existed. [Read More]

Calling Nim from Python

In this post I’ll show you how to write a function in Nim and call it in Python. Nim To start with we’ll need a Nim function. Here’s a one that takes 2 integers, adds them and returns the result: called.nim proc nim_add(num1: int, num2: int): int {.exportc.} = return num1 + num2 Most of this is pretty ordinary, except for the {.exportc.} in the function signature. This is a pragma, which provides metadata about a function to the Nim compiler. [Read More]

Automatic memoization of function calls in Python

The Fibonacci sequence is the integer sequence where each number is the sum of the previous two numbers, starting with zero and one: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Writing a function to calculate an arbitrary fibonacci number is a common problem in interviews and tests, that has an elegant if inefficient way of being solved via recursion. Here’s a simple solution in Python: def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) Given an integer n, this function will calculate the n’th number in the fibonacci sequence. [Read More]