You have a ZIP file protected by a 4-digit PIN code (0000–9999). You need to brute-force the password.
Write a function try_password(password) that:
- Checks whether the password is correct (for testing purposes, the correct password is
"1234"). - If the password is correct, prints
Password found: 1234and returnsTrue.
- Iterate through all passwords from 0000 to 9999 in a single thread.
- Measure execution time.
Split the same range into 4 parts:
- Thread 1:
0000–2499 - Thread 2:
2500–4999 - Thread 3:
5000–7499 - Thread 4:
7500–9999
- As soon as one thread finds the password, stop all other threads.
- Use
threading.Event()for synchronization.
- The 4-thread version runs faster than the single-threaded version.
- Once the password is found, the program stops brute-forcing immediately.