Primitive root mod p

Transire suum pectus mundoque potiri

Functional Programming in Python 26 April, 2008

Filed under: Programming — Nikolas Karalis @ 2:37 pm

Recently, I decided to learn how to use the functional programming tools provided by Python.

Truth is, it’s interestingly strong.

The whole concept consists of using 3 built-in functions (map, filter, reduce), lambda functions and of course the beloved List Comprehensions.

In order of appearance :

map (function, sequence) : It applies function on every item of the sequence.

You can even provide a function which takes two arguments and use it like this :

map (function, seq1, seq2)

filter (function, sequence) : It returns a sequence of the items which have a function (Item) == True value.

reduce (function, sequence) : It applies the function (2 arguments) to the first two items of the sequence, and then to the result and the third etc…

List comprehensions :

This a powerful feature of Python.

I will explain it by an example :

>>> a=range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> [x**3 for x in a]
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
Lambda functions :

With lambda functions you can create anonymous functions on the fly.

For example, the normal definition of a function is :

def f(n): return n**3

With lambda functions :

f = lambda n: n**3

In both cases, we use like this :

>>> f(5)
An example of using lambda functions with the map function would be like this :

>>> a=range(10)

>>> map(lambda x: x**3, a)
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729

This is exactly the same as the example in list comprehensions.

So, with all the above we have a whole new range of tools for dealing with lists.


Taking some time off 25 April, 2008

Filed under: /dev/random — Nikolas Karalis @ 12:13 pm

I’ve been away at countryside for a couple of weeks, trying to refill my batteries and refreshing my interests.

Some gardening, some cooking and a lot of beer and video games! 😛

I would post some beautiful pictures, but sadly I lost my mobile phone and my went with it.

I continued solving Project Euler problems reaching 22% and also started solving Python Challenge.

You should check these two out, since you can exercise your programming and mathematical skills while learning a bunch of really useful tricks and techniques.

Btw, Facebook introduced a nice Instant Messaging feature, kinda mimicking  Google Talk in the implementation. The first impression is good, however I would like to see more features included and also a wider range of (aesthetically better) emoticons.



Bureaucracy 13 April, 2008

Filed under: /dev/random — Nikolas Karalis @ 8:36 pm

I’ve been silent for a while, spending my time doing… mostly nothing.

Spring came and took me with it… 🙂 It’s like my unofficial spring break.

The title comes from my little adventure of finding a list of the courses offered by my school in English.

Oh yes! In year 2008, the primary technological institute of Greece doesn’t offer course list and other material in English. So, after 2 weeks of discussions with the , I was able to obtain a  hardcopy of such a list, which I am supposed to rewrite into a document and get the appropriate signatures.

Anyways, I thought to scan it and offer it here, in case anyone ever needs it (come on people, grad school application are coming soon).

So you can get it from here.