Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- my_list = []
- for i in range(20):
- value = random.randint(1, 100)
- my_list.append(value)
- # [10, 5, 8, 3, 15, 12, 17, 4, 9, 6, 19, 2, 7, 14, 16, 18, 20, 13, 11, 1]
- #========================================================
- def bubble_sort(arr):
- n = len(arr)
- # iterate through all the elements in the list
- for i in range(n):
- # the last i elements are already sorted
- for j in range(0, n-i-1):
- # we compare adjacent elements
- if arr[j] > arr[j+1]:
- # we swap the elements if the order is incorrect
- arr[j], arr[j+1] = arr[j+1], arr[j]
- return arr
- #========================================================
- def linear_search(arr, x):
- n = len(arr)
- for i in range(n):
- if arr[i] == x:
- return i
- return -1
- #========================================================
- def binary_search(arr, x):
- low = 0
- high = len(arr) - 1
- mid = 0
- while low <= high:
- mid = (high + low) // 2
- if arr[mid] < x:
- low = mid + 1
- elif arr[mid] > x:
- high = mid - 1
- else:
- return mid
- return -1
- #========================================================
- # bubble sort function
- def bubble_sort(arr):
- n = len(arr)
- for i in range(n):
- for j in range(0, n-i-1):
- if arr[j]["surname"] > arr[j+1]["surname"]:
- arr[j], arr[j+1] = arr[j+1], arr[j]
- return arr
- # contact list
- contacts = []
- # program loop
- while True:
- print("1. Add new contact ")
- print("2. Display the contact list ")
- print("3. Exit the program")
- choice = input("Select an option: ")
- # adding a new contact
- if choice == "1":
- name = input("Enter a name: ")
- surname = input("Enter a surname: ")
- number = input("Enter a phone number: ")
- contact = {"name": name, " surname": surname, "number": number}
- contacts.append(contact)
- contacts = bubble_sort(contacts)
- print("A new contact was added.")
- # displaying the contact list
- elif choice == "2":
- if len(contacts) == 0:
- print("No contacts.")
- else:
- print("Contact list:")
- for contact in contacts:
- print(f"{contact['surname']} {contact['name']} - {contact['number']}")
- # exit the program
- elif choice == "3":
- break
- # error - unknown option
- else:
- print("Unknown option.")
- #========================================================
- import random
- def binary_search(low, high, guess):
- mid = (low + high) // 2
- print("Is it the number", guess, "?")
- response = input("Enter your answer (too small/too large/yes):")
- if response == "too small":
- return binary_search(mid + 1, high, (mid + 1 + high) // 2)
- elif response == "too large":
- return binary_search(low, mid - 1, (low + mid - 1) // 2)
- else:
- return guess
- # draw a number in the range 1-100
- number = random.randint(1, 100)
- print("The number drawn is ", number)
- # the computer guesses
- guess = 50
- result = binary_search(1, 100, guess)
- print("I guessed! The number drawn is ", result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement