views:

422

answers:

13

I am trying to work on some problems and algorithms. I know C++ but a friend told me that it would be better if done with Python.As it would be much faster to develop and less time is spent in programming details which does not actually earn anything solution wise.

EDIT 2: I plan to use python-graph lib from Google-codes, Please provide example codes if you have used it.

EDIT 1: faster - less time && less work to code the solution

Thank you all for your help !

+8  A: 

A bit subjective, but I'd vote for python because it has good libraries and abstracts a lot of the low level 'detail' that you'd have to consider when using c++...

John Weldon
it has been only 34 seconds I posted the question, StackOverFLow totally \m/, Thanks John !
Gollum
:) I'm glad I could give you a good experience ;)
John Weldon
+6  A: 

I did all my algorithms work in college in C++ because I knew it.

If I'd had to learn a language at the same time, I would have picked Python most likely.

warren
+6  A: 

I am under the impression that it really depends from what you mean by faster.

Faster to develop: go python. Faster to run: go C++.

However python can use a lot of external C libraries, so the difference in processing time might not be that relevant, depending on the type of implementation.

mac
Hey Mac, Sorry. I meant faster in sense of "development time" - editing it.Thank you for your time.
Gollum
+5  A: 

At my university the 500 students in the "Algorithms and Datastructures" class get to choose the language they want.

Python is by far the most popular choice there, and personally I'm happy I also chose that, even though I already knew C++.

Styggentorsken
+3  A: 

Agree with your friend - use Python and put them within a unit test framework.

I worked for a number of years with scientists who did a lot of their algorithmic work in Python.

The example below shows typical tests (the import statement is for the stuff being tested), with a couple of niceties that might save you some time.

The business with the save and restore of sys.path is so you can have all your tests sitting in a directory adjacent to the src directory, without having to install the source into your main Python modules.

This test script is written so it can be imported into a larger suite of unit tests or simply run with python thisfile.py.

#!/usr/bin/python
"""
VRML Writer tests
"""

import unittest
import os
import sys

if __name__ == '__main__':
    global save_syspath
    save_syspath = sys.path
    sys.path = [os.path.abspath("../src/")] + sys.path

from cgtools.VizValueTools import *

if __name__ == '__main__':
    sys.path = save_syspath  # RESTORE SYS.PATH

# use some common constants to make tests easier
MINV = 0.002
MAXV = 12.789

class TestColdHotColorGeneration(unittest.TestCase):

    def testGeneratesLimitValues(self):
        assert generateColdHotColorValue(MINV, MAXV, MINV) == (0.0, 0.0, 1.0)
        assert generateColdHotColorValue(MINV, MAXV, MAXV) == (1.0, 0.0, 0.0)
        assert generateColdHotColorValue(0, 0, 0) == (1.0, 0.0, 0.0)  # cope with weird case where range is effectively one value, should be always top


    def testGeneratesLimitValuesWithClipping(self):
        assert generateColdHotColorValue(MINV, MAXV, MINV - 1.2) == (0.0, 0.0, 1.0)
        assert generateColdHotColorValue(MINV, MAXV, MAXV + 49) == (1.0, 0.0, 0.0)


    def testGeneratesMiddleValue(self):
        """
        Note to be careful picking values so your value IS in the middle,
        to generate pure green
        """
        assert generateColdHotColorValue(1.0, 3.0, 2.0) == (0.0, 1.0, 0.0)

if __name__ == '__main__':
    # When this module is executed from the command-line, run all its tests
    unittest.main()
Andy Dent
+3  A: 

I would go for python. And if you really need the performance, then you can always write C/C++ extensions and use them in python.

prime_number
+19  A: 

I think you're looking for Python, because you can:

  • Focus on the algorithms themselves and not have to worry about other detail like memory management.
  • Do more with less code
  • The syntax is almost like working with pseudo code.
  • There is great built in language support for lists, tuples, list comprehensions, etc...

But more specifically...

  • If by better you mean speed of development, then chose Python.
  • If by better you mean sheer execution speed, then chose C++.
Brian R. Bondy
+3  A: 

Algorithms are fine in Python (allthough you can only fly one OS scheduled python thread due to the global lock); however, when it comes to data structures + algorithms you need fixed complexity guarantees, and this case you mix Python with C.

I suppose what I have said applies more to long running computations. You can emulate data structures on-top of the python hashmap primitive.

Hassan Syed
thanks Vainstah, I did not know that, will check on this.
Gollum
+1  A: 

I also vote for python. When do algorithm, we tend to work on the algorithm itself rather than language, low level details. Basically, we works on abstraction level. And using python, we're less likely to be side-tracked. But if you're very familiar and comfortable with C++ and can use it freely to express your idea, just use it.

Raymond
+1  A: 

If using C++ means that STL is fair game, I'd say that it deserves serious consideration. STL is a fantastic library, combining structures, iterators, and algorithms. I love the Python recommendations, but if I could use STL I'd reconsider C++.

duffymo
+8  A: 
gotgenes
I checked for C++ libs and found that Boost has a support for graphs. Besides being faster to develop, python gives the flexibility to test the quick code snippets and syntax makes it very easy to do it quickly (no includes, no namespaces etc. ) just write anything and HIT ENTER. I am planning to use "python-graph" from google codes ? any suggestion ?Thanks for your time | was definitely helpful :-)
Gollum
I have a fair bit of experience with NetworkX and gladly recommend it. I agree that the Python interactive interpreter (and offshoots like IPython and bpython) is a really wonderful tool, and I miss when working with compiled languages.
gotgenes
writing your own graph lib may be a requirement, though, in an algorithms or data structures class
warren
@warren I already wrote the caveat, "...unless this is an *academic excercise*..."
gotgenes
A: 

Remember that Python is compiled to bytecode and then interpreted in a VM. So, in performance isn't better (faster) than C++.

konus
+1  A: 

With C++, you'd sometimes be concentrating more on the language issues than the problem itself, so Python. I'd even be recommending you do it in a higher-level language like Matlab (although the language itself can be a bit ugly).

moogs