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

Password Brute-Force Simulation

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Simulate a brute-force checker for a 3-character numeric password using digits 0 ~ 9; check all combinations (e.g., "000", "001", ...).

2

Use three nested while loops to generate all possible 3-digit numeric passwords from "000" to "999".

1

Convert each password into a byte string (e.g., b"001") and compute its SHA-1 hash using hashlib.sha1().hexdigest().

import hashlib

text = "001"
sha1_hash = hashlib.sha1(text.encode('utf-8')).hexdigest()
print(sha1_hash)

3

Compare each generated hash to the known hash:

7a698699a9229b278afa72593214582d739b9bad

4

If a match is found, print the original 3-digit password (e.g., "425") and immediately stop all loops.

5

Print only the matched password on a new line, with no extra output.

Python Nested Loops - Ex01

Password Brute-Force Simulation

1.

Save your file as "ClassNumber_YourName_Ex01.py"

Simulate a brute-force checker for a 3-character numeric password using digits 0 ~ 9; check all combinations (e.g., "000", "001", ...).

2

Use three nested while loops to generate all possible 3-digit numeric passwords from "000" to "999".

1

Convert each password into a byte string (e.g., b"001") and compute its SHA-1 hash using hashlib.sha1().hexdigest().

import hashlib

text = "001"
sha1_hash = hashlib.sha1(text.encode('utf-8')).hexdigest()
print(sha1_hash)

3

Compare each generated hash to the known hash:

7a698699a9229b278afa72593214582d739b9bad

4

If a match is found, print the original 3-digit password (e.g., "425") and immediately stop all loops.

5

Print only the matched password on a new line, with no extra output.

Python Nested Loops - Ex02

Find Triplets with Pythagorean Condition

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Find all triplets (a, b, c) such that a2 + b2 = c2 and 1 ≤ a, b, c ≤ 30 .

The values for a, b, and c must each range from 1 to 30, inclusive.

1


3, 4, 5
5, 12, 13
6, 8, 10
7, 24, 25
8, 15, 17
9, 12, 15
10, 24, 26
12, 16, 20
15, 20, 25
18, 24, 30
20, 21, 29

You should only print combinations where a² + b² == c².

2

Ensure the program does not repeat the same triplet in different orders, e.g., "3, 4, 5" and "4, 3, 5" should only appear once.

3

Your output should be printed as a string in the format: 1, 2, 3

4

Final Output:

5

Python Nested Loops - Ex02

Find Triplets with Pythagorean Condition

1.

Save your file as "ClassNumber_YourName_Ex02.py"

Find all triplets (a, b, c) such that a2 + b2 = c2 and 1 ≤ a, b, c ≤ 30 .

The values for a, b, and c must each range from 1 to 30, inclusive.

1


3, 4, 5
5, 12, 13
6, 8, 10
7, 24, 25
8, 15, 17
9, 12, 15
10, 24, 26
12, 16, 20
15, 20, 25
18, 24, 30
20, 21, 29

You should only print combinations where a² + b² == c².

2

Ensure the program does not repeat the same triplet in different orders, e.g., "3, 4, 5" and "4, 3, 5" should only appear once.

3

Your output should be printed as a string in the format: 1, 2, 3

4

Final Output:

5