P07 (**) Flatten a nested list structure.
scala> flatten(List(List(1, 1), 2, List(3, List(5, 8)))) res0: List[Any] = List(... more
P06 (*) Find out whether a list is a palindrome.
scala> isPalindrome(List(1, 2, 3, 2, 1)) res0: Boolean = true
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
P05 (*) Reverse a list.
scala> reverse(List(1, 1, 2, 3, 5, 8)) res0: List[Int] = List(8, 5, 3, 2, ... more
P04 (*) Find the number of elements of a list.
scala> length(List(1, 1, 2, 3, 5, 8)) res0: Int = 6
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
P03 (*) Find the Kth element of a list. By convention, the first element in the list is element 0.
scala> nth(2, List(1, 1, 2, 3, 5, 8)) res0: Int = 2
I expect the solution to be trivial using the indexing capabilities of
List types, such as in any language that supports
P02 (*) Find the last but one element of a list.
scala> penultimate(List(1, 1, 2, 3, 5, 8)) res0: Int = 5
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
P01 (*) Find the last element of a list.
scala> last(List(1, 1, 2, 3, 5, 8)) res0: Int = 8
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
Int, but it
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
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
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