Sunday, January 29, 2012

Python from scratch- Part 2


Well, I had crazy week.
My first post- Python from scratch was a huge hit, I got all this attention and even now few days later I still get visits and comments and being followed by Email and RSS.
I have read each comment and each referral to a site which led me to many python sites that come to help and found myself in heaven for newbie like me.
I also met this guy from Costa-Rica who is going through the same journey more or less like me and we are exchanging ideas by email.
My post was published on some great sites and if you search in Google “python from scratch” I am on top5.
All the above made me lose focus and on top of it my young son got sick (still) so I had some sleepless night lately.

Last night I decide that no matter what I will continue on my journey and I have started to read the next page in Google python class- lists.
So I read the entire chapter which teaches how lists work in Python, a bit tricky in some points yet easy to follow.
Then the chapter teach how to use the FOR var IN list which is different from C++, it took me some time to get used to it but once you get it- wow!
Towards the end you get familiar with some lists method which will come handy later on and voila- the exercise.

So here I am solving the tests and the first 2 problems I was doing fine only to bump into the third problem which took me around an hour, here it is:

"Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple." example- (1, 3), (3, 2), (2, 1) should return- (2, 1), (3, 2), (1, 3).
Easy to see the obstacle over here is how to sort using the last element, I really tried to make my code as simple as possible (KISS) but no matter what I did I was writing more and more rows and more and more taking care of corner cases until I decide that I am doing something wrong and surely there is a faster solution hidden in the learning subject.

So I read the lists page course again until I discovered the problem- Google has a Doc BUG in this page- an important method is mentioned but not explained. It says: 
list.sort() -- sorts the list in place (does not return it).
(The sorted() function shown below is preferred.)
But there is no sorted() below and this method was the method to solve the issue easily, in fact also list.sort() can handle this case but I couldn’t understand it from reading over here.

After spending too much time on this and being so late at night I went to sleep, the night follow I spent another 30 minutes to solve the advanced exercise and again I met into strange situation- The second exercise was very easy to do and took me only 3 rows to complete, yet Google solution was about 10 rows (not making sense, haa?)
So I decide to show it to you and ask you where am I wrong with my solution:

The question is-
"Given two lists sorted in increasing order, create and return a merged list of all the elements in sorted order." example- ['aa', 'xx', 'zz'], ['bb', 'cc']) should return- ['aa', 'bb', 'cc', 'xx', 'zz'])


My 3 rows solution was-



Google 10 rows solution is-



I was running several lists to test my code and it works just fine.
This also goes fine with The Zen of Python “Simple is better than complex”
So I am asking you the Python people, do you see the reason why my code is only three rows of code while Google’s is much bigger? Do I have a bug? Can you do it better?

Anyway, that's it for this subject and I am all fired-up for the next one as i am continues in my journey into python from scratch.




## Press here for part 3- The journey continues

Monday, January 23, 2012

Python from scratch


I work 14 years in the High-Tech industry as QA doing manual testing in a manager position.
After so many years I reached to a point where I want to do something else, to write some code like I planned for myself in the past but somehow life led me to a different path in my career.

Last time I wrote some code was at university about 10 years ago and since then I only maintained some scripts mainly Perl and Basic.
So I start reading some Hacker-News posts and Job listening ads to see which language is needed today, what will be easy for me to re-start my code skill after so many years out?
Finally I read this post by eddy chan and found myself fit into this story and decide that I will learn Python.

I hope my journey will inspire others like me to do the same.
I hope my journey will eventually make me controlling the language.
I hope this journey will create for me new opportunities to come.

So I clicked the first link that leads to Google's Python class and here it is my journey into python from scratch using Google Class:

The first tutorial page is of course the Landing Page- Doesn't need any skill yet, takes few minutes to read.
Second lesson, Python Setup- Took me around 2 hours since I thought to run it on a Virtual-Box setup but somewhere in the middle I realized it won't be comfortable to run notepad++ and command-line on one screen so I better use my 2 wide screens on my desktop. 
Installing Python was easy and fast and I was ready my next challenge.

Third lesson, Introduction- I Read about Python Interpreter (Nice feature) and did some debug like tricks, also read some notes about Python compare to other languages.     
Later I did the first examples from this page and found myself thinking how easy it is compare to my c++ studies in U of TLV (OpenU). 
My first real obstacle was understanding the boilerplate __name__  ==  '__main__' (the truth is that I still not really understand why it is so complicate but I am a good student and do what I was asked to do).
After reading some forums I understood why and how important is to use indentation correctly and that Google recommends to use <space> for better indentation, I prefer to use <tab> as I see the spaces better and clearer.
Anyway, reading this section and let the info drill into my brain took me some time and effort and it was late at night. I knew that in few hours my 2 baby boys will wake up for a new day that needs my full attention and so I called it the night and went to sleep (finally)

Next day I was more excited than the day before and couldn't wait for the night to come, after work and after putting my 2 boys to sleep I opened the
4th lesson, String page- This is actually the first page where dealing with real code, handling strings and manipulate them. 
Also learnt the if Statement which is very important for any code. 
It took me to learn this page around 3 hours and then I call it the night.

After finished to learn strings I felt ready to do my first exercise- 7 functions which I was asked to write.
I decide to do it early in the morning when I am awake and not tired after long day at work and dealing my children. So I took the exam on Saturday (Shabbat) and to my surprise finished it after 2 hours successfully.

And now I am here, tomorrow probably I will start the next issue to learn- Lists.
So far my impression from Python is that it is easy language for beginners, it is easy language to convert from C++ like I did, and that I find the internet very helpful with questions I had during my studies so far.
Stay tune for more lessons to come of my journey into Python.


30/Jan/2012- Click here for Part 2