Solution of Problem #3 - This one is in Python (The Perl code got really confusing because of all the symbols and brackets).