diff options
-rw-r--r-- | lesson1.py | 14 | ||||
-rw-r--r-- | lesson2.py | 39 | ||||
-rw-r--r-- | main.py | 0 |
3 files changed, 48 insertions, 5 deletions
@@ -11,14 +11,16 @@ def binsearch(arr: list, item): found = False while not found and lbound <= ubound: index = (lbound + ubound) // 2 - if lbound == ubound: - if arr[index] == item: - found = True - print("Found here") + if arr[index] == item: + found = True + print("Found here") + return found if arr[index] > item: ubound = index - 1 if arr[index] < item: lbound = index + 1 + print("not found") + return found def linear_search_while(arr: list, item): @@ -50,11 +52,13 @@ def test_existsness(samples: int): no += 1 print(f"In {samples} samples, {yes} match, {no} don't") def main(): + narr = sorted(["A", "B", "C", "D", "E"]) print("Hello world") array = generate_test_data(10, (0, 100)) print(array) query = int(input("Your query for item: ")) - binsearch(array, query) + qstring = input("String item: ") + binsearch(narr, query) found1 = linear_search(array, query) found2 = linear_search_while(array, query) if found1[0]: diff --git a/lesson2.py b/lesson2.py new file mode 100644 index 0000000..f77ea08 --- /dev/null +++ b/lesson2.py @@ -0,0 +1,39 @@ +import random +import string +CHARS = list(string.ascii_lowercase) +print(CHARS) +def generate_test_data(length: int, _range: tuple): + arr = [] + for i in range(length): + arr.append(random.randint(_range[0], _range[1])) + return arr +def bubsort(arr: list): + for i in range(len(arr)): + # print(arr) + changed = False + for j in range(len(arr) - i - 1): + if arr[j] > arr[j+1]: + swp = arr[j+1] + arr[j+1] = arr[j] + arr[j] = swp + changed = True + if not changed: return arr +def testarr_string(length: int): + arr = [] + for i in range(length): + string = "" + for i in range(10): + string += CHARS[random.randint(0,len(CHARS) - 1)] + arr.append(string) + print(arr) + return arr +def bubsort_reliability(samples: int): + y = 0 + n = 0 + for _ in range(samples): + test = generate_test_data(100, (0, 100)) + if bubsort(test) == sorted(test): y += 1 + else: n += 1 + if y == samples: print(f"{samples} checks passed!") +bubsort_reliability(100) +print(bubsort(testarr_string(10)))
\ No newline at end of file |