# Algorithmic Thinking

Algorithmic Thinking
0

#1

Hi all,

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:

1. Does anyone else find this to be the case? And if so, what do you/did you do to solve the challenges?

2. 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.

Thanks,
Jeremy

#2

We all have been through that

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.

#3

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.

#4

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

#5

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:

1. Understand the problem they are trying to solve (i.e. what is the starting point, where do they want to end up).
2. 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.
3. 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.
4. 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.

Rinse and repeat.

#6

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.

#7

Thank you everyone for taking the time to respond. These are all good ideas and I definitely appreciate the advice!

Thank you.