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

MOP Coin Combo Finder

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Use Macanese coins of 1 MOP, 5 MOP, and 10 MOP to find all combinations that total exactly 63 MOP.

2

Use only coins of 1 MOP, 5 MOP, and 10 MOP denominations.

1

Each combination must total exactly 63 MOP.

4

Use exactly three nested while loops to generate the combinations.

5

Print each valid result in the format: 1*c1 + 5*c5 + 10*c10 = 63.

6

Output Example

3

No coin count may exceed 63.

1*63 + 5*0 + 10*0 = 63
1*58 + 5*1 + 10*0 = 63
1*53 + 5*2 + 10*0 = 63
1*48 + 5*3 + 10*0 = 63
1*43 + 5*4 + 10*0 = 63
1*38 + 5*5 + 10*0 = 63
1*33 + 5*6 + 10*0 = 63
1*28 + 5*7 + 10*0 = 63
1*23 + 5*8 + 10*0 = 63
1*18 + 5*9 + 10*0 = 63
...

Python Nested Loops - Ex01

MOP Coin Combo Finder

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Use Macanese coins of 1 MOP, 5 MOP, and 10 MOP to find all combinations that total exactly 63 MOP.

2

Use only coins of 1 MOP, 5 MOP, and 10 MOP denominations.

1

Each combination must total exactly 63 MOP.

4

Use exactly three nested while loops to generate the combinations.

5

Print each valid result in the format: 1*c1 + 5*c5 + 10*c10 = 63.

6

Output Example

3

No coin count may exceed 63.

1*63 + 5*0 + 10*0 = 63
1*58 + 5*1 + 10*0 = 63
1*53 + 5*2 + 10*0 = 63
1*48 + 5*3 + 10*0 = 63
1*43 + 5*4 + 10*0 = 63
1*38 + 5*5 + 10*0 = 63
1*33 + 5*6 + 10*0 = 63
1*28 + 5*7 + 10*0 = 63
1*23 + 5*8 + 10*0 = 63
1*18 + 5*9 + 10*0 = 63
...

Python Nested Loops - Ex02

Two-Hour Clock Coincidence

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Brute-force all second granularity times 00:00:0002:00:00 with loops h, m, s. Print those times.

2

Loop through all times from 00:00:00 up to and including 02:00:00, printing each second.

1

Use three nested loops over hour h (0–2), minute m (0–59), and second s (0–59), but stop exactly at 02:00:00.

4

Output Example

3

Print the time (in HH:MM:SS format)

00:00:00
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
...
01:59:57
01:59:58
01:59:59
02:00:00

Python Nested Loops - Ex02

Two-Hour Clock Coincidence

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Brute-force all second granularity times 00:00:0002:00:00 with loops h, m, s. Print those times.

2

Loop through all times from 00:00:00 up to and including 02:00:00, printing each second.

1

Use three nested loops over hour h (0–2), minute m (0–59), and second s (0–59), but stop exactly at 02:00:00.

4

Output Example

3

Print the time (in HH:MM:SS format)

00:00:00
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
...
01:59:57
01:59:58
01:59:59
02:00:00