How to Build an Algorithm Part 2: Iterate Through Possible Solutions

This is the second of two parts on building an algorithm.

After you've defined your inputs and outputs (from Part 1) you can start working through the possible solutions. The tests will confirm if your proposed solution works and if your solutions for edge cases break the main course. You know what is supposed to happen, so now you can develop a 'hello world' solution. This is the most basic solution to the first test on your list. You should only attempt to solve one test at a time, this will save you time, although it seems counterintuitive.

Write the 'hello world' solution in pseudo code. This means plain English, and each line of pseudo code should translate easily into a line of actual code. It may help you to write it in comments because that will encourage you to write English. If your pseudo code doesn't clearly express actual code it may be too broad and you should clarify it before moving on. Walk through this code from input to output and make sure it does what you expect. If your pseudo code appears to output what you expect. Translate each line into a line of code.

Review the tests. Remember that your goal is to pass one test at a time, from the first to the last. As you begin to solve later tests, you may make changes that modify earlier tests. When this happens, revert back to the code that previously worked and continue. Iterate through new solutions until you find one that solves all your tests. Writing code is the final step, and only a fraction of the total time spent, in solving an algorithm. Front-loading the work saves you time and energy. Happy coding!