Image description

Seunex Da G

10 months ago
Topic

Some of the feedback I hear from new developers working on a programming problem revolves around uncertainty of where to start. You understand the problem, the logic, basics of the syntax, etc. If you see someone else’s code or have someone to guide you, you can follow along. But maybe you feel uncertain about doing it yourself and have trouble turning your thoughts into code at first even though you understand the syntax or logic. Here’s my process and some tips to tackling a sample problem that hopefully some of you may find helpful in your journey.

1. Read the problem at least three times (or however many makes you feel comfortable)

You can’t solve a problem you don’t understand. There is a difference between the problem and the problem you think you are solving. It’s easy to start reading the first few lines in a problem and assume the rest of it because it’s similar to something you’ve seen in the past. If you are making even a popular game like Hangman, be sure to read through any rules even if you’ve played it before. I once was asked to make a game like Hangman that I realized was “Evil Hangman” only after I read through the instructions (it was a trick!).

Sometimes I’ll even try explaining the problem to a friend and see if her understanding of my explanation matches the problem I am tasked with. You don’t want to find out halfway through that you misunderstood the problem. Taking extra time in the beginning is worth it. The better you understand the problem, the easier it will be to solve it.

Let’s pretend we are creating a simple function selectEvenNumbers that will take in an array of numbers and return an array evenNumbers of only even numbers. If there are no even numbers, return the empty array evenNumbers.

function selectEvenNumbers() {
// your code here
}

Here are some questions that run through my mind:

  • How can a computer tell what is an even number? Divide that number by 2 and see if its remainder is 0.
  • What am I passing into this function? An array
  • What will that array contain? One or more numbers
  • What are the data types of the elements in the array? Numbers
  • What is the goal of this function? What am I returning at the end of this function? The goal is to take all the even numbers and return them in an array. If there are no even numbers, return an empty array.

2. Work through the problem manually with at least three sets of sample data

Take out a piece of paper and work through the problem manually. Think of at least three sets of sample data you can use. Consider corner and edge cases as well.

Corner case: a problem or situation that occurs outside of normal operating parameters, specifically when multiple environmental variables or conditions are simultaneously at extreme levels, even though each parameter is within the specified range for that parameter.

Edge case: problem or situation that occurs only at an extreme (maximum or minimum) operating parameter

For example, below are some sets of sample data to use:

[1]
[1, 2]
[1, 2, 3, 4, 5, 6]
[-200.25]
[-800.1, 2000, 3.1, -1000.25, 42, 600]

When you are first starting out, it is easy to gloss over the steps. Because your brain may already be familiar with even numbers, you may just look at a sample set of data and pull out numbers like2 , 4 , 6 and so forth in the array without fully being aware of each and every step your brain is taking to solve it. If this is challenging, try using large sets of data as it will override your brain’s ability to naturally solve the problem just by looking at it. That helps you work through the real algorithm.

Let’s go through the first array [1]

  1. Look at the only element in the array [1]
  2. Decide if it is even. It is not
  3. Notice that there are no more elements in this array
  4. Determine there are no even numbers in this provided array
  5. Return an empty array

Let’s go through the array [1, 2]

  1. Look at the first element in array [1, 2]
  2. It is 1
  3. Decide if it is even. It is not
  4. Look at the next element in the array
  5. It is 2
  6. Decide if it is even. It is even
  7. Make an array evenNumbers and add 2 to this array
  8. Notice that there are no more elements in this array
  9. Return the array evenNumbers which is [2]

I go through this a few more times. Notice how the steps I wrote down for [1] varies slightly from [1, 2]. That is why I try to go through a couple of different sets. I have some sets with just one element, some with floats instead of just integers, some with multiple digits in an element, and some with negatives just to be safe.

3. Simplify and optimize your steps

Look for patterns and see if there’s anything you can generalize. See if you can reduce any steps or if you are repeating any steps.

  1. Create a function selectEvenNumbers
  2. Create a new empty array evenNumbers where I store even numbers, if any
  3. Go through each element in the array [1, 2]
  4. Find the first element
  5. Decide if it is even by seeing if it is divisible by 2. If it is even, I add that to evenNumbers
  6. Find the next element
  7. Repeat step #4
  8. Repeat step #5 and #4 until there are no more elements in this array
  9. Return the array evenNumbers, regardless of whether it has anything in it

This approach may remind you of Mathematical Induction in that you:

  1. Show it is true for n = 1n = 2...
  2. Suppose it is true for n = k
  3. Prove it is true for n = k + 1


4. Write pseudocode

Even after you’ve worked out general steps, writing out pseudocode that you can translate into code will help with defining the structure of your code and make coding a lot easier. Write pseudocode line by line. You can do this either on paper or as comments in your code editor. If you’re starting out and find blank screens to be daunting or distracting, I recommend doing it on paper.

Pseudocode generally does not actually have specific rules in particular but sometimes, I might end up including some syntax from a language just because I am familiar enough with an aspect of the programming language. Don’t get caught up with the syntax. Focus on the logic and steps.

For our problem, there are many different ways to do this. For example, you can use filter but for the sake of keeping this example as easy to follow along as possible, we will use a basic for loop for now (but we will use filter later when we refactor our code).

Here is an example of pseudocode that has more words:

function selectEvenNumberscreate an array evenNumbers and set that equal to an empty arrayfor each element in that array
see if that element is even
if element is even (if there is a remainder when divided by 2)
add to that to the array evenNumbers
return evenNumbers

Here is an example of pseudocode that has fewer words:

function selectEvenNumbersevenNumbers = []for i = 0 to i = length of evenNumbers
if (element % 2 === 0)
add to that to the array evenNumbers
return evenNumbers

Either way is fine as long as you are writing it out line-by-line and understand the logic on each line.

Refer back to the problem to make sure you are on track.

Muili Muhammed

10 months ago

Thank, for the Tips

Seunex Da G

10 months ago

@Muhammed they are not a bad tip aren't they?

Joshrose

5 months ago

They really are Great Tips. I have read them several times now.

Thank You for thinking of us Rookies