99 Scala Problems 06 - Find out whether a list is a palindrome

The problem

P06 (*) Find out whether a list is a palindrome.


scala> isPalindrome(List(1, 2, 3, 2, 1))
res0: Boolean = true

Initial thoughts

The algorithm is pretty simple. A list is palindrome when it is equal to its reversed version. So both a procedural and a recursive solution may get a reversed version of the list and then compare the two.

For long lists this could be a performance issue, so a ... more

99 Scala Problems 04 - Find the number of elements of a list

The problem

P04 (*) Find the number of elements of a list.


scala> length(List(1, 1, 2, 3, 5, 8))
res0: Int = 6

Initial thoughts

This should be straightforward both with object-oriented and functional techniques. There are no special cases except that of an empty list. There is an interesting solution that mixes functional and object-oriented techniques, which is ... more

99 Scala Problems 02 - Find the last but one element of a list

The problem

P02 (*) Find the last but one element of a list.


scala> penultimate(List(1, 1, 2, 3, 5, 8))
res0: Int = 5

Initial thoughts

This problem come in the same form as the first one, so most of the syntax issues have already been solved. The challenge here is to find a good algorithm to extract the penultimate element and, as done for the first problem, it ... more

99 Scala Problems 01 - Find the last element of a list

The problem

P01 (*) Find the last element of a list.


scala> last(List(1, 1, 2, 3, 5, 8))
res0: Int = 8

Initial thoughts

This is the very first Scala problem, but a lot of topics are covered even by such a small task.

First I have to learn how to define functions. Then the problem shows two data types, namely List and Int, but it ... more

Python 3 OOP Notebooks

The Python 3 OOP series of posts that you can find here is now available as a series of IPython Notebooks.

From the official site:

The IPython Notebook is a web-based interactive computational environment where you can combine code execution, text, mathematics, plots and rich media into a single document.

As a matter of fact, IPython Notebook is the perfect environment to teach Python itself. If you want to know more about this wonderful piece of software check the official site

You can find the notebook of each post here

Default arguments in Python

Recently I found a nasty bug in some Python code due to a misuse of default arguments. If you already know everything about default arguments and just want to laugh at the funny error feel free to jump to the end of the article. Alas, the code was written by me, but I'm pretty sure that day I was replaced by an evil clone of myself. You know, it happens sometimes. =)

This post just summarizes the basics about standard and default arguments in Python functions and warns against possible pitfalls in your code. If you are just approaching Python and started writing functions I warmly recommend checking what the official Python manual says about them at the following links: Defining Functions and More on Defining Functions.

A quick review of functions

Python is a strongly object-oriented language, pushing this ... more

Accessing attributes in Python

Python is a language that tries to push the object-oriented paradigm to its maximum. This means that its object model is very powerful compared to that of other languages, but also that the behaviour of Python code may result surprising to new programmers.

In this post I want to review the methods that Python provides to access object attributes, trying to provide a comprehensive overview of the matter to everyone wants to start programming in this beautiful language.

What are attributes

Since the nomenclature may vary from language to language, let me name things. In Python we call attribute everything is contained inside an object. In Python there is no real distinction between plain data and functions, being both objects, so what I will say about attributes is perfectly valid even for methods.

As a working example, in this post I will use the following class. It represents a book with a title and an author. It also provides a get_entry() method which ... more

Advanced use of Python decorators and metaclasses


While introducing people to Python metaclasses I realized that sometimes the big problem of the most powerful Python features is that programmers do not perceive how they may simplify their usual tasks. Therefore, features like metaclasses are considered a fancy but rather unuseful addition to a standard OOP language, instead of a real game changer.

This post wants to show how to use metaclasses and decorators to create a powerful class that can be inherited and customized by easily adding decorated methods.

Metaclasses and decorators: a match made in space

Metaclasses are a complex topic, and most of the times even advanced programmers do not see a wide range of practical uses for them. Chances are that this is the part of Python (or other languages that support metaclasses, like Smalltalk and Ruby) that fits the least the "standard" object-oriented patterns or solutions found in C++ and Java, just to mention two big players.

Indeed metaclasess usually come ... more