Friday, June 29, 2012

It is Hard to Bring A Rusted Motor to Life

The last time I did any coding was in early nineties, so it has been almost two decades since I practiced my skills. As you might imagine, stopping for so long does no good to one's skills. I have forgotten most tricks and shortcuts that used to employ then to get to achieve my objectives. Plus the world has moved on. I know it will take some time before I remember any of it but it is still embarrassing when someone else tells you that this is another, easier way to do it. Even if no one else is watching.

I finished the Unit 1 of the course last week. I (re)learned the basic ideas of programming and learned about some string manipulation commands leading to how to find the position of a string within another string starting from a predefined position in the original string.

Arithmetic Expressions

addition<Number> + <Number> ⇒ <Number> 
multiplication<Number> * <Number> ⇒ <Number> subtraction<Number> - <Number> ⇒ <Number> division<Number> / <Number> ⇒ <Number> 

Also, if at least one of the numbers in the expression is a non-integer the answer is also non-integer.

Variables and Assignment

Names

Assignment Statement: <Name> = <Expression>
Strings
     Indexing Strings: <String>[<Number>] ⇒ <String>
     String extraction: <String>[<Start Number>:<Stop Number>] ⇒ <String>
     Find: <Search String>.find(<Target String>) ⇒ <Number> 





At the end of the unit there were a few homework questions. Most of them were pretty routine stuff but the last one stumped me completely. It went something like this:


Given any non-integer number (3.1415, 6.7619, etc.) how would you get a number rounded to the nearest integer. So, starting from 3.1415 will print out a "3", starting from 6.7619 will print out a "7".


One additional function not covered in the unit that the question allowed us to use was: str(x), where the function "str" converts a number "x" into a string, so that the output still looks like the original number but you cannot perform mathematical operations on it but you can now perform string operations like finding and extracting. My first attempt was:
     x=6.7619
     str_x=str(x)
     print str_x[:str_x.find('.')]
but this, of course, would only print the first digit of the number in a text form, without rounding it first to the nearest integer. It worked fine for numbers that did not need to be rounded up, but not for the other numbers.



I thought hard and long but could not find the answer, so I cheated and used the [not allowed] round(x) function thus:
     x=6.7619
     str_x=str(round(x))
     print str_x[:str_x.find('.')]


This gave me the answer but I felt bad at not being able to resolve the question. The answer video, of course, resolved in such a simple way that the only thing I could do afterwards was - a facepalm! I only need to add 0.5 to the initial number to get the answer.

     x=6.7619
     str_x=str(x+0.5)
     print str_x[:str_x.find('.')]

Wednesday, June 27, 2012

Python Installer for your Computer

If you want to learn to code in Python then it is best if you get an implementation installed on your computer. Use the version 2.7.3 found here. In my case in got the Python 2.7.3 Windows X86-64 Installer for my computer running Win8.

Python logo
Once you have installed it, open in up and go to the IDLE Preferences item in the Options menu. Click on the General tab and then choose Open Edit Window in the Startup Preferences section. This will make sure that the next time you open Python you get the Editor instead of the command line shell interface.

The advantage of the shell interface is that each time you enter a line of code you get an immediate feedback on any errors you might have made in that line. The drawback - and it is a huge one for me - is that your code is not saved in any file so you cannot reuse them the next time. In the Editor interface, on the other hand, you write the complete code, save it as a file and then run the complete code. The editor will open up the shell, which is where the code is run, and display the final results. If there is any errors, you just switch back to the editor, make your corrections and run your code again.

Easy peasy.

Tuesday, June 26, 2012

Embarking On The Adventure

Every one knows that the coolest thing that kids can learn now is how to code to make even cooler things. If kids don't learn to code now they'd be like those old timers that used to shun learning the newfangled computer things in the eighties. "I've been doing very well for the last sixty years without compooters, thank you very much. I have no need for them!", they'd say. See what kind of jobs they's find now without knowing anything about using computers?! Coding, as I see it, is on a similar path today. Pretty soon all kids will be learning to code just like they learn any other language.

So, thinking this, I have been trying to get my son to take an interest in learning some sort of programming language. (I would have my daughter learn it too but she is more interested in arts - painting, sketching, dance, etc). The other day I found a website called Udacity, founded by Sebastian Thrun, Stanford Robotics research professor and the guy responsible for Google's self-driving cars. Udacity offers a beginners Python class, and I got both Sebastian, my son, and myself registered to take this class.

The class is self paced, with plenty of exercises and examples to make you feel comfortable. At the end of the class, which should take a not-too-hurried seven to eight weeks, Sebastian and I will end up with a home made search engine.

Who knows - we might give even Google a run for their money! :)