Tag Archives: Python

This was an pretty interesting read, TimSort.java
TimSort is a stable form of merge-sort algorithm invented by Tim Peters. Tim orginal wrote the sorting algorithm for the python language and was ported into the Java language by Josh Bloch.

This will be a very brief post on the sample usage of python’s lambda function. The following code is an identity function that will return the argument passed in. Nothing too special but it will help demonstrate the syntax. In this case I’m defining the symbol id to the lambda function.

id = lambda x: x

The following snippet contains some sample use cases of the id symbol.

Here is a code snippet that is a bit more useful. The following code is a bit hack that finds the least significant bit(LSB) in a number, meaning the lowest bit that is used to generate the given number.

lsb = lambda x: x & (-x)

As an example, passing in 8 (1000) will return 8 as the LSB. When a more complex number like 6 (0110) is passed in the LSB will be 2.

print( lsb(8) ) # prints 8
print( lsb(6) ) # prints 2

You can also nest lambda’s together in Python. The next code snippet finds the sum of two numbers.

sum = lambda x: lambda y: x+y

So, how do you define the value of y when lambda expressions only take in a single argument? Well when you define the values of x and y in the expression, you use two sets of parenthesis. Each parenthesis will pass in the argument into the targeted lambda function.

I wanted to try coding a python program that used a Strassen algorithm to multiply two extremely large matrices. I my first hunch was to use numpy and after a bit of googling I found my way to this SO question.

These were great articles written by Peteris Krumins. I would have never thought that sed would be a turning complete language but it kinda makes sense now. Also check out his post on the busy beaver problem. His scripts are pretty fun to run.

This should be a no brainier for anyone who has been coding for awhile but computer math can be funky at times. To demonstrate how accuracte a floating point number is I opened up my python interperter and ran this mathimatical gem.

>>> float(1/998001)
1.002003004005006e-06

As you can see python gives up pretty quickly calculating this number. Dividing 1 by 998001 will create a decimal sequence of every three digit number in order.

Ok, the decimal length is bigger but it sequence turns to garbage at around the same point where the floating point number gave up. There are a few ways to improve your programming math but the given answer will still be bound by the constants of being computed by a computer.