T O P

Is it possible (or a good idea) for an intermediate python programmer who has done a little C programming (and understands pointers) to learn Data Structures and Algorithms in C, but implement everything in Python?

Is it possible (or a good idea) for an intermediate python programmer who has done a little C programming (and understands pointers) to learn Data Structures and Algorithms in C, but implement everything in Python?

Crims0nCr0w

Please do keep in mind that you probably wouldn't be writing proper Python. Writing Python like C code is generally frowned upon and the language is optimized behind the scenes for the usage of specific Python idioms. Example: my_list = [10, 20, 30] other_list = [1, 2, 3] for i in range(len(my_list)): # Bad print(my_list[i] + other_list[i]) for x, y in zip(my_list, other_list): # Good (no usage of indexes) print(x + y) Here's a link with interesting [Python anti-patterns](https://docs.quantifiedcode.com/python-anti-patterns/). Some of them are about writing Python in a C-like way.


BrilliantScarcity354

I am going to wager more people find this confusing than a useful way to iterate given the nature of the sub, I’m all for conventions but the actual process is more obfuscated in this case, hence less understandable Just my thought and I appreciate you sharing the anti patterns, I will put many to good use - thank you


redfacedquark

>I appreciate you sharing the anti patterns, I will put many to good use - thank you You mean you'll not put them to bad use?


BrilliantScarcity354

Touché


Crims0nCr0w

I understand. For future reference, [this anti-pattern](https://docs.quantifiedcode.com/python-anti-patterns/readability/not_using_zip_to_iterate_over_a_pair_of_lists.html) is the one referred to in my example. The simple rationale behind a lot of them is often “There should be one– and preferably only one –obvious way to do it.” from the [Zen of Python](https://www.python.org/dev/peps/pep-0020/).


BrilliantScarcity354

Nono I saw the one you were referencing, I’m saying that sure it’s “the only way” but for a beginner learning, zip(list1, list2) doesn’t really “tell” you what the program is doing whereas for i in range(len(list)): print(list[i]]) Makes it clear you’re printing the ith element of the list Pardon the formatting, on mobile


INeedHelpFromYourMom

I think this is usually the case for beginners in the language but as you use it more and more, using functions like zip and enumerate become second nature and are easily understandable, especially in this context. In my opinion, as people learn Python, it’s important from the get go to use the right conventions instead of learning those later on. Why not build a solid foundation so that you don’t have to revisit it later? Just my two cents.


BrilliantScarcity354

I completely agree, more just giving a reason why someone wouldn’t want to state a convention without and explanation on a learning subreddit


33498fff

I understood the iteration, but not the zip as I am not that far along in learning Python. Without the description I would have had to look it up somewhere else.


O_X_E_Y

That's a great page, I learned a lot just now!


vardonir

thanks so much for that link! i was initially taught how to program using C++, so i still have a tendency to do range(len(my_list)) instead of, say, enumerate(my_list)


ManvilleJ

looks like they need to update to f-strings. Super cool resource


Caligatio

In general, it's always a good idea to have a solid understanding of computer science fundamentals like data structures and algorithms (DSA). You should definitely take a DSA if you haven't done so. Where I'll break from the rest of the commenters is that I believe you should take it in C. I say this for two main reasons: * Having a solid background in C helps you in so many ways. Is Python doing something bizarre? It may be buried in the interpreter that itself is written in C (assuming you're using the normal CPython). Want to get into reverse engineering? Best case scenario is your tooling spits out decompiled C code. Ever want to get into OS-level programming? All that is C/C++ (Rust is making an appearance but still a ways off). * A programmer that only knows one language is far less useful than a programmer polygot. IMO, it's best to get a good understanding of multiple languages early in your career as it makes learning more languages easier.


Encomiast

I think it's a **great** idea. Most of the main ideas about algorithms are agnostic to the language — Dijkstra's algorithm is Dijkstra's algorithm whether you code it in c, Python, Lisp and thinking about the best way to implement it in your chosen language is *really* great practice. If you look at Donald Knuth's *Art of Computer Programming* you will notice his code is all in a hope-spun version of assembly language that nobody uses. It doesn't matter. It will make you better at both Python and algorithms. Just don't get discouraged that your amazing merge sort isn't faster than the native python sort — that's not why you're doing this :). (p.s. I would also recommend the MIT open courseware algorithm lectures with Erik Demaine. They are legendary.


Bobbias

Yes, you're right on the money. I've been randomly moving between languages for 20 years, and I've come to realize just how useful it is to be able to separate the programming language from the structures and algorithms. Currently in the process of wrapping my head around Haskell. Despite the slow progress, it's definitely helped me look at things differently.


Kenkron

I disagree. It's true that algorithms can be made in any language, but if you're learning data structures, python will cripple you. Dijkstra's algorithm in Python is fine, but Python will handle nodes with variable numbers of connections, and sorting the edge lengths, and growing/shrinking the open list for you. You won't need to think about it.


Encomiast

Fair, but isn't this true at whatever level you choose until you get down to machine code?. Does c cripple you because you don't need to think about registers and CPU instructions? Does assembly cripple you because it's too easy to forget that there a binary numbers under the hood? Some might say yes. As I mentioned, Knuth uses an assembly language to explain his ideas because he thinks you need to the low-level understanding. Who's going to argue with *him*? I suppose at a certain level the argument is similar to those who say you should learn to drive with a stick-shift (my dad used to say that). It's not really wrong, just like it's not wrong that Python hides a lot of implementation (for example, not understanding that list.append() isn't free). But the opposite argument is that this allows you to focus on the actual *idea* behind the algorithm while also upping your Python game. My main argument is that this makes you a better Python programmer, not necessarily a better computer scientist. Figuring out how to translate algorithms into idiomatic Python code is a terrific exercise. I'm totally on-board with caveats, but I still don't think that it's a bad idea.


Kenkron

I'm mostly worried about python because he specifically mentioned data structures. Memory management is one of the biggest reasons data structures matter, and it's something you don't have to worry about in python. You're definitely right about the algorithm being easier to grasp, and I wasn't trying to diss a Dijkstra's written in python. It's that there are a lot of little bits of data structure behind the algorithm that you might miss. To borrow the automatic transmission analogy, it looks like op wants to learn manual.


PM_Me_Python3_Tips

If you need resources that aren't in C but in Python instead, there is: Runestone Academy - [Problem Solving with Algorithms and Data Structures using Python - By Brad Miller and David Ranum](https://runestone.academy/runestone/books/published/pythonds3/index.html) -- [Grokking Algorithms: An illustrated guide for programmers and other curious people - By Aditya Bhargava](https://www.manning.com/books/grokking-algorithms)


turingparade

NOTE: I am not really a Python programmer, so citation is needed. This is my assumptions based on what I know about Python and C. (I am a C/C++ programmer) Ignore if you don't want a non-python programmer lecturing you. ​ Yes it is possible and it *might* be a good idea. Structures and algorithms are translatable to any programming language. However, Python is slow. It was made to be a high level, interpreted language. This means that it's taken certain liberties to function in a way that's easy to use and easy to do complex work in. ​ First, ask yourself if the data-structure is already in Python. Most of the lower level stuff in Python has already been implemented through C. That means that Python's data structures and algorithms will **always** be faster than your own data structures and algorithms programmed in Python. That is simply because Python's data structures and algorithms are programmed in C. Second, see if there's a library that already implements the data structure for you. Why do it yourself if you don't need to? If you can't do either of those things, then yeah, it's probably a good idea. Though do note that your program will run slower than if the data structure/algorithm was implemented in C and then exposed to Python. ​ TL;DR: It is possible, but only do so if you need to. Try to use something already created.


BigDog1920

I know it's not efficient to implement it from scratch, but I'm trying to learn DSA, not simply crank something out. I'm trying to learn not put something into production.


turingparade

Then yes, it's both possible and a good idea. Python is the most commonly used language for learning data structures and algorithms (afaik).


spez_edits_thedonald

learning data structures and algorithms in C is a good idea applying that knowledge in python is a good idea then, learning to write pythonic code is a good idea Harvard's famous CS50 course is free through EdX, and is a good move, see also r/CS50


mfb1274

All around will probably be good. You’ll need to understand not only the basic ideas of the algos, but also highlight the differences of the syntax and nuances of both languages. Assuming you’re truly intermediate and have a good grasp on C and programming in general, then it honestly shouldn’t even be too hard either. Probably just teach you syntax/speed/ease-of-use comparatively.


dvali

You can do it but you'll be fighting the language sometimes. Just do it in C. It probably seems scary but it will be a valuable skill and will make you a better programmer in general. You'll learn a lot about Python just as a result of seeing what C does differently.


Kenkron

This is the right answer. One of the most important parts of learning data structures is learning memory management. This is normal in C, but Python tries to abstract away memory management as much as possible.


Electrical_Ingenuity

Agree. C isn't scary. It's a pretty simple language, that will teach you much about how a computer works, sitting one step above assembly language. It will also quickly teach you why higher level languages exist, as it is hard to build stuff from scratch and do it correctly.


Single_Bookkeeper_11

It is not a bad idea to learn, but never ever use such code in production. Python written this way is much slower, all around worse and more error prone.


Morlaco13

Human mind is strange..


Kenkron

I'll have to say no to this one. python already does so much for your behind the scenes that it would be hard to make sense of low level stuff. Dynamic arrays are a structure that acts as a variable length list, but is built out of arrays that can't change size. It's awesome. In python, there is no array. All lists are already dynamic. In c you could get this "aha" moment, when you see how something simple is used to build something that is more complicated, but keeps good performance. In python, you can't see the beauty of what you've made because it was already there to begin with.


DigBick2199

So I completed cs50 course recently, which starts off in C and moves on to python. Python week's homeworks were just to rewrite what you submitted during C weeks. I can tell you that I actually had more trouble writing same things in python then C. As a newbie and inexperienced dev, I would suggest mainly excercising what you want to become better at, because yea, Fundamentals are the same (mostly), but if you want to specialise in one thing, do that one much more.


RobinsonDickinson

I’d use C++ instead of either of those languages.


Magnus0re

Hello friend, I'm here to warn all future programmers that C++ is a horrible language ;).


captain_awesomesauce

Doing algorithms and data structures in Python, in my opinion, makes it easier to learn the concepts because python makes it so simple. Doing it in c or c++ will probably give you a better understanding of what languages like python do unthe the covers for you. Both have their benefits.


Magnus0re

I do not think it is a good idea. A project is a good idea, but in general the good things in C are not good things in Python. And besides, no pointers in Python. Please by all means do some projects in Python, you'll have lots of fun. But don't do C pointer stuff. If you know oop pretty well it's time for a new paradigm: Functional programming. Look it up, it's fun and solves many of the problems of oop programs, and if nothing else will learn you to think with a broader, more full, perspective. Python is not the ideal language for this, but most of it, if not all of FP can be done.


BigDog1920

Shouldn't I learn Data Structures and Algorithms though? Or should I learn FP beforehand?


Electrical_Ingenuity

It's not an either/or item. You need to understand algorithms and data structures, along with big O notation, along with a practical respect for the tradeoffs of memory, CPU, and I/O. You also need to understand different programming paradigms, including functional, object oriented, etc. There have long been functional programming zealots going back to the early days of Lisp. The reality is that real life is more pragmatic, and you'll use the best aspects of all paradigms to solve your problem at hand.


BigDog1920

I know I need to learn alot of things, I'm asking for an order in which to learn them. Surely they build on each other?


Electrical_Ingenuity

Algorithms and data structures first. C is a fine way to do this.


Magnus0re

Good answer!


Magnus0re

My personal opinion is that [SICP](https://mitpress.mit.edu/sites/default/files/sicp/index.html) is just as well if not more worthy of your time. For data structures, once you learn what a linked list, deque and a tree is you have pretty much all the knowledge needed for most practical applications. Lists in python are singly linked lists if I remember correctly, but the docs do not say (I just checked), so I guess we don't care that much about data structures in Python. Now if you really want to take a deep dive into Data structures and algorithms, I won't stop you, there are lots of really interesting, and even new developments in that field. (for example the Bagwell HAMT, which is quite new, but used quite widely) But that knowledge is not as useful as FP knowledge because the scope of it is comparatively small. When we program we call the \`sort\` function on our data, we don't care that its QuickSort or anything else, just that it's fast. (we know that theoretically any comparison based sort has to be at least O(nlogn)) What is more important is how we best communicate the intent of our code. Showing intent means that we all have some common knowledge, in OOP world it's the big four patterns, in FP world there is functions first and common patterns on how to process data and manipulate state (map/filter/reduce ++). Learning a bit about FP will help you communicate better in your code, whilst learning about algorithms and data-structures will help you solve a very specific set of problems very fast (the ones in the book). My opinion is that the better learning path is towards FP because of this 'wider' sense of those concepts. The exception to my arguments is this: If you are making a game/3d/graphics \*engine\* you really, really need data structures and linear (matrix) algebra, and also some algorithms (I'm thinking specifically about A-star for games, pathfinding and the like), but you would not do that in Python. Where you wish to invest your time is of course your business, these are just my 2 cents.