Python – Preferring Python over C for Algorithmic Programming

algorithmscprogramming practicespythonself-improvement

I've been studying a bit of algorithms and have been looking at sites like SPOJ.pl TopCoder etc. I've seen that programmers prefer C or C++ usually for most algorithmic programming contests.

Now I've been having some trouble lately. I know both a bit of C and Python and when trying to write a code I seem to prefer Python over C for most algorithms. Everytime I sit down to write a code in C I give up after about 15 minutes because I find it too cumbersome and tend to move over to python. Passing matrices Pointers and so on seem to be useless time wasted that I could actually be utilizing to think about the algorithm itself.

Now I know and have heard from a lot of people that C is a very important language and is the bread and butter of a lot of programmers out there.

What I wanted to know was whether this approach of mine has any drawbacks/consequences/Disadvantages etc.

This is not a Python vs C debate; This a question about how this specific practice of preferring python over C because of the ease of use will affect me or any other programmer/computer Scientist in the long run.


I'd love to hear from people who've used these languages in the industry/and or to develop large software/libraries etc.

Best Answer

In my experience, when people have excess difficulty coding algorithms in C, it's often because they are tightly coupling their data structure management with their algorithm instead of creating appropriate abstractions. For example, manually manipulating linked list pointers everywhere instead of making push() and pop() functions. They are too accustomed to having those abstractions provided to them.

While this problem is much more evident with lower level abstractions, failure to recognize tight coupling and create appropriate abstractions is a problem at any level. Practicing these skills in C until you can make an algorithm that looks clean and readable will carry over to any language you use.

The other problem I occasionally see among python programmers is difficulty adapting for performance at scale. Granted, performance isn't usually the primary concern, but the most pythonic way to implement an algorithm for a relatively small data structure can grind your system to a halt when you're working with gigabytes or more of data. Becoming a good C programmer helps you be more aware of those kinds of issues in any language.

Can you learn those skills in other languages? Sure, but C helps by making it a lot more obvious when you get it wrong.

That being said, I use python for algorithmic programming when I have a choice, even though I'm just as comfortable in C. Python has language features that make it very nice for that kind of programming, and performance differences are usually negligible. I can't speak to why other programmers who know both would choose C. I imagine a lot of them do it simply to set themselves apart from the crowd.

Related Topic