summaryrefslogtreecommitdiff
path: root/simulation_test/Question1_N24.py
diff options
context:
space:
mode:
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