How can I improve my skill of building algorithms for problem solving? How to make them effective? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I improve my skill of building algorithms for problem solving? How to make them effective?

Needed for solving project Euler questions.

5th Oct 2019, 12:53 PM
Mohamed Ishad
Mohamed Ishad - avatar
7 Answers
+ 3
Before you can make your algorithm/code more performant you need to understand the complexity of your code. That is why you should learn about big O. A good example is a membership test in python: if element in a_list: can be a lot slower than if element in a_set: The list needs to be searched element for element. So in worst case you need to search the whole list. In a set each element is indexed with its hash value and can be accessed directly. In one of my code that simple switch brought down run time from 20 minutes to one minute. You learn about stuff like that when learning about big O.
7th Oct 2019, 6:03 AM
Thoq!
Thoq! - avatar
+ 4
practice is basically the only way to improve 😅 perhaps looking at other peoples codes and understanding the logic maybe helps too 😁 if you're looking for some challenges for you or how people solved them here is a thread full of em 😄: https://www.sololearn.com/discuss/1270852/?ref=app
5th Oct 2019, 1:16 PM
Anton Böhler
Anton Böhler - avatar
+ 3
As ~ swim ~ said, you just need to practice. The secret is the right choice of task: It should be mildly challenging. If you have to research, test, debug, struggle a bit, but in the end you make it, the level is just right. If you always choose these, you'll gradually increase your skill and understanding, just as if you were training a muscle. You can try to do a bit more by looking at other people's solutions for the same task, seeing how they did it more skillfully. Also you can study your own codes from a while ago and look for ways to make them shorter, quicker, easier to read...
5th Oct 2019, 1:21 PM
HonFu
HonFu - avatar
+ 3
The poster of question is struggling with recursions. Everyone in this thread may I please know should I really prefer recursions over loops. Some problems like factorial can be solved by using loop instead of recursion. Recursions basically take place on stack (hope I'm right ) and it's limited compared to heap. So should I use recursions for such problems ?
5th Oct 2019, 2:12 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
Thanks ~swim~ I'm struggling to understand solutions with recursion 😢 It's hard to me
5th Oct 2019, 1:12 PM
Mohamed Ishad
Mohamed Ishad - avatar
+ 2
I prefer to use loops for said reasons. But I wanted to *be able* to write recursive stuff anyway.
5th Oct 2019, 2:23 PM
HonFu
HonFu - avatar
+ 1
I also struggled with recursion a while ago. Solution was the same: Do *very simple things* with recursion - things you normally never would use it for. After a while it just clicks. Search in my code list using the word 'recursive' - you'll find all my attempts. You can tackle them too.
5th Oct 2019, 1:25 PM
HonFu
HonFu - avatar