diff options
-rw-r--r-- | simulation_test/Data.txt | 45 | ||||
-rw-r--r-- | simulation_test/Question1_N24.py | 36 | ||||
-rw-r--r-- | simulation_test/Question2_N24.py | 72 | ||||
-rw-r--r-- | simulation_test/Question3_N24.py | 72 |
4 files changed, 225 insertions, 0 deletions
diff --git a/simulation_test/Data.txt b/simulation_test/Data.txt new file mode 100644 index 0000000..bb64450 --- /dev/null +++ b/simulation_test/Data.txt @@ -0,0 +1,45 @@ +beige
+green
+scarlet
+silver
+bronze
+slate
+yellow
+orange
+jade
+lavender
+magnolia
+magenta
+turquoise
+black
+grey
+russet
+maroon
+mango
+mint
+purple
+red
+pink
+white
+cream
+navy
+olive
+brown
+violet
+cyan
+amber
+aqua
+azure
+copper
+fawn
+fuschia
+gold
+indigo
+ivory
+mauve
+mulberry
+peach
+periwinkle
+plum
+rose
+sage
\ No newline at end of file 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 diff --git a/simulation_test/Question2_N24.py b/simulation_test/Question2_N24.py new file mode 100644 index 0000000..e5cfac5 --- /dev/null +++ b/simulation_test/Question2_N24.py @@ -0,0 +1,72 @@ +class Horse:
+ def __init__(self, Name: str, MaxFenceHeight: int, PercentageSuccess: int):
+ self.__Name: str = Name # String type
+ self.__MaxFenceHeight: int = MaxFenceHeight # Integer type
+ self.__PercentageSuccess: int = PercentageSuccess # Integer type
+ def GetName(self):
+ return self.__Name
+ def GetMaxFenceHeight(self):
+ return self.__MaxFenceHeight
+ def Success(self, fence_height: int, fence_risk: int):
+ if fence_height > self.__MaxFenceHeight:
+ return 0.2 * self.__PercentageSuccess
+ modifier = 0.6 + 0.1 * (5 - fence_risk)
+ return modifier * self.__PercentageSuccess
+# Python has dynamic sized arrays, here simulates static sizes by assigning None 2 times
+Horses = [None for i in range(2)]
+Horses[0] = Horse("Beauty", 150, 72)
+Horses[1] = Horse("Jet", 160, 65)
+for horse in Horses:
+ print(horse.GetName())
+
+class Fence:
+ def __init__(self, Height: int, Risk: int):
+ self.__Height: int = Height # Integer type
+ self.__Risk: int = Risk # Integer type
+ def GetHeight(self):
+ return self.__Height
+ def GetRisk(self):
+ return self.__Risk
+
+
+# creates array local to main program scope
+# with maximum 4 elements, initialised to None
+Course = [None for i in range(4)]
+
+for i in range(4):
+ fence_height = int(input(f"Fence {i + 1} height (70 to 180): "))
+ while fence_height > 180 or fence_height < 70:
+ # invalid height
+ print("Height invalid! 70 <= Height <= 180!")
+ fence_height = int(input(f"Fence {i + 1} height (70 to 180): "))
+ fence_risk = int(input(f"Fence {i + 1} risk (1-5): "))
+ while fence_risk > 5 or fence_risk < 1:
+ # invalid height
+ print("Risk invalid! 1 <= Risk <= 5!")
+ fence_height = int(input(f"Fence {i + 1} risk (1 to 5): "))
+ new_fence = Fence(fence_height, fence_risk)
+ Course[i] = new_fence
+
+# initialising averages tracker array
+Averages = [0 for i in range(len(Horses))]
+for i in range(len(Horses)):
+ total_chance = 0
+ for j in range(len(Course)):
+ success_chance = Horses[i].Success(Course[j].GetHeight(), Course[j].GetRisk())
+ total_chance += success_chance
+ display_percentage = round(success_chance)
+ message = f"Horse {Horses[i].GetName()} has a {display_percentage}% chance of jumping over fence {j + 1}!"
+ print(message)
+ average_chance = total_chance / len(Course)
+ Averages[i] = average_chance # store raw average chance in tracker array at corresponding index
+ display_average = round(average_chance) # pretty-print display average
+ print(f"The horse {Horses[i].GetName()} has a {display_average}% chance of successfully jumping over all fences!")
+
+# finding the horse with largest chance of winning
+largest_at = 0
+for i in range(len(Averages)):
+ # if there exists a new largest, replace index with new
+ if Averages[i] > Averages[largest_at]:
+ largest_at = i
+# print output, refer to horse at the largest_at index in Horses array
+print(f"The horse {Horses[largest_at].GetName()} has the highest average chance of success!")
\ No newline at end of file diff --git a/simulation_test/Question3_N24.py b/simulation_test/Question3_N24.py new file mode 100644 index 0000000..55f20b2 --- /dev/null +++ b/simulation_test/Question3_N24.py @@ -0,0 +1,72 @@ +# initialise linked list structure
+global LinkedList
+# creating empty data and joining pointers
+LinkedList = [[-1, i + 1] for i in range(0, 20)]
+# set last element to be null pointer
+LinkedList[-1][1] = -1
+global FirstEmpty
+global FirstNode
+FirstEmpty = 0
+FirstNode = -1
+
+def InsertData():
+ global FirstEmpty, FirstNode, LinkedList
+ for i in range(5):
+ number = int(input("your positive integer number: "))
+ while number < 0:
+ print("invalid, must be positive!")
+ number = int(input("your positive integer number: "))
+ if FirstEmpty != -1:
+ # the linked list is not full
+ NextEmpty = LinkedList[FirstEmpty][1]
+ LinkedList[FirstEmpty][0] = number
+ LinkedList[FirstEmpty][1] = FirstNode
+ FirstNode = FirstEmpty
+ FirstEmpty = NextEmpty
+ else:
+ return
+
+def OutputLinkedList():
+ global FirstNode, LinkedList
+ # assign initial node location
+ next_node = FirstNode
+ while next_node != -1:
+ # outputs the data at the given position
+ print(LinkedList[next_node][0])
+ # updates the next following pointer
+ next_node = LinkedList[next_node][1]
+
+InsertData()
+OutputLinkedList()
+
+def RemoveData(target_data):
+ global FirstNode, FirstEmpty, LinkedList
+ current_node = FirstNode
+ next_node = LinkedList[FirstNode][1]
+ if LinkedList[current_node][0] == target_data:
+ # the first node needs to be removed
+ new_first_node = LinkedList[current_node][1]
+ LinkedList[current_node][1] = FirstEmpty
+ FirstEmpty = current_node
+ FirstNode = new_first_node
+ return
+ while current_node != -1:
+ # checks if the immediately succeeding element is target
+ if LinkedList[next_node][0] == target_data:
+ if LinkedList[next_node][1] == -1:
+ # the target is at the end of the list
+ # terminating linked list with null pointer
+ LinkedList[current_node][1] = -1
+ # put element in empty list
+ else:
+ # the target is in the middle of the list
+ # skip the removed element
+ LinkedList[current_node][1] = LinkedList[next_node][1]
+ LinkedList[next_node][1] = FirstEmpty
+ FirstEmpty = next_node
+ return # returning here because only matching for one occurrence
+ current_node = next_node
+ next_node = LinkedList[next_node][1]
+RemoveData(5)
+print("After")
+OutputLinkedList()
\ No newline at end of file |