Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Improvement
#1
Trying to make completion time faster and stable, anyone have better code for that?

Here is my try

import time

def find_pin(pin):
    start_time = time.time()
    pin_int = int(pin)
    for guess in range(1000000):
        if guess == pin_int:
            end_time = time.time()
            print(f"PIN found: {guess:06}")
            print(f"Completion time: {end_time - start_time} seconds")
            break

# Example usage:
pin_input = input("Enter a 6-digit PIN: ")
find_pin(pin_input)
buran write Mar-21-2024, 02:41 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.")
MoreMoney likes this post
Reply
#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
#4
(Mar-24-2024, 10:45 AM)MoreMoney Wrote: 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?
There are many ways algorithms can do some improvement,but for big leap can use eg Numba, Cython, PyPy.
As this is homework i guess is more the algorithms rute.
To show a example with Numba.
from numba import jit
import time

@jit(nopython=True)
def find_pin_numba(target_pin):
    for guess in range(1000000000):
        if guess == target_pin:
            return True
    return False

def main():
    pin_input = input("Enter PIN: ")
    # Validate input
    if not pin_input.isdigit() or len(pin_input) != 9:
        print("Entered PIN is invalid.")
        return
    target_pin = int(pin_input)
    start = time.time()
    if find_pin_numba(target_pin):
        print(f"Found PIN in {time.time() - start} seconds.")
    else:
        print("PIN not found.")

if __name__ == "__main__":
    main()
# Python
G:\div_code\po_env
λ python fast_1.py
Enter PIN: 999999999
Found pin in 46.00567603111267 seconds.

# Numba
G:\div_code\po_env
λ python fast_numba.py
Enter PIN: 999999999
Found PIN in 0.43939685821533203 seconds.
So as you see that speed goes down from 46-sec standar Python to 0.4-sec with Numba.
The only change is is comment out line 5.
#@jit(nopython=True)
MoreMoney likes this post
Reply
#5
(Mar-24-2024, 11:49 PM)snippsat Wrote:
(Mar-24-2024, 10:45 AM)MoreMoney Wrote: 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?
There are many ways algorithms can do some improvement,but for big leap can use eg Numba, Cython, PyPy.
As this is homework i guess is more the algorithms rute.
To show a example with Numba.
from numba import jit
import time

@jit(nopython=True)
def find_pin_numba(target_pin):
    for guess in range(1000000000):
        if guess == target_pin:
            return True
    return False

def main():
    pin_input = input("Enter PIN: ")
    # Validate input
    if not pin_input.isdigit() or len(pin_input) != 9:
        print("Entered PIN is invalid.")
        return
    target_pin = int(pin_input)
    start = time.time()
    if find_pin_numba(target_pin):
        print(f"Found PIN in {time.time() - start} seconds.")
    else:
        print("PIN not found.")

if __name__ == "__main__":
    main()
# Python
G:\div_code\po_env
λ python fast_1.py
Enter PIN: 999999999
Found pin in 46.00567603111267 seconds.

# Numba
G:\div_code\po_env
λ python fast_numba.py
Enter PIN: 999999999
Found PIN in 0.43939685821533203 seconds.
So as you see that speed goes down from 46-sec standar Python to 0.4-sec with Numba.
The only change is is comment out line 5.
#@jit(nopython=True)

Thank you for your help, but i have question i just need 6 atm digit for my homework should i decrase the range for faster completion time, or they dont make any different? Thank you

Also how to install numba properly?
Reply
#6
(Mar-26-2024, 03:57 PM)MoreMoney Wrote: Thank you for your help, but i have question i just need 6 atm digit for my homework should i decrase the range for faster completion time, or they dont make any different? Thank you

Also how to install numba properly?
It was more a example on how to speed up code a lot,i don't think you should give Numba code as a solution.
Without the ability to use external libraries or multiprocessing,and given the straightforward nature of a brute-force search,
there are limitations to how much the efficiency of this algorithm can be improved with pure Python.
Can clean it up a little.
from time import time

def find_pin(target_pin):
    for guess in range(1000000):
        if guess == target_pin:
            return True
    return False

def valid_pin(pin):
    return pin.isdigit() and len(pin) == 6

pin_input = input("Enter PIN: ")
start = time()
if not valid_pin(pin_input):
    print("Entered PIN is invalid")
else:
    target_pin = int(pin_input)
    if find_pin(target_pin):
        print(f"Found PIN in {time() - start} seconds.")
    else:
        print("PIN not found.")
Numba you install with pip.
MoreMoney likes this post
Reply
#7
(Mar-26-2024, 05:58 PM)snippsat Wrote:
(Mar-26-2024, 03:57 PM)MoreMoney Wrote: Thank you for your help, but i have question i just need 6 atm digit for my homework should i decrase the range for faster completion time, or they dont make any different? Thank you

Also how to install numba properly?
It was more a example on how to speed up code a lot,i don't think you should give Numba code as a solution.
Without the ability to use external libraries or multiprocessing,and given the straightforward nature of a brute-force search,
there are limitations to how much the efficiency of this algorithm can be improved with pure Python.
Can clean it up a little.
from time import time

def find_pin(target_pin):
    for guess in range(1000000):
        if guess == target_pin:
            return True
    return False

def valid_pin(pin):
    return pin.isdigit() and len(pin) == 6

pin_input = input("Enter PIN: ")
start = time()
if not valid_pin(pin_input):
    print("Entered PIN is invalid")
else:
    target_pin = int(pin_input)
    if find_pin(target_pin):
        print(f"Found PIN in {time() - start} seconds.")
    else:
        print("PIN not found.")
Numba you install with pip.
Thank you very much for helping with my code, i'd really appreciate it
Now my code is better

Would you like to help me with my other coding i'm stuck with?
It's about logic improvement

(Sorting Steps)
https://python-forum.io/thread-41845.html

Thank you
Reply


Forum Jump:

User Panel Messages

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