Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

From Problem Statement to Pseudocode and Examples

Recommended resources

Hello! In our last session, we focused on applying structured thinking to your learning process through active recall. We saw how creating a plan—in that case, a set of questions—before you try to "perform" (i.e., recall information) leads to much stronger results. Today, we'll apply that same core principle of "plan before you do" to the world of programming.

Many new programmers, when given a problem, feel an immediate urge to start writing code. This is often counterproductive. It's like trying to build a house without a blueprint. You might get a wall up, but you'll soon run into problems with the foundation, the wiring, and the overall structure. A little bit of planning goes a long way.

In this lesson, you will learn how to translate a programming problem into a clear, actionable plan before writing any Python code. Specifically, you'll learn to:

  1. Define representative input/output examples to clarify what the program must do.
  2. Write step-by-step pseudocode to outline the logic of how the program will do it.

Mastering this two-step planning process is one of the most important habits you can build as a software developer.

Why Plan? The Power of Thinking Before Coding

Jumping straight into coding can feel productive, but it often leads to confusion, messy code, and wasted time debugging a flawed approach. Planning forces you to separate the problem-solving from the implementation. First, you solve the problem in your head and on paper; then, you translate that solution into code.

Let's watch a short video that explains the benefits of using pseudocode, a key planning tool, to abstract away the details of syntax and focus purely on the logic of the solution.

What is pseudocode and how do you use it?

This video from Codecademy discusses the value of pseudocode.

Please watch the segment from the beginning, which explains why this practice is so valuable for breaking down problems and thinking like a programmer.

The core idea is to first build a solid mental model of the solution. A great starting point for this is the simple but powerful Input-Process-Output (IPO) model. For any given programming task, you should be able to clearly state:

  • Input: What information does the program need to start?
  • Process: What steps does it need to take to transform the input?
  • Output: What result should it produce?

This simple framework helps you structure your thoughts and is the foundation of the planning process we're about to explore.


Step 1: Clarify the Goal with Input/Output Examples

Before you can figure out how to solve a problem, you must be absolutely certain about what the problem is. The most effective way to do this is by working through a small, concrete example by hand. This forces you to understand the requirements and the desired outcome.

Let's explore this idea using an excellent guide from a Duke University course.

2. Problem Solving by Programming — Programming for Financial Technology

This resource introduces the first step of a systematic problem-solving process.

Please read the section titled 2.1. Work an Instance Yourself. It demonstrates how to take a problem statement and manually process a small set of data to understand the required steps and the final output.

Working an example by hand naturally leads to the next crucial step: defining more test cases. You should think about what representative inputs will test your program's logic. These examples become your specification and, later, your test suite.

It's vital to consider not just "normal" cases, but also corner cases or edge cases. These are inputs that are extreme, empty, or unusual and might cause a simple algorithm to fail.

For example, if a problem asks you to find the average of a list of numbers, you might define these I/O examples:

  • Normal Case: Input: [10, 20, 30] -> Output: 20
  • Case with Negatives: Input: [-10, 10, 30] -> Output: 10
  • Corner Case (Single Item): Input: [42] -> Output: 42
  • Corner Case (Empty List): Input: [] -> Output: ? (This reveals an ambiguity! Should it be 0, or an error? Planning forces you to ask this question.)

The resource you just read also discusses the importance of using these examples to test your logic.

2. Problem Solving by Programming — Programming for Financial Technology

This section expands on the idea of using examples for validation.

Now, please read section 2.4. Test your Steps. Focus on how it moves from the original example to testing different situations and the discussion on identifying corner cases.


Step 2: Create a Blueprint with Pseudocode

Once you have a clear understanding of the "what" (via your I/O examples), you can start planning the "how." This is where pseudocode comes in.

Pseudocode is a way of describing the steps of an algorithm using plain English mixed with programming-like conventions. It is not tied to any specific language like Python. Its purpose is to let you express the logic of your solution without worrying about precise syntax like colons, parentheses, or specific function names.

Pseudocode and Flowchart: Complete Beginner's Guide

This article from Codecademy provides a clear definition and practical advice for writing pseudocode.

First, read the section "What is Pseudocode?" to get a formal definition. Then, carefully review the best practices in the section "How to write pseudocode". Pay close attention to the use of keywords like IF/THEN, FOR, and indentation.

The process of writing pseudocode often starts by verbalizing or writing down the exact steps you took when working through your manual example. Then, you generalize those steps to work for any valid input. Let's see how the Duke University guide illustrates this transition from a specific, manual process to a general, abstract algorithm.

2. Problem Solving by Programming — Programming for Financial Technology

This part of the guide shows the key mental step of abstraction.

Read section 2.2. Write Down Exactly What You Just Did, which lists the concrete steps for the specific example. Then, continue to section 2.3. Generalize your Steps to see how those concrete steps are turned into a general algorithm—this is the pseudocode!

Notice how the generalized steps no longer mention "Coffee" or "Bus pass" but instead refer to "the transaction line" or "the category." This is the essence of algorithmic thinking.


A Complete Example: The Password Validator

Let's tie this all together with a practical example that goes from the problem statement all the way to the final code.

Pseudocode and Flowchart: Complete Beginner's Guide

This section of the Codecademy article provides a full walkthrough.

Please read the entire section Practical example of pseudocode and flowchart. It starts with a problem statement for a password validator. Notice how it first defines valid and invalid I/O examples. Then, it develops the pseudocode implementation. Finally, it shows how easily the pseudocode translates into real Python code. (You can ignore the flowchart part for now).

This example perfectly illustrates the workflow you should aim for. The IPOT (Input, Processing, Output, Testing) chart, mentioned in the Duke resource, is a great tool for formally documenting this plan.


Your Turn: Plan a Solution

Now it's time to practice this process yourself. Consider the following programming problem:

Problem: Given a list of numbers, find and return the largest number in the list.

Your Task:
On a piece of paper or in a text editor, create a plan to solve this problem. Do not write any Python code yet. Your plan should include two parts:

  1. Input/Output Examples:

    • Write at least three representative test cases. Include a typical case, and try to think of one or two corner cases. For each, show the Input list and the expected Output number.
  2. Pseudocode:

    • Write a step-by-step algorithm in pseudocode that describes how to solve this problem for any list of numbers.
    • Hint: You will likely need a variable to keep track of the "largest number seen so far." As you iterate through the list, you'll compare each number to this variable and update it if you find a new largest number. Use keywords like SET, FOR EACH, and IF as described in the resources.

Take about 10-15 minutes to complete this. The goal is not to find the most clever solution, but to practice the process of systematic planning.


Conclusion

In this lesson, we've built on our theme of structured thinking, extending it from how you learn to how you solve problems. By deliberately planning before you code, you move from being a reactive coder to a proactive problem-solver.

Here are the key takeaways:

  • Always plan your solution before you start writing code.
  • Begin by creating representative input/output examples. This clarifies the problem and helps you consider corner cases.
  • Write pseudocode to create a language-independent blueprint of your algorithm, focusing on logic rather than syntax.
  • This process (Examples -> Pseudocode -> Code) helps you write better, more correct code faster and with less frustration.

In our next lesson, we will focus on what happens when you start implementing your plan. We will learn how to apply an attempt–check–correct cycle to your programming exercises, a systematic method for finding and learning from the inevitable mistakes and bugs that arise during coding.