blob: 3e15e27ece2b131a8fcc7ec6762d613970710ab5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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))
|