summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2024-10-09 06:41:52 +0000
committerZhongheng Liu <z.liu@outlook.com.gr>2024-10-09 06:41:52 +0000
commitd7ff01b21b4983da621cdb5746dfd6999b9b5ab7 (patch)
treed4cf088917bfd2c926ef5e833a4bdfd104b43568
parent71d24d186871d3a76acdf13c7ad194fbc6613bf9 (diff)
downloadcs-y13-d7ff01b21b4983da621cdb5746dfd6999b9b5ab7.tar.gz
cs-y13-d7ff01b21b4983da621cdb5746dfd6999b9b5ab7.tar.bz2
cs-y13-d7ff01b21b4983da621cdb5746dfd6999b9b5ab7.zip
feat: working linear search and bubble sort
-rw-r--r--lesson1.py14
-rw-r--r--lesson2.py39
-rw-r--r--main.py0
3 files changed, 48 insertions, 5 deletions
diff --git a/lesson1.py b/lesson1.py
index c2c59f4..052ec27 100644
--- a/lesson1.py
+++ b/lesson1.py
@@ -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
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/main.py