- Develop a strategy for breaking down multi-step problems
- Practice writing pseudocode to explain the process
- Identify programming concepts that can achieve the goal
Imagine someone you know has never made a peanut butter and jelly sandwich before. Take 2 minutes and jot down every step of creating the perfect peanut butter and jelly sandwich.
After writing those steps, watch this video of a dad following the directions his kids gave him. How do your directions compare?
Approaching complex tasks in code is very similar to approaching complex tasks in real life! Similar to the dad in the video, your program only does exactly what you tell it to do, so it's very important that we can break challenging problems down into clear and simple steps.
Problem Solving Process
With practice, you’ll get faster at problem solving, but in the beginning it can be helpful to slow down the process and think through each step. We recommend using the following 8 steps to Problem Solving as a guide to help you get started.
- Restate the goal. Reframe the end goal in your own words. Take note of exactly what you expect to happen.
- Consider the data. What data types are you working with? Will you use parameters and arguments?
- Ask clarifying questions. If anything is still unclear, pause for a moment to get clarification. You’ll need to have a clear idea of both what you’re hoping to accomplish and the data you’re working with before you begin.
- Break it down. Pseudocode the steps you might need to take. Write the steps in plain English, but you may use technical terms that reference data types or methods you think might be helpful.
- Research. Is there anything in your pseudocode that you don’t know how to do? Take note of that and use Google as a tool to find some things you might try.
- Start coding. Use your pseudocode as a guide and start writing your code! Work in iterations, starting with the simplest possible output and slowly add layers of complexity.
- Stuck? Go back to your pseudocode and rethink what you might be missing. You may need to reorder your steps to continue.
- Refactor. After we’ve found one solution, it’s always a good idea to go back and see if there’s another simpler way to achieve the same result. Sometimes after solving a problem once, we are able to see it from a new perspective.
A Note About Research
While we’ve all used Google a time or two, there are ways to make your search more effective. Follow the guidelines below to ensure you get the best possible results.
- Include the programming language
- Include the data type (if applicable)
- Use technical terms
- Choose reliable sources (MDN or Ruby docs)
If the result you’re looking for isn’t in the first two pages of results, it’s probably not there. You might try reframing your search terms or use another resource, like your Turing community!
Problem Solving in Action
Let’s work through those steps together, using Challenge 1. Open a blank replit (using your programming language) and we’ll complete the challenge below.
Start with an array of strings. Then, print only the words with more than 3 characters.
Work with your partner to complete Challenge 2.
Start with an array of strings. Print only the words that start with the letter s.
Before continuing, take some time to reflect on the following questions.
- What do you like about this process?
- What might make this process challenging?
- Are there any steps you would add or modify?
For this next one, start by working through the Problem Solving steps on your own.
Start with an array of strings. Print all of the words in the array, but change every t to an uppercase T.
After 5 minutes of independent work, share with your partner the steps that you completed and how you went about solving the problem. Take note of anything your partner did that could help improve your process!
What if we wanted to write a method or function to achieve the same goal for Challenges 1-3? This would allow us to reuse that code over and over for any array! Use your Problem Solving steps to go back and refactor your solutions.
Continue to Strengthen This Skill
This was only an introduction to Problem Solving and pseudocoding. It’s a skill that you will continue to develop for years, so don’t beat yourself up if it’s still confusing or hard! Below, you’ll find a couple more challenges to give you an opportunity to continue developing those problem solving skills.
Start with an array of student names. Print a numbered list of the student's names in alphabetical order. For example, if you start with
["Hector", "Winston", "Finley"], the final output should be
1. Finley 2. Hector 3. Winston.
Here’s another one - notice that we start with an array of integers, but our output will be a string.
Write a method or function that accepts an array of 10 integers (between 0 and 9) and returns a string of those numbers in the form of a phone number. For example, if you were given
[5, 5, 5, 1, 2, 3, 4, 5, 6, 7], the method or function will return
Those two challenges are helpful, but you don’t have to stop there! Here are two more resources outside of Turing that also provide opportunities to strengthen this skill:
- CodeWars - Create a free account, choose your language, and start solving challenges right away! They get increasingly more challenging, so don’t get discouaged if they are difficult.
- The Odin Project - A free open source curriculum with a course on problem solving.
Take a moment to reflect on each of the following questions:
- This process is definitely slower than starting by writing code. Why might this be helpful, even though it takes more time?
- Some pseudocoding is more helpful when we sit down to start writing the actual code. What are some characteristics of helpful pseudocode?
Check For Understanding
Complete at least 3 challenges in the Check For Understanding repository. Submit your copy of the repository in the submission form.