How to make better (simple) algorithms?

Dear friends,

I am a musician, changing career to IT.
I’ve been studding Python for 10 months. It is my first programming language and it is also the first (and only) thing I am studding until now.

I am solving exercism (about 70 Python exercises). In general, it takes lots of time for me to find solutions. And that’s ok!
But my code is usually made with too many lines and now I am asking myself the following question:

“How to develop (improve) the algorithm inside my mind, before starting to solve the exercises”?

  1. Sometimes, I think maybe better algorithms only come after lots and lots of practice.
  2. Sometimes, I am afraid when I ask myself: is logic something we are really able to improve?
  3. Sometimes, I think that after finding my own solution, I should learn from community solutions, but most of the time the best solutions are unreachable to me (I just can’t understand the logic or even the code from other users). Should I invest (spend) lots and lots of time on this or should I move on to the next exercise and hope someday I will come back and improve my own algorithms from this exercises?

So, having in mind these 3 items, could you please give me some advise?

Thank you so much,

Rod.

  1. Practice helps :slight_smile:
  2. Algorithm design is a skill. Like any skill, it requires practice and gets better with practice.
  3. You gotta learn from somewhere. A lot of nifty algorithm ideas aren’t something that each programmer came up with on their own but is something they were shown, read about or learned in a class. People will be like, “Oh, yeah. I used Dijkstra to solve that!” Those people didn’t come up with that algorithm themselves; they were shown it and shown a couple of places it applied. Over time, you build a collection of tricks, tips, approaches and algorithms. Over time, you learn patterns and get better at recognizing them. Over time, you get better at mapping patterns to algorithms.

I’d strongly encourage you to study and work out how solutions work especially when they seem over your head. If you have a mentor on a solution, do ask them to help you out!

  1. It’s ok to start small. Write an imperfect solution that solves the problem. Then think about what you can improve.
  2. Awake your inner librarian or Googler. For many common problems and algorithms you will find amazing explanations in books or on the web (as blog posts, in forums, or as videos.)
  3. Read other people’s solutions. Often even experienced programmers will learn something new. Sure, not all solutions are good or understandable. But some are and you can learn a lot.
  4. Ask someone with experience, they might give you hints, explanations, or pointers to helpful resources. Exercism has amazing mentors, tap that well.
  5. Practice makes perfect. I’m sure that you as a musician had to spend many hours training. That’s the same with programming. With time you’ll be able to recognize familiar aspects in problems, and apply known concepts and patterns.

I’ll agree with everything @IsaacG and @siebenschlaefer say.

I will add emphasis to mentoring: use the code review for every exercise. Let a mentor give you tips for improvement. This should accelerate your learning.

Go back to previously-solved exercises to practice new concepts that you’ve learned.

After you feel comfortable with your python fluency, which may take a long time, pick a different track and learn another new language. Then learn more languages. Each one will be easier than the last.

Learning to program is quite similar to learning to speak or write:

  • Many/most experienced programmers do not remember their initial learning process.
  • Becoming good at it takes forever.
  • Picking up your first language is super hard; thereafter it becomes easier with every additional language (though depending on similarity).
  • Learning new languages improves ability in prior languages.
  • Ability/eloquence depends a lot on how much one has heard/read/seen.

That said, on to answers to quotes.

How many lines is too many, and how do you know? (Beware self-judgements!)

I can think of two causes of high line counts:

  • Ages ago, the Elders produced code just like yours. After a while they noticed repeating patterns, which they encoded in useful language constructs. Having not yet noticed these repeating patterns yourself nor having learned to use these constructs, you are not yet using these constructs.
  • Your thinking is cluttered. (I should probably add: this is entirely normal!)

The first one can be remedied by reading and understanding others’ code.

The second one… I dunno. The only think I know for certain is that it improves with time. Probably reading code and thinking about problems in the shower will help, but I do not know how much, relative to each other.

(You might think that a mathematician might have something to say about how learning to think logically / in a structured manner works. Alas, I haven’t really.)

You cannot practice what you cannot already do.

(Case in point: try practicing writing in Dutch right now.)

In my experience, ‘better algorithms’ come primarily after lots of reading + understanding. ‘Practice’ has much less influence.

Remarkably (and reassuringly): yes, this is possible. It happens all the time, in fact. I have observed it in myself and in others, and have no doubt that at some point in the future you will look back and find that you, too, have improved markedly.

I recommend asking for clarification. As a mentor I have clarified community solutions for students; it isn’t unusual. Doing this is valuable.

No need for hope: you will come back and improve your own code. That’s just what happens.


Notice: all four of Isaac, Sieben, Glenn, and I emphasize input, i.e. reading and understanding.

Make sure that you make coding an enjoyable activity. Work at your own pace and don’t stress yourself too much. Programming is very hard for most beginners. It takes a lot of time to learn how to structure the code well and write efficient programs.

Read a book that explains how Python works and how specific functions, classes, data structures etc. are used. You should also read a book on basic algorithms, preferably written in Python. Don’t expect to figure all these things out by yourself.

Reading other solutions is great to a certain extend but the problem is that beginners often pick bad habits because they don’t really know what they should be looking for. Is the solution well structured? Is it optimized for speed? Is it optimized for readability? What’s the used algorithm? Would a different algorithm be more suitable? Make sure you think about all these when you are writing your own solution too, but don’t try to over-optimize.

Besides making coding fun, the other very important thing i always emphasize is that the code should almost always be optimized for readability first. A well written code means you’ve made an effort to think how your program should be structured. It also helps you and other people read the code and improve it if needed. It’s a habit that pays off in the long run. Do not underestimate it. Find solutions that are well written and study them.

An extra suggestion: As you’ve learnt much about composition by studying other composers’ music, learn about algorithm design by reading other programmers’ code. The place I learned a lot from is RosettaCode. RosettaCode sets challenges and encourages programmers to solve them in their preferred language. Seeing as your focus is Python, check out the Python Category, select a challenge and then see how another python programmer solved it. On the same page you’ll see how other programmers solved the same challenge but in different languages. This is helpful because there’s usually more than one way to solve most things.

Thank you so much, @IsaacG , @MatthijsBlom , @glennj , @siebenschlaefer , @axtens and @tasx for all the precious words you have just written here. Thank you for sharing your so valuable experience with begginers like me myself.
Everything you wrote makes sense to me now and I will put all of this hard work in action!

Thank you all so much,

Rod.
PS: As your answers are also so motivational, I will read them again and again before (and maybe after) my study hours! :slightly_smiling_face:

1 Like