Form 4 - Computer
2024-2025
Floor 4 - Computer Room
Mr. Peter
Nested While Loops

Santa Rosa de Lima English Secondary School
聖羅撒英文中學
Colégio de Santa Rosa de Lima - Secção Inglesa
Python Exercises
Python Nested Loops - Ex01
Catching a Hit-and-Run Driver
1.
Save your file as "ClassNumber_YourName_Ex01.py"
A truck violated traffic rules and fled the scene after hitting a pedestrian. There were three eyewitnesses at the scene. Although none of them could remember the full license plate number, they each provided some useful clues:
Witness A said: “The first two digits of the license plate number are the same.”
1
Witness B said: “The last two digits of the license plate number are the same, but different from the first two digits.”
2
Witness C, a mathematician, said: “The 4-digit license plate number is a perfect square.”
3
Based on these clues, write the python to get the license plate number.
Python Nested Loops - Ex02
Integer to Roman Numeral Converter
1.
Save your file as "ClassNumber_YourName_Ex02.py"
In ancient Rome, numbers were written using combinations of letters from the Latin alphabet: I, V, X, L, C, D, and M. Create a Python function that converts a given integer to its corresponding Roman numeral. Your implementation should manually handle the conversion logic without using built-in Roman numeral libraries.
Start by setting num = 1987 and an empty string roman = "".
1
Use only while and if statements to convert num into a Roman numeral.
2
Use the provided values and symbols lists to match and subtract values from num.
3
Append the correct Roman symbol to roman and print the final result.
4
Below are the lists you must use in your solution:
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
I = 1, IV = 4, V = 5, IX = 9, X = 10, XL = 40, L = 50, XC = 90, C = 100, CD = 400, D = 500, CM = 900, M = 1000.
1987 => MCMLXXXVII
Example
Python Nested Loops - Ex02
Integer to Roman Numeral Converter
1.
Save your file as "ClassNumber_YourName_Ex02.py"
In ancient Rome, numbers were written using combinations of letters from the Latin alphabet: I, V, X, L, C, D, and M. Create a Python function that converts a given integer to its corresponding Roman numeral. Your implementation should manually handle the conversion logic without using built-in Roman numeral libraries.
Start by setting num = 1987 and an empty string roman = "".
1
Use only while and if statements to convert num into a Roman numeral.
2
Use the provided values and symbols lists to match and subtract values from num.
3
Append the correct Roman symbol to roman and print the final result.
4
Below are the lists you must use in your solution:
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
I = 1, IV = 4, V = 5, IX = 9, X = 10, XL = 40, L = 50, XC = 90, C = 100, CD = 400, D = 500, CM = 900, M = 1000.
1987 => MCMLXXXVII
Example
Python Nested Loops - Ex03
Check if Two Strings Are Anagrams Using While Loops
1.
Save your file as "ClassNumber_YourName_Ex02.py"
You are writing a Python script to determine whether two strings are anagrams. An anagram is made by rearranging all the letters of one word to form another, using every letter exactly once.
The two input strings are already in lowercase and contain only alphabet letters.
1
If the lengths of the two strings are not equal, display the message: "Not anagrams: different lengths." and stop the program.
2
Use a while loop to go through each character of the first string one by one.
3
For each character in the first string, use another while loop to go through the second string character by character to find a match.
4
In the if condition inside the second loop, use == to compare characters. If a match is found, mark that match as used in a separate way (such as using a list or another string). If no match is found for any character, display the message: "Not anagrams: character mismatch." and stop. If all characters are matched, display: "The strings are anagrams."
5
str1 = "listen"
str2 = "silent"
# Expected output:
# The strings are anagrams.
7
Examples
str1 = "elbow"
str2 = "below"
8
More Examples
str1 = "dusty"
str2 = "study"
str1 = "night"
str2 = "thing"
How to convert a string into a list and remove an element using pop() by index number.
6
str1 = "listen"
str2 = "silent"
str2 = list(str2)
# Removes the character at index 2 ('l') from the list: ['s', 'i', 'e', 'n', 't']
str2.pop(2)
print(str2)
Examples
Python Nested Loops - Ex03
Check if Two Strings Are Anagrams Using While Loops
1.
Save your file as "ClassNumber_YourName_Ex02.py"
You are writing a Python script to determine whether two strings are anagrams. An anagram is made by rearranging all the letters of one word to form another, using every letter exactly once.
The two input strings are already in lowercase and contain only alphabet letters.
1
If the lengths of the two strings are not equal, display the message: "Not anagrams: different lengths." and stop the program.
2
Use a while loop to go through each character of the first string one by one.
3
For each character in the first string, use another while loop to go through the second string character by character to find a match.
4
In the if condition inside the second loop, use == to compare characters. If a match is found, mark that match as used in a separate way (such as using a list or another string). If no match is found for any character, display the message: "Not anagrams: character mismatch." and stop. If all characters are matched, display: "The strings are anagrams."
5
str1 = "listen"
str2 = "silent"
# Expected output:
# The strings are anagrams.
7
Examples
str1 = "elbow"
str2 = "below"
8
More Examples
str1 = "dusty"
str2 = "study"
str1 = "night"
str2 = "thing"
How to convert a string into a list and remove an element using pop() by index number.
6
str1 = "listen"
str2 = "silent"
str2 = list(str2)
# Removes the character at index 2 ('l') from the list: ['s', 'i', 'e', 'n', 't']
str2.pop(2)
print(str2)
Examples
Python Nested Loops - Ex02
Check if Two Strings Are Anagrams Using While Loops
1.
Save your file as "ClassNumber_YourName_Ex03.py"
You are writing a Python script to determine whether two strings are anagrams. An anagram is made by rearranging all the letters of one word to form another, using every letter exactly once.
The two input strings are already in lowercase and contain only alphabet letters.
1
If the lengths of the two strings are not equal, display the message: "Not anagrams: different lengths." and stop the program.
2
Use a while loop to go through each character of the first string one by one.
3
For each character in the first string, use another while loop to go through the second string character by character to find a match.
4
In the if condition inside the second loop, use == to compare characters. If a match is found, mark that match as used in a separate way (such as using a list or another string). If no match is found for any character, display the message: "Not anagrams: character mismatch." and stop. If all characters are matched, display: "The strings are anagrams."
5
str1 = "listen"
str2 = "silent"
# Expected output:
# The strings are anagrams.
7
Examples
str1 = "elbow"
str2 = "below"
8
More Examples
str1 = "dusty"
str2 = "study"
str1 = "night"
str2 = "thing"
How to convert a string into a list and remove an element using pop() by index number.
6
str1 = "listen"
str2 = "silent"
str2 = list(str2)
# Removes the character at index 2 ('l') from the list: ['s', 'i', 'e', 'n', 't']
str2.pop(2)
print(str2)
Examples
Python Nested Loops - Ex02
Check if Two Strings Are Anagrams Using While Loops
1.
Save your file as "ClassNumber_YourName_Ex03.py"
You are writing a Python script to determine whether two strings are anagrams. An anagram is made by rearranging all the letters of one word to form another, using every letter exactly once.
The two input strings are already in lowercase and contain only alphabet letters.
1
If the lengths of the two strings are not equal, display the message: "Not anagrams: different lengths." and stop the program.
2
Use a while loop to go through each character of the first string one by one.
3
For each character in the first string, use another while loop to go through the second string character by character to find a match.
4
In the if condition inside the second loop, use == to compare characters. If a match is found, mark that match as used in a separate way (such as using a list or another string). If no match is found for any character, display the message: "Not anagrams: character mismatch." and stop. If all characters are matched, display: "The strings are anagrams."
5
str1 = "listen"
str2 = "silent"
# Expected output:
# The strings are anagrams.
7
Examples
str1 = "elbow"
str2 = "below"
8
More Examples
str1 = "dusty"
str2 = "study"
str1 = "night"
str2 = "thing"
How to convert a string into a list and remove an element using pop() by index number.
6
str1 = "listen"
str2 = "silent"
str2 = list(str2)
# Removes the character at index 2 ('l') from the list: ['s', 'i', 'e', 'n', 't']
str2.pop(2)
print(str2)
Examples
Python Nested Loops - Ex04
Balanced Parentheses Check
1.
Save your file as "ClassNumber_YourName_Ex04.py"
Write a Python program to check if a given string of parentheses is balanced, using a while loop, append, and pop.
Use the fixed input string: input_str = "((())())()".
1
Use a while loop to go through each character in the string.
2
Use a list as a stack, adding '(' with append() and removing with pop() when ')' is found.
3
If the stack is empty when trying to pop(), or not empty at the end, print "Not Balanced", otherwise print "Balanced".
4
input_str = "((())())()"
# Output:
Balanced
Example
5
input_str = "((())(()"
# Output:
Not Balanced
Python Nested Loops - Ex04
Balanced Parentheses Check
1.
Save your file as "ClassNumber_YourName_Ex04.py"
Write a Python program to check if a given string of parentheses is balanced, using a while loop, append, and pop.
Use the fixed input string: input_str = "((())())()".
1
Use a while loop to go through each character in the string.
2
Use a list as a stack, adding '(' with append() and removing with pop() when ')' is found.
3
If the stack is empty when trying to pop(), or not empty at the end, print "Not Balanced", otherwise print "Balanced".
4
input_str = "((())())()"
# Output:
Balanced
Example
5
input_str = "((())(()"
# Output:
Not Balanced
F4 Lesson 11 - Problem Solving with Nested While Loops
By Mr Peter
F4 Lesson 11 - Problem Solving with Nested While Loops
- 199