Computer Science Grade 7 20 min

Finding Multiple Solutions

Finding Multiple Solutions

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Identify problems that can have more than one correct answer. Design a program that continues searching for solutions after the first one is found. Use a list or array to store multiple solutions found by their program. Implement nested loops to systematically check all possible combinations in a simple problem. Explain the difference between a program that finds one solution and a program that finds all solutions. Modify an existing program to switch from finding a single solution to finding multiple solutions. Ever used a GPS that shows you a few different routes to get to your friend's house? 🗺️ How does it find all of them instead of just one? In this lesson, we'll move beyond problems with only one right answer. You'll learn how to wr...
2

Key Concepts & Vocabulary

TermDefinitionExample Solution SpaceThe entire set of all possible answers or combinations that could potentially solve a problem.For the problem 'pick a number between 1 and 10', the solution space is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. IterationThe process of repeating a set of instructions. We use loops to iterate through a solution space.A 'for' loop that checks every number from 1 to 100 is performing 100 iterations. ConstraintA rule or condition that a solution must satisfy to be considered correct.In the problem 'find all even numbers between 1 and 10', the constraint is that the number must be even. Brute-Force ApproachA straightforward method of problem-solving that consists of systematically checking every single possibility until you find all that wor...
3

Core Syntax & Patterns

The 'Solution Collector' Pattern 1. Create an empty list. 2. Loop through possibilities. 3. If a possibility meets the criteria, add it to the list. 4. After the loop, display the list. Use this pattern when you need to find and save ALL possible solutions to a problem. The key is to not stop the loop after finding the first match. Nested Loops for Combinations for itemA in list1: for itemB in list2: // Check the combination of itemA and itemB When you need to check every possible pairing of items from two (or more) groups, use nested loops. The inner loop will run completely for each single step of the outer loop.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You have a working program that finds all pairs from a list `[2, 3, 4, 6, 8]` that SUM to 12. What is the single, most direct change needed to make it find all pairs whose PRODUCT is 24?
A.Change the input list to `[1, 2, 3, 4, 6, 8, 12, 24]`.
B.Change the `if` condition from `num1 + num2 == 12` to `num1 * num2 == 24`.
C.Add a third nested loop.
D.Rename the `solutions` list to `products`.
Challenging
The original coin problem found these solutions for 30¢: `[(0, 6), (1, 4), (2, 2), (3, 0)]`. If a new constraint is added: 'You must use at least one of each coin type (dimes and nickels)', what would the final solution list be?
A.[(0, 6), (1, 4), (2, 2), (3, 0)]
B.[(1, 4), (2, 2)]
C.[(0, 6), (3, 0)]
D.[]
Challenging
A program uses nested loops to find pairs in a list `numbers`. It generates both `(2, 8)` and `(8, 2)`. To be more efficient and avoid checking pairs twice, a programmer changes the inner loop to start from the element *after* the outer loop's current element. How would this affect the output?
A.It would produce the exact same output, just faster.
B.It would produce only one of each pair, for example `(2, 8)` but not `(8, 2)`.
C.It would miss some valid pairs entirely, like `(6, 4)`.
D.It would cause the program to crash with an index error.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.