Breaking Into the Programming Mindset

When I decided I wanted to become a programmer, the biggest hurdle I had was breaking into the programming mindset. Writing code for me was very difficult when I began learning how to program because in order to write code you need to be able to solve the problem first! When I looked at a problem that I had to solve, I would often freeze up and not know where to start. There is material from Launch School, like the Problem Solving videos, that demonstrates how to decompose a problem which is what helped me establish a systematic approach on how to tackle a programming problem. I wanted to share the different steps I take in order to solve programming problems so that it may help another student if they feel stuck.

1. Look at what the input and output is.

I like doing this because it gives you an idea of what methods you should use in your code. I will be using a problem that I got from Codewars (a level 5 kyu) as an example.

# Write a method that takes a maximum bound and returns all primes 
# starting with 0 up-to and including the maximum bound.
# test case
prime_numbers(11) # should return [2,3,5,7,11]

In the example above, we have an integer as an input to the prime_numbers method and we are returning an array.

2. Read the problem and jot down notes.

Jotting down notes gives me the opportunity to pick out important information that I can later use when solving the problem and writing down what the problem wants in my own worlds helps me fully digest what the problem is asking.

Lets pick apart the problem given.

# Write a method that takes a maximum bound...

The maximum bound is the integer 11 that is passed into the prime_numbers method and will be denoted as n.

...and returns all primes starting with 0 up-to and including the maximum bound.

We want to return prime numbers from 0 up to the maximum bound n. So it looks like we will be working with a range (0..n).

In summary we need to return all prime numbers that are in the range (0..n).

Since we established in step number 1 that we want to return an array and we know that we are working with a range, a useful method we can use for this situation is the each method. This will allow us to iterate through the range so we can pick out prime numbers and then put the prime numbers in an array which is what we want the method to return.

3. What is the logic behind what I want to accomplish?

In this example we want to return prime numbers between a certain range. The main logic of this problem is deciding if a number is prime or not.

We first need to know what a prime number is. A prime number is a natural number that has no positive divisors other than 1 and itself and is greater than 1.

We need to first check to see if the possible prime number is greater than 1 because if it is not, then it cannot be a prime number. We then need to see if any positive divisor (besides 1 and the number itself) can be divided into the possible prime number evenly because if there is then we know for sure that the number is not prime. Likewise, if there is not any positive divisor (besides 1 and the number itself) that can be divided into the possible prime number evenly then the number is prime.

It would be best to extract this logic to a prime? method so that when we are done writing the prime? method we can check to see if it works correctly before implementing it into our main code.

4. Write pseudocode.

Now that I have a good understanding of what the problem wants, I can write steps I need to take in order to solve the problem.

- Create an empty array called results to put all of the prime numbers in
- Iterate from 0 to the maximum bound number n, and for each number 
   - If the number is a prime number, put it into the results array
- Return the results array 

The prime word is bolded above because we want to extract that logic to a prime? method. The pseudocode for the prime? method is below.

- Return false if number is less than or equal to 1
- Iterate from the integer 2 to the number passed into the method                minus 1, and for each divisor
   - If the number can be divided by the divisor evenly then return
false
- If the number cannot be divided by the divisor evenly then return
true

5. Translate the pseudocode into code.

I like to translate the pseudocode into code one small step at a time so I can run the code frequently and check for errors. (You can also translate your pseudocode into a more formal pseudocode before writing out code.)

# - Create an empty array called results to put all of the prime  numbers in
def prime_numbers(n)
results = []
end
# - Iterate from 0 to the maximum bound number n, and for each number
def prime_numbers(n)
results = []
(0..n).each do |num|

end
end
# - If the number is a prime number, put it into the results array
def prime_numbers(n)
results = []
(0..n).each do |num|
results << num if prime?(num)
end
end
# - Return the results array
def prime_numbers(n)
results = []
(0..n).each do |num|
results << num if prime?(num)
end
results
end

And now for the prime? method.

# - Return false if number is less than or equal to 1
def prime?(num)
return false if num <= 1
end
# - Iterate from the integer 2 to the number passed into the method minus 1, and for each divisor
def prime?(num)
return false if num <= 1
(2..(num - 1)).each do |divisor|
  end 
end
# - If the number can be divided by the divisor evenly then return
false
def prime?(num)
return false if num <= 1
(2..(num - 1)).each do |divisor|
return false if num % divisor == 0
end
end
# - If the number cannot be divided by the divisor evenly then return true
def prime?(num)
return false if num <= 1
(2..(num - 1)).each do |divisor|
return false if num % divisor == 0
end
true
end

6. Refactor Code.

Here is the finished product! If refactoring is needed, now is a great time to do it.

def prime?(num)
return false if num <= 1
(2..(num - 1)).each do |divisor|
return false if num % divisor == 0
end
true
end
def prime_numbers(n)
results = []
(0..n).each do |num|
results << num if prime?(num)
end
results
end
prime_numbers(11) # should return [2,3,5,7,11]

The take away of this blog is to demonstrate that a programming mindset is taking a complex problem and being able to break it down into pieces in order to solve it.