summaryrefslogtreecommitdiff
path: root/algorithms
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2025-02-19 09:43:52 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2025-02-19 09:43:52 +0200
commit8834da60a01294fd509c7cebf3b129fcc378d152 (patch)
treea1c6c4bd71e95780f87d35240754c5b54d3042ae /algorithms
parent34bd7099d27656b4454015b0c410ca1713db5271 (diff)
downloadcs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.tar.gz
cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.tar.bz2
cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.zip
chore: changing to ipynb
Diffstat (limited to 'algorithms')
-rw-r--r--algorithms/binary_sort.py73
-rw-r--r--algorithms/bubble_sort.py39
-rw-r--r--algorithms/linear_search.py21
3 files changed, 0 insertions, 133 deletions
diff --git a/algorithms/binary_sort.py b/algorithms/binary_sort.py
deleted file mode 100644
index 2e71b52..0000000
--- a/algorithms/binary_sort.py
+++ /dev/null
@@ -1,73 +0,0 @@
-import random
-def generate_test_data(length: int, _range: tuple):
- arr = []
- for i in range(length):
- arr.append(random.randint(int(_range[0]), int(_range[1])))
- return arr
-def binsearch(arr, item):
- arr = sorted(arr)
- print(arr)
- ubound, lbound = len(arr) - 1, 0
- found = False
- while not found and lbound <= ubound:
- index = (lbound + ubound) // 2
- 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):
- found: bool = False
- index = 0
- while not found and index < len(arr):
- if (arr[index] == item):
- found = True
- break
- index += 1
- return (found, index)
-def linear_search(arr: list, item):
- index = 0
- for _item in arr:
- if _item == item:
- return (True, index)
- index += 1
- return False
-def test_existsness(samples: int):
- yes = 0
- no = 0
- for i in range(samples):
- query = random.randint(0, 100)
- array = generate_test_data(100, (0, 100))
- found = linear_search(array, query)
- if found:
- yes += 1
- else:
- 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: "))
- qstring = input("String item: ")
- binsearch(narr, query)
- found1 = linear_search(array, query)
- found2 = linear_search_while(array, query)
- if found1[0]:
- print("Found by for loop method ", found1[1])
- else: print("Not found.")
- if found2[0]:
- print("Found by while loop method ", found2[1])
- else:
- print("Not found.")
- test_existsness(100)
-if __name__ == "__main__":
- main()
diff --git a/algorithms/bubble_sort.py b/algorithms/bubble_sort.py
deleted file mode 100644
index f77ea08..0000000
--- a/algorithms/bubble_sort.py
+++ /dev/null
@@ -1,39 +0,0 @@
-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/algorithms/linear_search.py b/algorithms/linear_search.py
deleted file mode 100644
index ae198d2..0000000
--- a/algorithms/linear_search.py
+++ /dev/null
@@ -1,21 +0,0 @@
-import random
-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 linsearch(myList: list, valueToFind: int):
- mindex: int = len(myList) - 1
- index: int = 0
- found = False
- while not found and index <= mindex:
- if myList[index] == valueToFind:
- found = True
- index += 1
- if found: print("VALUE FOUND!!!")
- else: print("OH NO ITEM NOT FOUND IN LIST!!")
-def main():
- inputFind = int(input("Value to find: "))
- linsearch(generate_test_data(10, (0, 1000)), inputFind)
-if __name__ == "__main__":
- main()