summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu on Homelab <z.liu@outlook.com.gr>2025-03-19 09:19:11 +0200
committerZhongheng Liu on Homelab <z.liu@outlook.com.gr>2025-03-19 09:19:11 +0200
commit556402c0fdbab1f1af0a1e9923a859d5c12618b5 (patch)
treecd9eb4ada139a9fa72e83d759f0062b3918a4f8a
parentac44ac75508b72ca63e08875a623f992fe372fc2 (diff)
downloadcs-y13-556402c0fdbab1f1af0a1e9923a859d5c12618b5.tar.gz
cs-y13-556402c0fdbab1f1af0a1e9923a859d5c12618b5.tar.bz2
cs-y13-556402c0fdbab1f1af0a1e9923a859d5c12618b5.zip
funniness
-rw-r--r--9618-42-mj-2022/Question1_J22.py12
-rw-r--r--9618-42-mj-2022/Question2_J22.py23
-rw-r--r--9618-42-mj-2022/Question3_J22.py25
-rw-r--r--9618-42-mj-2023/Question1_J2023.py29
-rw-r--r--notebooks/data_structures.ipynb26
5 files changed, 81 insertions, 34 deletions
diff --git a/9618-42-mj-2022/Question1_J22.py b/9618-42-mj-2022/Question1_J22.py
index 3b8e5c5..2bee365 100644
--- a/9618-42-mj-2022/Question1_J22.py
+++ b/9618-42-mj-2022/Question1_J22.py
@@ -1,19 +1,17 @@
#1a
-StackData = [None for i in range(10)] # global variable because defined in the main script execution context.
-StackPointer = 0 # global variable because defined in the main script execution context.
+global StackData
+global StackPointer
+StackData = [None for i in range(10)] # global variable
+StackPointer = 0 # global variable
#1b
def OutputAllElements():
- global StackData
- global StackPointer
for dataItem in StackData:
print(dataItem)
print(f"Stack pointer: {StackPointer}")
#1c
def Push(push_item: int):
- global StackData
- global StackPointer
if StackPointer == len(StackData):
return False
StackData[StackPointer] = push_item
@@ -21,8 +19,6 @@ def Push(push_item: int):
return True
# 1 e i
def Pop():
- global StackData
- global StackPointer
if StackPointer == 0:
return -1
StackPointer -= 1
diff --git a/9618-42-mj-2022/Question2_J22.py b/9618-42-mj-2022/Question2_J22.py
index d514542..70e8586 100644
--- a/9618-42-mj-2022/Question2_J22.py
+++ b/9618-42-mj-2022/Question2_J22.py
@@ -6,18 +6,7 @@ for i in range(10):
for j in range(10):
Row.append(random.randint(1, 100))
ArrayData.append(Row)
-
-# 2 b i
ArrayLength = 10
-for x in range(0, ArrayLength): # range() function excludes ArrayLength, so equiv to 0 -> ArrayLength - 1
- for y in range(0, ArrayLength - 1): # range() function excludes ArrayLength, so equiv to 0 -> ArrayLength - 2
- for z in range(0, ArrayLength - y - 1):
- if ArrayData[x][z] > ArrayData[x][z+1]:
- # swapping
- tempValue = ArrayData[x][z]
- ArrayData[x][z] = ArrayData[x][z+1]
- ArrayData[x][z+1] = tempValue
-
# 2 b ii
def OutputAllElements():
global ArrayData
@@ -26,6 +15,18 @@ def OutputAllElements():
for j in range(0, ArrayLength):
print(ArrayData[i][j], end=" ")
print()
+print("before")
+OutputAllElements()
+# 2 b i
+for x in range(0, ArrayLength): # range() function excludes ArrayLength, so equiv to 0 -> ArrayLength - 1
+ for y in range(0, ArrayLength - 1): # range() function excludes ArrayLength, so equiv to 0 -> ArrayLength - 2
+ for z in range(0, ArrayLength - y - 1):
+ if ArrayData[x][z] > ArrayData[x][z+1]:
+ # swapping
+ tempValue = ArrayData[x][z]
+ ArrayData[x][z] = ArrayData[x][z+1]
+ ArrayData[x][z+1] = tempValue
+print("after")
# 2 b iii
OutputAllElements()
diff --git a/9618-42-mj-2022/Question3_J22.py b/9618-42-mj-2022/Question3_J22.py
index 764f4c0..384e9e3 100644
--- a/9618-42-mj-2022/Question3_J22.py
+++ b/9618-42-mj-2022/Question3_J22.py
@@ -13,17 +13,20 @@ class Card:
# 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
+try:
+ 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
+except IOError:
+ print("Cannot find file.")
#print(cards)
# 3 d
selected_cards = [False for i in range(30)]
diff --git a/9618-42-mj-2023/Question1_J2023.py b/9618-42-mj-2023/Question1_J2023.py
new file mode 100644
index 0000000..1e2e2bd
--- /dev/null
+++ b/9618-42-mj-2023/Question1_J2023.py
@@ -0,0 +1,29 @@
+#1a
+global Animals
+Animals = [None for i in range(10)] # String array, initialise to None to imply emptiness
+#1b
+Animals = [
+ "horse",
+ "lion",
+ "rabbit",
+ "mouse",
+ "bird",
+ "deer",
+ "whale",
+ "elephant",
+ "kangaroo",
+ "tiger"
+]
+#1c
+def SortDescending():
+ ArrayLength = len(Animals)
+ for x in range(0, ArrayLength):
+ for y in range(0, ArrayLength - x - 1):
+ if Animals[y][0] < Animals[y+1][0]:
+ Temp = Animals[y]
+ Animals[y] = Animals[y+1]
+ Animals[y+1] = Temp
+#1di
+SortDescending()
+for i in range(len(Animals)):
+ print(Animals[i]) \ No newline at end of file
diff --git a/notebooks/data_structures.ipynb b/notebooks/data_structures.ipynb
index 370c569..25de32d 100644
--- a/notebooks/data_structures.ipynb
+++ b/notebooks/data_structures.ipynb
@@ -11,9 +11,27 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 1,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[1, None, None, None, None, None, None, None, None, None]\n",
+ "[1, 2, None, None, None, None, None, None, None, None]\n",
+ "[1, 2, 69, None, None, None, None, None, None, None]\n",
+ "I popped this 69\n",
+ "[1, 2, None, None, None, None, None, None, None, None]\n",
+ "I popped this 2\n",
+ "[1, None, None, None, None, None, None, None, None, None]\n",
+ "[1, None, None, None, None, None, None, None, None, None]\n",
+ "I popped this 1\n",
+ "[None, None, None, None, None, None, None, None, None, None]\n",
+ "ERROR List is empty, cannot pop.\n"
+ ]
+ }
+ ],
"source": [
"stack = [None for index in range(0,10)]\n",
"basePointer = 0\n",
@@ -108,7 +126,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": ".venv",
+ "display_name": "Python 3",
"language": "python",
"name": "python3"
},
@@ -122,7 +140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.12.8"
+ "version": "3.12.7"
}
},
"nbformat": 4,