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('.')]

No comments:

Post a Comment