Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Improvement
#3
(Mar-21-2024, 09:33 PM)deanhystad Wrote: What is the problem you are trying to solve?

If there are no restriction on PIN's other than they have 6 digits, you are already guessing the pin in the most efficient manner possible. If multi-processing is an option, you could break up the task into multiple ranges.

But I'm guessing there is more information in the problem description than you have shown.

Your program is structured wrong. You should not put the timer calls in the thing you want to time, and a function that guesses a pin should return the guessed pin number instead of printing the pin number. I would write the program like this:
from time import time


def find_pin(pin):
    # Validate input
    try:
        if len(pin) != 6:
            return None
        pin = int(pin)
        for guess in range(1000000):
             if guess == pin:
                 return pin
    except ValueError:
        pass
    return None


pin = input("Enter PIN: ")
start = time()
if find_pin(pin) is None:
    print("Entered pin is invalid")
else:
    print(f"Found pin in {time() - start} seconds.")
Thanks it's work but i'm open for improvement is it possible to increase the completion time even small amount? How to do it?
Reply


Messages In This Thread
Coding Improvement - by MoreMoney - Mar-21-2024, 11:12 AM
RE: ATM PIN Coding Improvement - by deanhystad - Mar-21-2024, 09:33 PM
RE: ATM PIN Coding Improvement - by MoreMoney - Mar-24-2024, 10:45 AM
RE: Coding Improvement - by snippsat - Mar-24-2024, 11:49 PM
RE: Coding Improvement - by MoreMoney - Mar-26-2024, 03:57 PM
RE: Coding Improvement - by snippsat - Mar-26-2024, 05:58 PM
RE: Coding Improvement - by MoreMoney - Mar-27-2024, 01:03 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020