Wednesday, December 30, 2015

I became a Python zealot!

Some time ago I started solving coding puzzles from codingame webpage. I mentioned it in a previous blog entry. I am doing all the puzzles in Python as a way to learn this language. Currently I have done all easy, all except one medium, half of hard and half of very hard. You can find solutions on my github account.

Thing that I would like to share with you today my dear reader is my findings about Python language.

Let me give you some background:

I learned some basics Pascal in a high school (using the Free Pascal IDE, yes the blue one pictured below!) and created a master mind game (you can admire this marvellous piece of work here).


Then I started my studies - Control Engineering and Robotics and learned C and C++. It was during medieval ages, harsh and dark times without vectors, list and all other shiny things kids play with these days. I prepared my own double-linked list, stack, queue through the pain and hard work.

On a second year, way before it was introduced on lectures I started working with microcontrollers and after short stint using asembler (I still have one of the programs I wrote, check it HERE) I switched to programming them in C. At that times the mighty AVR Studio 4 was considered a state of the art IDE, with no code completion and ugly looking font! All of this skewed me away from the objective C++ and made me favour C.

After the second year of studies I started additional field of studies - Computer Science. There I have learnt that C# is a great way to write programs with windows, buttons and so on. I also got to know collections, generic types and a world without pointers. On the other hand I was introduced to image processing library called OpenCV (during glory days of C api with IPLImage and manual memory managment <yay>). What is funny is that you can still found a lot of advices and code examples related to old OpenCV APIs. During that times I just coded - if there was a task, I did it, did not have any favourite language - I used what was best suited - C for micros, C++ for image processing, C# for windows apps. I did not know about unit tests, refactoring, clean code theories, version control systems (!) and other things. There were just problems that had to be solved. If the problem was small enough I was able to finish it fast, if it was rather large I was suffering because of my unawareness of  proper code management techniques. I drifted far away from the main point that I was planning to make - during all that time I did not find my go to language.

Diving into Python

Last year a friend (thanks a lot Marek!) convinced me to try Python. At the beginning I was sceptical as I thought that the same thing can be achieved with C# or C++ with heavy usage of standard library (especially with new things from C++11). Boy I was wrong! During this year I wrote some programs with Python:
and solutions for codingame puzzles:

After the last one especially I came to conclusion that Python is a great language to start your programming journey. This is somehow important to me as I am teaching programming at the Poznan University of Technology. Earlier, I was teaching students how to solve problems using C/C++ programming language and I did it with passion. Nowadays I am doubting if it is the best way and it is because of Python. I tried to convince my colleagues about Python superiority but they are stubborn so I will try to convince YOU, the reader. Try solving following problems in C++, while I will do them in Python and we will compare the solutions in the instalment of this series (I sent the questions to a few friends and if they accept I will post their solutions too). Here we go:

* you can use any collection you find appropriate for the task (vector / list / other). Same goes for strings / arrays of chars.

1) lets assume we have to collections and would like to print values in the following format:
value_0_from_first_collection, value_0_from_second_collection
value_1_from_first_collection, value_1_from_second_collection
etc

2) lets assume we have a text and we would like to print it without the first and the last sign. In addition print text length.

3) we have a collection of values and we would like to prit it without the first and the last value. In addition print collection size.

4) we have a function that has to return:
a) two values,
b) three values.
How would you implement that?

5) for collection of your choice do:
a) print all values,
b) increase each value by 10,
c) remove last element,
d) remove n-th element,
e) print value alongside its index,
f) check if value x is in collection,
g) check if all values are smaller than y.

6) having a text count how many letters are lower-case and how many are upper-case

7) is there any situation that you do not use indentation?

8) are you using standard arrays (static or dynamic) at all (like: int a[5] or int* b = new int[5])?

TL; DR

My programming history was presented. It is full of entertaining and absorbing adventures (who would contempt some asembler and pascal code!) that you should read carefully. Also Python is the thing right now. At the end I ordered you a homework that you have to do and post the solution in the comment section below!

No comments:

Post a Comment