summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu on Homelab <z.liu@outlook.com.gr>2025-03-13 14:28:29 +0200
committerZhongheng Liu on Homelab <z.liu@outlook.com.gr>2025-03-13 14:28:29 +0200
commitac44ac75508b72ca63e08875a623f992fe372fc2 (patch)
tree2fe4c0c9df491cb77f94c389abb35d17a0b02cb8
parent2384d8d8898d7a2edddec61366b697f9816078cd (diff)
downloadcs-y13-ac44ac75508b72ca63e08875a623f992fe372fc2.tar.gz
cs-y13-ac44ac75508b72ca63e08875a623f992fe372fc2.tar.bz2
cs-y13-ac44ac75508b72ca63e08875a623f992fe372fc2.zip
feat: add question 2 and 3
-rw-r--r--9618-42-mj-2022/CardValues.txt60
-rw-r--r--9618-42-mj-2022/Question2_J22.py22
-rw-r--r--9618-42-mj-2022/Question3_J22.py50
-rw-r--r--notebooks/dictionaries.ipynb2
4 files changed, 131 insertions, 3 deletions
diff --git a/9618-42-mj-2022/CardValues.txt b/9618-42-mj-2022/CardValues.txt
new file mode 100644
index 0000000..7965d0a
--- /dev/null
+++ b/9618-42-mj-2022/CardValues.txt
@@ -0,0 +1,60 @@
+1
+red
+5
+black
+2
+white
+4
+red
+9
+green
+6
+black
+7
+purple
+8
+red
+9
+orange
+10
+red
+1
+black
+2
+orange
+3
+yellow
+4
+purple
+5
+green
+6
+yellow
+7
+yellow
+8
+white
+9
+white
+10
+yellow
+1
+grey
+2
+blue
+3
+black
+4
+blue
+5
+blue
+6
+purple
+7
+grey
+8
+blue
+9
+blue
+10
+orange \ No newline at end of file
diff --git a/9618-42-mj-2022/Question2_J22.py b/9618-42-mj-2022/Question2_J22.py
index 17db560..d514542 100644
--- a/9618-42-mj-2022/Question2_J22.py
+++ b/9618-42-mj-2022/Question2_J22.py
@@ -30,16 +30,34 @@ def OutputAllElements():
OutputAllElements()
# 2 c i
+
+# Bugfix included
+# Originally, was "if Upper >= Lower"
+# Does not work due to max recursion depth exceeding
+
def BinarySearch(SearchArray: list, Lower: int, Upper: int, SearchValue: int) -> int:
- if Upper >= Lower:
+ if Upper > Lower:
Mid = (Lower + (Upper - 1)) // 2
if SearchArray[0][Mid] == SearchValue:
return Mid
else:
if SearchArray[0][Mid] > SearchValue:
+ print(f"Searching: {SearchArray[0][Lower:Mid-1]}")
+ input()
return BinarySearch(SearchArray, Lower, Mid - 1, SearchValue)
else:
+ print(f"Searching: {SearchArray[0][Mid+1:Upper]}")
+ input()
return BinarySearch(SearchArray, Mid + 1, Upper, SearchValue)
+ elif Upper == Lower:
+ return -1 if SearchArray[0][Upper] != SearchValue else Upper
return -1
-BinarySearch(ArrayData,) \ No newline at end of file
+# a value dynamically input by user, since ArrayData is randomly generated
+# target = int(input("Search for a value in first line of array: "))
+not_exists = -50 # a value that definitely does not exist in ArrayData
+
+find1 = BinarySearch(ArrayData, 0, 10, ArrayData[0][6])
+find2 = BinarySearch(ArrayData, 0, 10, not_exists)
+print(find1)
+print(find2) \ No newline at end of file
diff --git a/9618-42-mj-2022/Question3_J22.py b/9618-42-mj-2022/Question3_J22.py
new file mode 100644
index 0000000..764f4c0
--- /dev/null
+++ b/9618-42-mj-2022/Question3_J22.py
@@ -0,0 +1,50 @@
+# 3 a
+class Card:
+ def __init__(self, number: int, colour: str):
+ self.__Number = number # Integer
+ self.__Colour = colour # String
+
+ # 3 b
+ def GetNumber(self):
+ return self.__Number
+ def GetColour(self):
+ return self.__Colour
+
+# 3 c
+TEXT_FILE_NAME = "CardValues.txt"
+cards = [None for i in range(30)] # type is Card, use NoneType to indicate empty
+with open(TEXT_FILE_NAME, "r") as file:
+ i = 0
+ j = 0
+ text_lines = file.readlines()
+ #print(text_lines)
+ while i < 30:
+ number = int(text_lines[j])
+ colour = text_lines[j+1]
+ cards[i] = Card(number, colour.strip())
+ i += 1
+ j += 2
+#print(cards)
+# 3 d
+selected_cards = [False for i in range(30)]
+def ChooseCard(array_index: int):
+ global cards
+ global selected_cards
+ if array_index < 1 or array_index > 30:
+ print("The card array index is out of range")
+ return
+ normalized_index = array_index - 1
+ if selected_cards[normalized_index] == True: # already selected
+ for i in range(len(selected_cards)):
+ if not selected_cards[i]: return i + 1
+ else:
+ return array_index
+
+# 3 e i
+Player1 = [None for i in range(4)] # type in array is Card
+for i in range(4):
+ selected = int(input("Your selected card index: "))
+ Player1[i] = cards[ChooseCard(selected) - 1]
+for card in Player1:
+ print(f"{card.GetNumber()} {card.GetColour()}")
+# .......
diff --git a/notebooks/dictionaries.ipynb b/notebooks/dictionaries.ipynb
index b70bf65..aa68193 100644
--- a/notebooks/dictionaries.ipynb
+++ b/notebooks/dictionaries.ipynb
@@ -49,7 +49,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.12.8"
+ "version": "3.12.7"
}
},
"nbformat": 4,