summaryrefslogtreecommitdiff
path: root/lesson2.py
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 /lesson2.py
parent71d24d186871d3a76acdf13c7ad194fbc6613bf9 (diff)
downloadcs-y13-d7ff01b21b4983da621cdb5746dfd6999b9b5ab7.tar.gz
cs-y13-d7ff01b21b4983da621cdb5746dfd6999b9b5ab7.tar.bz2
cs-y13-d7ff01b21b4983da621cdb5746dfd6999b9b5ab7.zip
feat: working linear search and bubble sort
Diffstat (limited to 'lesson2.py')
-rw-r--r--lesson2.py39
1 files changed, 39 insertions, 0 deletions
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