Introduction to Problem Solving
We have seen how to create our own Python programs/scripts and the various stages involved in writing such programs. You may find it useful to create your own program just like we did in this chapter so that you become comfortable with Python as well as problem-solving.
Begin with the Problem
Writing a Python program is not only a matter of producing instructions. It is also an exercise in problem solving. You begin with a problem, think about how it could be solved, and then create a Python program or script that expresses the solution. The more programs you create yourself, the more comfortable you become with both Python and problem-solving.
Do not treat a program as something to copy mechanically. Try to solve the problem yourself first, then ask smart questions when you need help.
Trace the Solution Path
The diagram is a practical learning path rather than a complete list of every stage that might appear in a programming project. Its central idea is that a program is the result of thinking about a problem. An algorithm provides a general process for solving a category of problems, and a Python program or script is created from that problem-solving work.
Use Algorithms as Plans
An algorithm is a general process for solving a category of problems.
An algorithm helps separate the solving idea from the final program. Before focusing on Python instructions, you can think about the general process that would solve the problem. That process can then guide the creation of a Python program or script. This way of thinking is useful because the goal is not merely to produce lines of code; the goal is to produce a solution to a problem.
Plan a Small Program
Turning a Small Task into a Program Plan
Imagine that you want to create a small Python program for a task you have chosen yourself.
State the task: Describe what the program should solve before thinking about individual Python instructions.
Try first: Make your own attempt to work out the solution. This gives you a clearer understanding of what you know and where you need help.
Ask a smart question: If part of the task is unclear, ask about that specific difficulty instead of immediately asking someone else to solve the entire problem.
Describe the algorithm: Write down the general process that could solve this category of problem.
Create the script: Use the algorithm as a guide while creating the Python program or script.
Split it if it grows: If the program becomes large, divide it into smaller pieces so there is less to look at while solving a problem, fixing a bug, or adding a new feature.
The finished program is connected to a deliberate problem-solving process instead of being an unexplained collection of instructions.
This example does not prescribe one universal algorithm for every task. It demonstrates how a learner can move from a problem to an attempted solution, a clearer question, an algorithm, and finally a Python program or script.
Control Complexity with Smaller Pieces
Large programs create a special problem: it becomes impossible to keep the entire program in your mind at the same time. The source material describes programs that may grow to millions of lines and explains why code should be easy to understand. Breaking a large program into multiple smaller pieces gives you less to look at when solving a problem, fixing a bug, or adding a new feature.
Breaking a program into smaller pieces is not only useful when first solving a problem. It also helps when fixing a bug or adding a new feature.
Mistakes to Avoid
Looking for a solution before trying the problem yourself
You lose an opportunity to become comfortable with Python and problem-solving.
Fix:
Make a genuine attempt first, then ask a focused question about the part that remains unclear.Asking a vague question
The question does not show which part of the problem needs clarification.
Fix:
Ask a smart question about the specific step or idea that is causing difficulty.Treating a large program as one indivisible object
As programs grow, it becomes increasingly difficult to keep the entire program in your mind at the same time.
Fix:
Break the program into multiple smaller pieces.Focusing only on writing code
The program can become disconnected from a clear solution to the problem.
Fix:
Think about the problem and its general solving process before creating the Python program or script.
Apply the Process
Choose a small program that you would like to create. Write down the problem it should solve, describe your first attempt, record one smart question you might ask, and outline the algorithm as a general process. Then decide whether the program would need to be divided into smaller pieces as it grows.
Hints
- Keep the chosen task small enough to describe clearly.
- Describe the solving process rather than writing Python code.
- Think about what would become difficult if the program became much larger.
Remember the Method
- Creating a Python program or script is part of a broader problem-solving process.
- An algorithm is a general process for solving a category of problems.
- Try to solve a problem yourself before asking for help, and ask smart questions when help is needed.
- As programs become large, break them into smaller pieces so they are easier to understand, modify, and debug.
- Creating your own programs helps you become more comfortable with Python and problem-solving.
Key Takeaways
- A Python program is the result of solving a problem, not merely writing instructions.
- An algorithm describes a general process for solving a category of problems.
- Independent effort followed by focused questions strengthens problem-solving practice.
- Breaking large programs into smaller pieces makes problems, bugs, and new features easier to handle.