• Nov 19, 2022 •CodeCatch
0 likes • 4 views
""" Binary Search Algorithm ---------------------------------------- """ #iterative implementation of binary search in Python def binary_search(a_list, item): """Performs iterative binary search to find the position of an integer in a given, sorted, list. a_list -- sorted list of integers item -- integer you are searching for the position of """ first = 0 last = len(a_list) - 1 while first <= last: i = (first + last) / 2 if a_list[i] == item: return ' found at position '.format(item=item, i=i) elif a_list[i] > item: last = i - 1 elif a_list[i] < item: first = i + 1 else: return ' not found in the list'.format(item=item) #recursive implementation of binary search in Python def binary_search_recursive(a_list, item): """Performs recursive binary search of an integer in a given, sorted, list. a_list -- sorted list of integers item -- integer you are searching for the position of """ first = 0 last = len(a_list) - 1 if len(a_list) == 0: return ' was not found in the list'.format(item=item) else: i = (first + last) // 2 if item == a_list[i]: return ' found'.format(item=item) else: if a_list[i] < item: return binary_search_recursive(a_list[i+1:], item) else: return binary_search_recursive(a_list[:i], item)
• Sep 14, 2024 •rgannedo-6205
# Python binary search function def binary_search(arr, target): left = 0 right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 # Usage arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] target = 7 result = binary_search(arr, target) if result != -1: print(f"Element is present at index {result}") else: print("Element is not present in array")
• Feb 23, 2025 •hasnaoui1
0 likes • 7 views
print("hello world")
0 likes • 0 views
""" Number Guessing Game ---------------------------------------- """ import random attempts_list = [] def show_score(): if len(attempts_list) <= 0: print("There is currently no high score, it's yours for the taking!") else: print("The current high score is {} attempts".format(min(attempts_list))) def start_game(): random_number = int(random.randint(1, 10)) print("Hello traveler! Welcome to the game of guesses!") player_name = input("What is your name? ") wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name)) // Where the show_score function USED to be attempts = 0 show_score() while wanna_play.lower() == "yes": try: guess = input("Pick a number between 1 and 10 ") if int(guess) < 1 or int(guess) > 10: raise ValueError("Please guess a number within the given range") if int(guess) == random_number: print("Nice! You got it!") attempts += 1 attempts_list.append(attempts) print("It took you {} attempts".format(attempts)) play_again = input("Would you like to play again? (Enter Yes/No) ") attempts = 0 show_score() random_number = int(random.randint(1, 10)) if play_again.lower() == "no": print("That's cool, have a good one!") break elif int(guess) > random_number: print("It's lower") attempts += 1 elif int(guess) < random_number: print("It's higher") attempts += 1 except ValueError as err: print("Oh no!, that is not a valid value. Try again...") print("({})".format(err)) else: print("That's cool, have a good one!") if __name__ == '__main__': start_game()
• Mar 12, 2021 •mo_ak
0 likes • 1 view
prime_lists=[] # a list to store the prime numbers def prime(n): # define prime numbers if n <= 1: return False # divide n by 2... up to n-1 for i in range(2, n): if n % i == 0: # the remainder should'nt be a 0 return False else: prime_lists.append(n) return True for n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30 prime(n) check=0 # a var to limit the output to 10 only for n in prime_lists: for x in prime_lists: val= n *x if (val > 1000 ): check=check +1 if (check <10) : print("the num is:", val , "=",n , "* ", x ) break
def max_n(lst, n = 1): return sorted(lst, reverse = True)[:n] max_n([1, 2, 3]) # [3] max_n([1, 2, 3], 2) # [3, 2]