Advertisement
coding_giants

l16

Nov 16th, 2023
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. my_list = []
  3.  
  4. for i in range(20):
  5.     value = random.randint(1, 100)
  6.     my_list.append(value)
  7.  
  8. # [10, 5, 8, 3, 15, 12, 17, 4, 9, 6, 19, 2, 7, 14, 16, 18, 20, 13, 11, 1]
  9.  
  10. #========================================================
  11.  
  12. def bubble_sort(arr):
  13.     n = len(arr)
  14.     # iterate through all the elements in the list
  15.     for i in range(n):
  16.         # the last i elements are already sorted
  17.         for j in range(0, n-i-1):
  18.             # we compare adjacent elements
  19.             if arr[j] > arr[j+1]:
  20.                 # we swap the elements if the order is incorrect
  21.                 arr[j], arr[j+1] = arr[j+1], arr[j]
  22.     return arr
  23.  
  24. #========================================================
  25.  
  26. def linear_search(arr, x):
  27.     n = len(arr)
  28.     for i in range(n):
  29.         if arr[i] == x:
  30.             return i
  31.     return -1
  32.  
  33. #========================================================
  34.  
  35. def binary_search(arr, x):
  36.     low = 0
  37.     high = len(arr) - 1
  38.     mid = 0
  39.  
  40.     while low <= high:
  41.  
  42.         mid = (high + low) // 2
  43.  
  44.         if arr[mid] < x:
  45.             low = mid + 1
  46.  
  47.         elif arr[mid] > x:
  48.             high = mid - 1
  49.  
  50.         else:
  51.             return mid
  52.  
  53.     return -1
  54.  
  55. #========================================================
  56.  
  57. # bubble sort function
  58. def bubble_sort(arr):
  59.     n = len(arr)
  60.     for i in range(n):
  61.         for j in range(0, n-i-1):
  62.             if arr[j]["surname"] > arr[j+1]["surname"]:
  63.                 arr[j], arr[j+1] = arr[j+1], arr[j]
  64.     return arr
  65.  
  66. # contact list
  67. contacts = []
  68.  
  69. # program loop
  70. while True:
  71.     print("1. Add new contact ")
  72.     print("2. Display the contact list ")
  73.     print("3. Exit the program")
  74.     choice = input("Select an option: ")
  75.  
  76.     # adding a new contact
  77.     if choice == "1":
  78.         name = input("Enter a name: ")
  79.         surname = input("Enter a surname: ")
  80.         number = input("Enter a phone number: ")
  81.         contact = {"name": name, " surname": surname, "number": number}
  82.         contacts.append(contact)
  83.         contacts = bubble_sort(contacts)
  84.         print("A new contact was added.")
  85.  
  86.     # displaying the contact list
  87.     elif choice == "2":
  88.         if len(contacts) == 0:
  89.             print("No contacts.")
  90.         else:
  91.             print("Contact list:")
  92.             for contact in contacts:
  93.                 print(f"{contact['surname']} {contact['name']} - {contact['number']}")
  94.  
  95.     # exit the program
  96.     elif choice == "3":
  97.         break
  98.  
  99.     # error - unknown option
  100.     else:
  101.         print("Unknown option.")
  102.  
  103. #========================================================
  104.  
  105. import random
  106.  
  107. def binary_search(low, high, guess):
  108.     mid = (low + high) // 2
  109.     print("Is it the number", guess, "?")
  110.     response = input("Enter your answer (too small/too large/yes):")
  111.     if response == "too small":
  112.         return binary_search(mid + 1, high, (mid + 1 + high) // 2)
  113.     elif response == "too large":
  114.         return binary_search(low, mid - 1, (low + mid - 1) // 2)
  115.     else:
  116.         return guess
  117.  
  118. # draw a number in the range 1-100
  119. number = random.randint(1, 100)
  120. print("The number drawn is ", number)
  121.  
  122. # the computer guesses
  123. guess = 50
  124. result = binary_search(1, 100, guess)
  125. print("I guessed! The number drawn is ", result)
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement