Pseudo-coding 101

Keep in mind that one peck is equal to 8 quarts.

Let’s also assume that the peppers were pepperoncinis, and that there are 15 pepperoncinis per quart.

1.

Take in the number of pecks Peter Piper picked, the conversion from pecks to quarts, and the conversion from quarts to peppers.

2.

Multiply the two conversion factors to determine how many peppers per peck.

3.

Multiply the number of pecks Peter Piper picked by the peppers per peck conversion.

4.

Return the totalNow we have an algorithm that can solve for the total number of peppers no matter how many pecks that Peter Piper might pick.

It is now time to add more complexity to our algorithms.

Here we will be looking at loops and conditionals.

Loops are instructions that are done repeatedly, and conditionals are instructions that are performed only when a certain condition is true.

Conditional instructions can also include multiple cases in which we would include an “else”.

Before we actually write an algorithm including a loop or conditional however, let’s outline how those things are written.

LoopsFor loops, we need a few pieces of information.

We need a place to start, a place to end, and how large of a step or increment to take each time.

In writing an algorithm, the instructions for a generic loop would look something like this:1.

Count from x to y in increments of 12.

Each time, do somethingWrite an algorithm that will return the numbers from one to some number one at a time.

1.

Take in num11.

Count from 1 to num1 in increments of 1.

2.

Each time, return count.

ConditionalsFor a conditional instruction, we also need a few pieces of information.

We need to know what the condition that needs to be met for a given instruction might be, as well as if there is something else that we should do if conditions are different, or if we should just continue on.

We would write a conditional instruction like this:1.

If (condition) do something.

2.

Else if (condition) do something else.

3.

Else continue.

In order to demonstrate a conditional statement, we are going to combine it with a conditional statement just for the sake of practice.

Write an algorithm that will return all of the even numbers between 1 and some number.

1.

Take in num12.

Count from 1 to num1 in increments of 13.

Each time, if count is evenly divisible by 2a.

return countNow that have covered all the basics, of how to write an algorithm, let’s see if we can apply what we know to one of the staple whiteboarding problems in the programming community: FizzBuzz.

FizzBuzz goes something like this.

Write an algorithm that will return “Fizz” if a number is divisible by 3, “Buzz” if a number is divisible by 5, and “FizzBuzz” if divisible by both.

Otherwise, just return the number, from 1 to some number.

As always our first step will be to take in any needed inputs.

Then we can run through our other steps.

1.

Take in num1.

2.

Count from 1 to num1 in increments of 1.

3.

Each time:a.

If count is evenly divisible by 3 and 5, return “FizzBuzz”b.

If count is evenly divisible by 3, return “Fizz”c.

If count is evenly divisible by 5, return “Buzz”d.

Else return countRemember, that our instructions must be as simple as possible.

Meaning that each time we go through the loop, we will only pass one of the tests, and if we do, we are done with that turn in the loop.

If that is the case, this algorithm will only work because it is written in this particular order.

With the steps written in this order, we will test each number to see if it is evenly divisible by 3 and 5.

If it is, we will return “FizzBuzz”.

If not, we will then test if the number is evenly divisible by 3.

If so, we will return “Fizz”.

If not, we will test to see if the number is evenly divisible by 5.

If so, we will return “Buzz”.

Then if the number is not evenly divisible by either 3 or 5, we will return the number.

If we had instead written the step in 3a after b and c in the order of tests, we never would have gotten to it because we would have passed one of those tests first and ended that turn in the loop.

So we would have seen “Fizz” for all numbers only divisible by 3, as well as numbers divisible by 3 and 5 because we would pass the first test for numbers divisible by 3.

We would then see “Buzz” for all numbers only divisible by 5, and of course all remaining numbers would just be returned.

Without us ever hitting the test for numbers divisible by 3 and 5, we would never see “FizzBuzz”.

Pseudo-codingPseudo-coding uses many of the conventions of a normal programming language, but is intended more readable by a person.

Its really just a step in between writing an algorithm, and writing a program in a specific language.

Like in a programming language we will be pseudo-coding functions.

However, we will have a shift from the more explicit language of writing algorithms, to a greater use of mathematical operators, and brackets.

To make our lives a little easier, let’s outline the meaning that each of the different brackets carry?- ( ) Do this first, take in argument(s), use this.

– { } Steps of a function, values in an object.

– [ ] An index/address in an array.

Just for fun, if we were to pseudo-code out our tongue twister from above, it might look something like this:Write a function that will return all numbers from 1 to some number.

Write a function that will return all even numbers from 1 to some number.

Now let’s see what it would look like if we were to apply this format to our old friend FizzBuzz.

Write an algorithm that will return “Fizz” if a number is divisible by 3, “Buzz” if a number is divisible by 5, and “FizzBuzz” if divisible by both.

Otherwise, just return the number, from 1 to some number.

We have covered a lot of ground here.

I hope that this can help those new to coding learn programming concepts more clearly, as well as more quickly.

.. More details

Leave a Reply