diff options
author | Zhongheng Liu on Homelab <z.liu@outlook.com.gr> | 2025-03-22 20:52:50 +0200 |
---|---|---|
committer | Zhongheng Liu on Homelab <z.liu@outlook.com.gr> | 2025-03-22 20:52:50 +0200 |
commit | 2f3657a6111cbb6ee51971eefbd0fcf36420c4cc (patch) | |
tree | 616b65ccdc10cc3bc559b28365a947c3e9eace72 | |
parent | 9b2d46cc2a19f964e7c483c54c6a731422336419 (diff) | |
download | cs-y13-2f3657a6111cbb6ee51971eefbd0fcf36420c4cc.tar.gz cs-y13-2f3657a6111cbb6ee51971eefbd0fcf36420c4cc.tar.bz2 cs-y13-2f3657a6111cbb6ee51971eefbd0fcf36420c4cc.zip |
9618-mj-2021
-rw-r--r-- | 9618-41-mj-2021/TreasureChestData.txt | 15 | ||||
-rw-r--r-- | 9618-41-mj-2021/question1.py | 44 | ||||
-rw-r--r-- | 9618-41-mj-2021/question2.py | 27 | ||||
-rw-r--r-- | 9618-41-mj-2021/question3.py | 50 |
4 files changed, 136 insertions, 0 deletions
diff --git a/9618-41-mj-2021/TreasureChestData.txt b/9618-41-mj-2021/TreasureChestData.txt new file mode 100644 index 0000000..d676fc8 --- /dev/null +++ b/9618-41-mj-2021/TreasureChestData.txt @@ -0,0 +1,15 @@ +2*2 +4 +10 +100/10 +10 +15 +1000*4 +4000 +20 +125/25 +5 +30 +3000+4000 +7000 +18 diff --git a/9618-41-mj-2021/question1.py b/9618-41-mj-2021/question1.py new file mode 100644 index 0000000..61f1325 --- /dev/null +++ b/9618-41-mj-2021/question1.py @@ -0,0 +1,44 @@ +#1a +class node: + def __init__(self): + self.data = None # integer type, none for empty value + self.nextNode = None # integer type, none for empty value +#1b +linkedList = [node() for i in range(10)] +startPointer = 0 +emptyList = 5 +newData = [1, 5, 6, 7, 2, 0, 0, 56, 0, 0] +newNextNode = [1, 4, 7, -1, 2, 6, 8, 3, 9, -1] +for i in range(len(linkedList)): + linkedList[i].data = newData[i] + linkedList[i].nextNode = newNextNode[i] +#1ci +def outputNodes(array: list[node], startPointer: int): + print(array[startPointer].data) + nextItemIndex = array[startPointer].nextNode + while nextItemIndex != -1: + print(array[nextItemIndex].data) + nextItemIndex = array[nextItemIndex].nextNode +#1cii +outputNodes(linkedList, startPointer) +#1di +def addNode(): + global linkedList + global startPointer + global emptyList + inputData = int(input("New data: ")) + if emptyList == -1: + return False + linkedList[emptyList].data = inputData + nextEmptyList = linkedList[emptyList].nextNode + linkedList[emptyList].nextNode = startPointer + startPointer = emptyList + emptyList = nextEmptyList + return True +#1dii +success = addNode() +if success == True: + print("Successfully added a new node.") +else: + print("Failed to add the node, array is full.") +outputNodes(linkedList, startPointer)
\ No newline at end of file diff --git a/9618-41-mj-2021/question2.py b/9618-41-mj-2021/question2.py new file mode 100644 index 0000000..abbe1ea --- /dev/null +++ b/9618-41-mj-2021/question2.py @@ -0,0 +1,27 @@ +#2a +global arrayData +arrayData = [10, 5, 6, 7, 1, 12, 13, 15, 21, 8] +#2bi +def linearSearch(searchValue: int): + for item in arrayData: + if item == searchValue: + return True + return False +#2bii +valueToSearch = int(input("Your value to search for: ")) +found = linearSearch(valueToSearch) +if found: + print("Value has been found") +else: + print("Value can't be found") +#2c +def bubbleSort(): + for x in range(0, len(arrayData)): + for y in range(0, len(arrayData) - x - 1): + if arrayData[y] < arrayData[y + 1]: + temp = arrayData[y] + arrayData[y] = arrayData[y + 1] + arrayData[y + 1] = temp +print(arrayData) +bubbleSort() +print(arrayData)
\ No newline at end of file diff --git a/9618-41-mj-2021/question3.py b/9618-41-mj-2021/question3.py new file mode 100644 index 0000000..42a991b --- /dev/null +++ b/9618-41-mj-2021/question3.py @@ -0,0 +1,50 @@ +#3a +class TreasureChest: + def __init__(self, question: str, answer: int, points: int): + self.__question = question # String + self.__answer = answer # Integer + self.__points = points # Integer + #3ci + def getQuestion(self): + return self.__question + #3cii + def checkAnswer(self, user_answer: int): + if self.__answer == user_answer: + return True + else: + return False + #3ciii + def getPoints(self, numberOfAttempts: int): + if numberOfAttempts == 1: return self.__points + if numberOfAttempts == 2: return self.__points // 2 + if numberOfAttempts == 3 or numberOfAttempts == 4: return self.__points // 4 + return 0 +#3b +def readData(): + TREASURE_CHEST_DATA_FILENAME = "TreasureChestData.txt" + try: + with open(TREASURE_CHEST_DATA_FILENAME, 'r') as file: + global arrayTreasure + arrayTreasure = [None for i in range(5)] # array of TreasureChest + for i in range(5): + question = file.readline().strip() + answer = int(file.readline().strip()) + points = int(file.readline().strip()) + arrayTreasure[i] = TreasureChest(question, answer, points) + file.close() + except IOError: + print("Error: the file cannot be found") + +#3civ +readData() +questionNumber = int(input("Input a question number (1 to 5): ")) +while questionNumber < 1 or questionNumber > 5: + questionNumber = int(input("Input a question number (1 to 5): ")) +print(arrayTreasure[questionNumber - 1].getQuestion()) +nAttempts = 1 +answerInput = int(input(f"[Attempt {nAttempts}] Your answer: ")) +while not arrayTreasure[questionNumber - 1].checkAnswer(answerInput): + answerInput = int(input(f"[Attempt {nAttempts}] Your answer: ")) + nAttempts += 1 +points = arrayTreasure[questionNumber - 1].getPoints(nAttempts) +print(f"You are awarded {points} points")
\ No newline at end of file |