DEV Community

femolacaster
femolacaster

Posted on • Updated on

Writing Your First Pseudocode: Let’s Help Bori Stay Fit Part 2

In the last series, we described pseudocode and its many advantages.

We were also presented with a problem.

A step by step solution using pseudocode would be considered in this series.

To recap:

PROBLEM:

Bori, an overweight lady who works every day from 8 am to 5 pm or sometimes 3 pm is trying to stay fit. She has to wake up as early as 5 am to prepare to catch a cab. So, she has only late in the evenings to work out. By the end of 3 months, she wants to have the right BMI. Also, she plans to add weight in the right places to be sexy for her hot musician boyfriend all in the space of that time-frame.

PSEUDOCODE SOLUTION:

To start writing pseudocodes, like many other idealistic endeavors, the first step is to summarize the problem you are about to solve with a title. A title that depicts the entirety of the problem and proposed solution. Make sure your title is clear to the reader. Let’s name this pseudocode:

Title: BORI’S FITNESS PSEUDOCODE

Next would be to give meta-details to the pseudocode. The details can help prepare your brain for the thinking process. Meta-details are additional information that supports the title such as a short description, the parties or entities involved, and how long the entire process should run for. So, we have this for the meta-details:

Description: An algorithm to cater to Bori’s perfect body.
Duration: 3 Months.
Parties: Bori, Bori's body, Kit, and Gym.

In order to ensure that the scope of the processes is not breached, we have to specify the things that must be present for the processes to start and the result we want when the processes are complete. We call this pre-condition and post-condition respectively.

So, we can say:

Pre-condition: Kit is always neat and packed.
Post-condition: Perfect body and BMI are achieved for Bori.

How then do you start writing the processes in itself? You must understand that processes are actions and the perfect grammar term that suits are verbs. Remember pseudocodes make use of human language and should follow its principles. So, it is best you begin your processes with verbs. All your processes should be numbered such as step 1, step 2...step n. So, we have something like this:

Step 1:  START
Step 2:  Another Process
.
.
.
Step n: 
END
Enter fullscreen mode Exit fullscreen mode

So, for Bori’s problem, we can use this same approach while putting the amount of time each process would take. You can see that we begin the first process with the START keyword and every other process begins with an action word represented in uppercase. This is not a rule but it helps your pseudocode have more clarity. Also, notice the indentation for processes that are dependent on each other represented with ellipses:

Step 1: START
Step 2: FOR days when Bori closes early from work
..........Branch the gym directly with her kit and do the required ..........exercise. (1 hour)
Step 3: FOR days when she doesn't close early from work
..........Do 3 rounds of either 4 of running on the treadmill, situp, ..........press-up, leg press, wall-ups, leg press, hand press and finish ..........up with yoga. (1 hour)

You see that we have solved Bori’s problem in 2 steps giving the information we have. But we should be able to think further. Look for conditions that can make our processes fail. How about when she is sick or when there is no work that day? These various conditions and how they fit into the entire process is what challenges your brainpower when writing pseudocodes:

Step 4: FOR sick or indisposed days
..........IF energy is preserved
...............DO light circulatory exercises (1 hour)
..........ELSE
...............TAKE fruit
...............LISTEN to music (1 hour)
Step 5: FOR public holidays and weekend
..........ALTERNATE between circulatory exercises and yoga (1hour).

However, let’s imagine there are some conditions your brain cannot just think of. Don’t worry, you can describe how to handle those exceptions in another process. A good combination of verbs to use would be Try and Catch. Think of exceptions like a baseball. Try and Catch the exception. And if you are lucky when the exception is caught, throw the exception in whatever best place you decide. Just like this:

Step 6: TRY and CATCH - (The Exception days not mentioned)
..........THROW rest to Bori’s body

And we can repeat our perfect pseudocode for 3 months just as specified in the problem.

Step 7: IF pseudocode duration is less than or equals to 3 months
...............JUMP to step 8
..........ELSE
...............JUMP to step 9
Step 8: REPEAT step 1-7 (3 months)
Step 9: EXIT

So, here is our pseudocode in full:

Title: BORI’S FITNESS PSEUDOCODE
Description: An algorithm to cater to Bori’s perfect body.
Duration: 3 Months.
Parties:  Bori, Bori's body, Kit, and Gym.
Pre-condition: Kit is always neat and packed.
Post-condition: Perfect body and BMI are achieved for Bori.
Step 1:  START
Step 2: FOR days when Bori closes early from work
..........Branch the gym directly with her kit and do the required ..........exercise. (1 hour)
Step 3: FOR days when she doesn't close early from work
..........Do 3 rounds of either 4 of running on the treadmill, situp, ..........press-up, leg press, wall-ups, leg press, hand press and finish ..........up with yoga. (1 hour)
Step 4: FOR sick or indisposed days
                        ..........IF energy is preserved
                                 ...............DO light circulatory exercises (1 hour)
                        ..........ELSE
                                  ...............TAKE fruit
                                  ...............LISTEN to music (1 hour)
Step 5: FOR public holidays and weekend
                          ..........ALTERNATE between circulatory exercises and yoga (1hour).
Step 6: TRY and CATCH - (The Exception days not mentioned)
                          ..........THROW rest to Bori’s body
Step 7: IF pseudocode duration is less than or equals to 3 months
        ...............JUMP to step 8
      ..........ELSE
        ...............JUMP to step 9
Step 8: REPEAT  step 1-7 (3 months)
Step 9: EXIT 

Enter fullscreen mode Exit fullscreen mode

So, that's it. Your first pseudocode!

We would be checking how efficient the written pseudocode is in the next series. Could we have described a better solution to Bori’s problem or we are adding to her woes?

Top comments (0)