summaryrefslogtreecommitdiff
path: root/simulation_test/Question1_N24.py
diff options
context:
space:
mode:
authorZhongheng Liu on Homelab <z.liu@outlook.com.gr>2025-04-29 15:00:38 +0300
committerZhongheng Liu on Homelab <z.liu@outlook.com.gr>2025-04-29 15:00:38 +0300
commit9de1e9c0e1bfa63f121676e83c58449543104ed6 (patch)
treec6b46e92d0953c82133f1489cab161d23a2c5e7a /simulation_test/Question1_N24.py
parent2f3657a6111cbb6ee51971eefbd0fcf36420c4cc (diff)
downloadcs-y13-9de1e9c0e1bfa63f121676e83c58449543104ed6.tar.gz
cs-y13-9de1e9c0e1bfa63f121676e83c58449543104ed6.tar.bz2
cs-y13-9de1e9c0e1bfa63f121676e83c58449543104ed6.zip
simulated paper 4 test on 29 apr 2025
Diffstat (limited to 'simulation_test/Question1_N24.py')
-rw-r--r--simulation_test/Question1_N24.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/simulation_test/Question1_N24.py b/simulation_test/Question1_N24.py
new file mode 100644
index 0000000..3e15e27
--- /dev/null
+++ b/simulation_test/Question1_N24.py
@@ -0,0 +1,36 @@
+# includes return type definition returning a list of strings
+def ReadData() -> list[str]:
+ with open("Data.txt", 'r') as file:
+ # Uses a Python list syntax to collect all lines,
+ # trim newline character, and assign to new lines variable
+ lines = [line.strip('\n') for line in file.readlines()]
+ return lines
+def FormatArray(string_array: list[str]) -> str:
+ # uses the str.join(iterator) built-in function for the str type
+ return " ".join(string_array)
+
+data = ReadData()
+print(FormatArray(data))
+
+def CompareStrings(first: str, second: str) -> int:
+ smallest = len(first) if len(first) < len(second) else len(second)
+ for i in range(smallest):
+ if first[i] < second[i]:
+ return 1
+ elif first[i] > second[i]:
+ return 2
+# type definitions for parameter and return value
+# takes a list of strings, returns a list of strings
+def Bubble(string_array: list[str]) -> list[str]:
+ for i in range(len(string_array)):
+ # have moved largest value to the end, shrink sort range by 1 each time
+ for j in range(len(string_array) - i - 1):
+ result = CompareStrings(string_array[j], string_array[j+1])
+ # if the two strings are in the wrong order
+ if result == 2:
+ swap = string_array[j+1]
+ string_array[j+1] = string_array[j]
+ string_array[j] = swap
+ return string_array
+sorted_array = Bubble(data)
+print(FormatArray(sorted_array)) \ No newline at end of file