I have a question about algorithms. I have completed the basic algorithm section and some of the intermediate, but now I feel very stuck. I have looked at the remaining challenges and I do not know how to solve any of them. I guess my question is two-fold:
Does anyone else find this to be the case? And if so, what do you/did you do to solve the challenges?
How do you improve your thinking to better understand how to implement these algorithms?
For example, I have no idea how to complete the Roman Numeral Conversion. I have read about it on the Internet and even looked at the code, but I still really don’t get it. Then there are challenges where I don’t have any idea how to solve the problem, but once I have it explained to me, it makes sense. So, I can code the solution if I’m given step by step instructions, but I just can’t think of the step by step instructions myself.
My ultimate goal is to build web apps so I know that I need to understand the JS very well, so I don’t want to just copy other people’s code, I want to understand how to come up with the answers myself.
Any help/advice you all have would be greatly appreciated.
What works for me most of the time is treating algorithmic problems as mathematical ones: I take a piece of paper, a pen and start writing down everything I am given, then the goal. From those two things I start brainstorming ideas. Even if they are really bad at first, I eventually get to the right track. When I have got a neat solution on paper (I usually don’t write full lines of code by hand, I prefer human language, like: 'If I get a number that is 1 or 0, I need to return 1, otherwise I need to return the given number times the function with parameter given number - 1" (this one is for factorials)), I go back to my text editor and code the solution. If it succeeds - Hurray! If it does not - I repeat the same process.
Sitting with a piece of paper gets me thinking a lot more, than just trying to come up with a solution on hand.
Every single human being who has ever faced a challenge has felt this way. I break things down into questions I can answer. What do I need to finish this? What do I have? If this were already solved, how could I describe the solution? If I don’t know how to finish this, what do I know how to do?
By doing them. There’s absolutely no other way. Don’t worry so much about doing them “right”, or doing them quickly, or even finishing them. The ultimate goal in doing these challenges is to develop an algorithm for writing algorithms. It’s like learning a language: you can read about it all you want, memorize abstracted grammar rules, and practice pronunciation, but fluency only comes from the experience of using the language.
Focus on what you do know how to do. What if the input were only the numbers 1 - 9? What if the input were 10, 50, or 100? What’s the difference between writing this for the number 9 and the number 19? Forget writing code, how would you describe the problem in plain English to someone who doesn’t know how to program? How would you describe a possible solution?
Remember: There is no losing here. Don’t mistake struggling for failing.
The best thing you can do is sit with a pen and a paper, jolt down the requirements and try and break the problem into smaller problems, try to solve it step by step if the problem is quite mathematically challenging as in Roman Numerals Converter. Everybody goes through this and eventually you will get better trust me, you already completed the basic algorithm scripting so don’t worry you’ve got the goods.Also, i would like to say there’s no harm in reading other people’s code, but its very important that you understand each line of code, i would suggest you fire up chrome dev tools if you are using chrome or firebug if you are using firefox,and debug the code line by line, that will help you understand the code better as it can provide you that visual aid. Hope it helps!
~Happy coding
Yea, like the others said: It’s more than normal to hit those roadblocks. That’s why they’re called challenges, anyway
As a general rule, I find all algorithm problems require the following:
Understand the problem they are trying to solve (i.e. what is the starting point, where do they want to end up).
It’s all about data transformations! All of these challenges want you to manipulate the initial input in some way or various ways, in order to get to the desired outcome. So learning about the many transformations you can do with existing data, will help your mind come up with ideas for solutions.
For example, try to experiment with changing arrays to objects and vica versa, find out what those transformations tend to have in common, you’ll quickly start to see patterns.
Take it step by tiny little step. If you input is here and your output is there, try to make tiny little changes to the input, more and more, until you might end up where you want to go.
If you see it going nowhere, go back and try different steps, bit by bit and see where those lead… eventually you find a solution.
After finding that (most likely ugly) first solution, refactor the crap out of it! Try to come up with ways of simplifying it, rewriting and rewriting some more, until you feel you’ve found an actually pretty neat solution.
As @batjko stated in his reply, make sure you understand the problem you’re trying to solve; try to learn the most basic algorithms (read something like Introduction to Algorithms).
Try to check out CS50 classes, you should learn very useful stuff from there.