summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2024-10-08 11:49:47 +0000
committerZhongheng Liu <z.liu@outlook.com.gr>2024-10-08 11:49:47 +0000
commit59197e3cad8623a72c6fe4d806fe5413166e4794 (patch)
treec3c36e305b02c6ec766ef6c9cc1a50a8aa0097b7
parenta530839efcf168edc2558e3faa2395845deae474 (diff)
downloadcs-y13-59197e3cad8623a72c6fe4d806fe5413166e4794.tar.gz
cs-y13-59197e3cad8623a72c6fe4d806fe5413166e4794.tar.bz2
cs-y13-59197e3cad8623a72c6fe4d806fe5413166e4794.zip
feat: add python code for lesson1 on 8.10.2024
-rw-r--r--lesson1.py29
-rw-r--r--main.py4
2 files changed, 29 insertions, 4 deletions
diff --git a/lesson1.py b/lesson1.py
new file mode 100644
index 0000000..a2f7741
--- /dev/null
+++ b/lesson1.py
@@ -0,0 +1,29 @@
+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 linear_search_while(arr: list, item):
+ found: bool = False
+ index = 0
+ while not found and index < len(arr):
+ if (arr[index] == item): found = True
+ index += 1
+ return found
+def linear_search(arr: list, item):
+ for _item in arr:
+ if _item == item:
+ return True
+ return False
+def main():
+ print("Hello world")
+ array = generate_test_data(10, (0, 100))
+ print(array)
+ query = int(input("Your query for item: "))
+ found1 = linear_search(array, query)
+ found2 = linear_search_while(array, query)
+ print("Found by for loop method") if found1 else print("Not found.")
+ print("Found by while loop method") if found2 else print("Not found.")
+if __name__ == "__main__":
+ main() \ No newline at end of file
diff --git a/main.py b/main.py
deleted file mode 100644
index 5b60048..0000000
--- a/main.py
+++ /dev/null
@@ -1,4 +0,0 @@
-def main():
- print("Hello world")
-if __name__ == "__main__":
- main() \ No newline at end of file