diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3bbe7b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +*.pyo diff --git a/binary_search.py b/binary_search.py index d93aef4..0b3de7e 100644 --- a/binary_search.py +++ b/binary_search.py @@ -24,6 +24,13 @@ # But we can make this *faster* by leveraging the fact that our array is sorted! # Binary search ~ O(log(n)), naive search ~ O(n) +def get_valid_integer(prompt): + while True: + try: + return int(input(prompt)) + except ValueError: + print("Invalid input. Please enter an integer.") + # In these two examples, l is a list in ascending order, and target is something that we're looking for # Return -1 if not found @@ -66,26 +73,50 @@ def binary_search(l, target, low=None, high=None): return binary_search(l, target, midpoint+1, high) if __name__=='__main__': - # l = [1, 3, 5, 10, 12] - # target = 7 - # print(naive_search(l, target)) - # print(binary_search(l, target)) - - length = 10000 - # build a sorted list of length 10000 - sorted_list = set() - while len(sorted_list) < length: - sorted_list.add(random.randint(-3*length, 3*length)) - sorted_list = sorted(list(sorted_list)) - - start = time.time() - for target in sorted_list: - naive_search(sorted_list, target) - end = time.time() - print("Naive search time: ", (end - start), "seconds") - - start = time.time() - for target in sorted_list: - binary_search(sorted_list, target) - end = time.time() - print("Binary search time: ", (end - start), "seconds") + try: + length = 10000 + # build a sorted list of length 10000 + sorted_list = sorted(random.sample(range(-3*length, 3*length), length)) + + print("Search conducted in repository: kying18/beginner-projects\n") + print("A sorted list of random integers has been generated.") + print("You will continue entering numbers until one is found in the list.\n") + while True: + target = get_valid_integer("Enter a number to search for: ") + result = binary_search(sorted_list, target) + + if result != -1: + print(f"Target {target} found at index {result}.") + + # Time comparison between naive and binary search + start = time.time() + naive_search(sorted_list, target) + end = time.time() + naive_time = end - start + + start = time.time() + binary_search(sorted_list, target) + end = time.time() + binary_time = end - start + + print(f"Naive search time: {naive_time:.8f} seconds") + print(f"Binary search time: {binary_time:.8f} seconds") + print("Binary search is faster due to its O(log n) complexity.\n") + else: + print(f"Target {target} not found in the list.") + + # Ask if the user wants to continue comparing searches + while True: + choice = input("\nWould you like to continue comparing searches? (y/n): ").strip().lower() + if choice in ['y', 'yes']: + print("\nContinuing search...\n") + break # exit inner loop, continue outer loop + elif choice in ['n', 'no']: + print("\nExiting program. Thank you for using the search comparison tool.") + raise SystemExit + else: + print("Invalid input. Please enter 'Y' or 'N'.") + + except Exception as e: + print(f"An unexpected error occurred: {e}") + diff --git a/login.py b/login.py index e816eca..1fcb274 100644 --- a/login.py +++ b/login.py @@ -8,29 +8,87 @@ Website: https://www.kylieying.com Github: https://www.github.com/kying18 Programmer Beast Mode Spotify playlist: https://open.spotify.com/playlist/4Akns5EUb3gzmlXIdsJkPs?si=qGc4ubKRRYmPHAJAIrCxVQ -""" +How to run: + python login.py + +Credentials are read from environment variables (no plaintext secrets in code): + export LOGIN_USERNAME=kylie + export LOGIN_PASSWORD=secretpassword + +If either variable is not set, the script will print instructions and exit. +""" +import getpass +import os +import sys +# Time module offers time values, timestamps, delays, +# and performance measurements. import time -username = 'kylie' -password = 'secretpassword' +# login function for more modularity. +# expected_username and expected_password receive the credentials at call time. +# username_input_fn and password_input_fn can be replaced in unit tests +# to avoid interactive prompts. +def login(expected_username, expected_password, + username_input_fn=input, password_input_fn=getpass.getpass): + # Two user inputs for Username and Password + username_input = username_input_fn('Username: ') + password_input = password_input_fn('Password: ') + # if user input (Username) is exact to defined username variable and + # if user input (Password) is exact to defined password variable + # proceed with if statement. + if username_input == expected_username and password_input == expected_password: + print('Access granted') # Print shown statements with pauses inbetween + print('Please wait') + time.sleep(5) # Pause program execution for 5 seconds + print('Ok... Loading...') + time.sleep(1) # Pause program execution for 1 second + print('...') + time.sleep(1) # Pause program execution for 1 second + print('...') # Gain access to "secret mainframe" + print('Alright you have security clearance. Pulling up the secret mainframe.') + # else if statement for incorrect password input prints Password incorrect. + elif username_input == expected_username and password_input != expected_password: + print('Password incorrect') + # else if statement for incorrect username input prints Username incorrect. + elif username_input != expected_username and password_input == expected_password: + print('Username incorrect') + # else (lastly) let user know username and password are incorrect. + else: + print('You might wanna check both fields...') -username_input = input('Username: ') -password_input = input('Password: ') +def main(username, password): + # Introduction to secret mainframe to identify username and + # password. + print("==============================================") + print(" WELCOME TO THE ASTRAL MAINFRAME v3.7") + print("==============================================") + time.sleep(1) + print("\nBefore you lies a terminal rumored to guard the") + print("ancient secrets of the digital realm. Only those") + print("who can decipher the clues may enter.") + time.sleep(2) + print("\nA whisper echoes from the machine:") + print('"The key is hidden in plain sight… if you can') + print(' read between the lines, you already know."') + time.sleep(3) + print("\nA faint note appears on the screen:") + print(" - The guardian favors a name that starts with 'k' and ends with 'ylie'.") + print(" - The password? Well… some say it's the most *secret* thing of all.") + time.sleep(3) + print("\nType the credentials to prove your worth.\n") + login(username, password) -if username_input == username and password_input == password: - print('Access granted') - print('Please wait') - time.sleep(5) - print('Ok... Loading...') - time.sleep(1) - print('...') - time.sleep(1) - print('...') - print('Alright you have security clearance. Pulling up the secret mainframe.') -elif username_input == username and password_input != password: - print('Password incorrect') -elif username_input != username and password_input == password: - print('Username incorrect') -else: - print('You might wanna check both fields...') \ No newline at end of file +# Execute program +if __name__ == "__main__": + # Load credentials from environment variables (no plaintext secrets in source). + # Verify they are set before starting the interactive session. + _username = os.environ.get('LOGIN_USERNAME') + _password = os.environ.get('LOGIN_PASSWORD') + if not _username or not _password: + print("Error: credentials not configured.") + print("Please set the LOGIN_USERNAME and LOGIN_PASSWORD environment variables:") + print(" export LOGIN_USERNAME=") + print(" export LOGIN_PASSWORD=") + sys.exit(1) + main(_username, _password) \ No newline at end of file