Freshly written on my new blog, a comparison between C and Python for a small application. The code speaks for itself about what someone’s choice should be in a given scenario.
Tag Archives: C++
New blog: Refactoring a C Program
Here is a completely new and fully working (out of what I have implemented) blog entry. I am liking this setting a lot; might eventually transform my whole blog to this new setting, built on Twitter’s Bootstrap (CSS and JS), Picons free icons, google-code-prettify and my own stuff.
Currently the promised post on how to refactor a C program is up. Happy refactoring!
The deque class in C++
Going through some really valuable and practical C++ issues from this resource from Stanford, I came across the deque STL class in C++. Seems pretty interesting to me. Although there are performance issues to take into consideration, seeing as, unlike the vector class which ensures that all elements come from contiguous blocks of memory, the deque implementation is a little different in that the elements might be spread out across different pages in the memory. However, it does allow for pushing and popping from both ends of the queue (hence the name deque – double ended queue), and in certain situations when the queue is going to be small and you are frequently going to be pushing and popping at both ends, deque is a really invaluable resource to have in your toolkit. This location very nicely and thoroughly differentiates deques from vectors. One of the exercises in the course reader was a ring buffer, which I implemented and posted here – really straightforward and simple to implement.
Interesting differences between Python and C++
This is not a post about praising or bashing either language, but rather an objective account of the differences I discovered myself while working through the Invent With Python book. These seem interesting to me, so here goes:
[1] This is still a bit fuzzy to me, but in Python, variables are in scope even outside the loops in which they are first instantiated. True, we do not need to declare variables in Python, as opposed to C++, but intuitively I would still have thought that a variable won’t be visible or retain its value outside the loop it is first used in. However, as the screenshot shows, Python recognizes the variable r outside the for loop in the function and returns it. C++ however, throws a compile time error when it sees the code in the corresponding screenshot.
[2] In Python a function may accept an argument of any type, and return a value of any type, without any kind of declaration beforehand. For example, in the screenshot, you see the function might accept a boolean and return a boolean, or it might accept an integer and return a string or vice versa. Even though Python is strongly typed in a sense that you may not add a string and an integer without an explicit conversion. For example in Perl, something like this is valid:
my $a = “A”;
my $b = 3;
my $c = $a.$b;
print $c.”\n”;
And it produces the result A3. However, you may not do the following in Python:
>>> a = ‘a’
>>> b = 3
>>> c = a + b
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: cannot concatenate ‘str’ and ‘int’ objects
Yet this kind of flexibility that Python provides in calling functions and returning values makes our code incredibly simple and much more maintainable. Perl provides something like that too but Python looks cleaner, is object oriented, and still maintains a little strictness about types. Again, this post is not about bashing a language or promoting another.
[3] The division of two integers in Python is always a float (in Python 3; or in Python 2 if you import division from __future__), as opposed to C++ where you need to do some casting to make that happen.
[4] The round function in Python always rounds intelligently – round(4.3) is 4 and round(4.9) is 5, and it even works for negative numbers. In C++ there’s no round function; there’s floor and ceil, but you need to know beforehand which one to use.