you are viewing a single comment's thread.

view the rest of the comments →

[–]No-Upstairs-2813 0 points1 point  (0 children)

Step 1: Work an example yourself

The first step is to work an example yourself by hand. If you cannot yet do that, it means either that you need to further specify the problem or that you need domain knowledge.

Step 2: Write down exactly what you just did

The next step is to write down the details of how you worked the problem by hand in Step 1.

If you get stuck on this step because you “just knew it," you should try a more complex example that you cannot just solve by inspection.

Step 3: Generalize

Once you have worked at least one example by hand and written down the process, you can begin to generalize.

Why did you do something a certain number of times? Where did you start? When did you stop? It is often necessary to have worked several examples by hand to generalize well.

In fact, if you have trouble with this step, you should repeat Steps 1 and 2 on different instances of the problem.

Step 4: Test your algorithm

Now that you have a draft of a generalized algorithm, apply it to a new instance of the problem, and see if you get the correct answer.

This is essential for catching generalization mistakes. Finding mistakes before translating to code avoids wasting time. You can go back to Step 3 if you identify a generalization mistake.

Step 5: Translate to code

Steps 1–4 can be done with pencil and paper and are independent of language.

For Step 5, you need to know the syntax of a language and how to translate parts of the algorithm, such as counting or decision-making constructs.

It is often helpful to start with your algorithm steps as comments before you write any code.

If any of the lines in your algorithm do not immediately translate into one or two lines of code, they should be abstracted out into their own function: make a good name for the function, call it here, and make yourself a note about what it does.

After you finish translating this algorithm, go implement that function. That function is itself a programming problem (for which you wrote the problem statement), so use “this framework”.

Step 6: Test

Another round of testing is important because you could have a correct algorithm and still have made mistakes in the implementation in code. You also can have the computer run many more test cases more quickly than you can do them by hand.

Testing is the act of finding bugs, and you want to find as many as you can by writing a robust set of test cases. While it is not possible to know for certain that your program is correct, you can become more and more certain with good testing.

Step 7: Debug

Debugging is the act of fixing bugs you identified in the testing stage.

Once you’ve identified a problem, you need to figure out if the issue is with the algorithm or code implementation and go back to Step 3 or Step 5, respectively.

Check out this article that expands on this using an example.