finished hw1

This commit is contained in:
0x01fe committed 2025-02-02 14:27:24 -06:00
1 parent 50211d40f0
commit 32135893d4
1 file changed
+181 -54
+181 -54
View File
@@ -51,7 +51,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 1,
"metadata": { "metadata": {
"id": "jUNgSbfpLrVP" "id": "jUNgSbfpLrVP"
}, },
@@ -329,37 +329,78 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 22,
"metadata": { "metadata": {
"id": "Praq255-p0cB" "id": "Praq255-p0cB"
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Write class definition of queue below this line\n", "# Write class definition of queue below this line\n",
"class Queue():\n", "class Queue:\n",
"\n", "\n",
" items: list[Item] = []\n", " head: Node = None\n",
"\n",
" def __init__(self) -> None:\n",
" pass\n",
"\n", "\n",
" def enqueue(self, item: Item) -> None:\n", " def enqueue(self, item: Item) -> None:\n",
" self.items.append(item)\n", " if not self.head:\n",
" self.head = Node()\n",
" self.head.item = item\n",
" return None\n",
"\n",
" next: Node | None = self.head.next\n",
" if not next:\n",
" self.head.next = Node()\n",
" self.head.next.item = item\n",
" return None\n",
"\n",
" while next:\n",
" if not next.next:\n",
" next.next = Node()\n",
" next.next.item = item\n",
" return None\n",
" next = next.next\n",
"\n", "\n",
" def dequeue(self) -> Item:\n", " def dequeue(self) -> Item:\n",
" return self.items.pop(0)\n", " item = self.head.item\n",
" self.head = self.head.next\n",
" return item\n",
"\n", "\n",
" def isEmpty(self) -> bool:\n", " def isEmpty(self) -> bool:\n",
" return not bool(self.items)\n", " return not self.head\n",
"\n", "\n",
" def peek(self) -> Item | None:\n", " def peek(self) -> Item | None:\n",
" return self.items[0] if len(self) > 0 else None\n", " return self.head.item if self.head else None\n",
"\n", "\n",
" def __len__(self) -> int:\n", " def __len__(self) -> int:\n",
" return len(self.items)\n", " l = 0\n",
" for i in self:\n",
" l += 1\n",
"\n",
" return l\n",
"\n", "\n",
" def __str__(self) -> str:\n", " def __str__(self) -> str:\n",
" return str(self.items)\n", " s = \"[\"\n",
"\n",
" for item in self:\n",
" s += f'{str(item)}, '\n",
"\n",
" if len(self) > 0:\n",
" s = s[:-2]\n",
"\n",
" s += \"]\"\n",
"\n",
" return s\n",
"\n",
" def __iter__(self):\n",
" self.__iter_node = self.head\n",
" return self\n",
"\n",
" def __next__(self):\n",
" if self.__iter_node is not None:\n",
" x = self.__iter_node.item\n",
" self.__iter_node = self.__iter_node.next\n",
" return x\n",
" else:\n",
" raise StopIteration\n",
"\n" "\n"
] ]
}, },
@@ -374,7 +415,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 23,
"metadata": { "metadata": {
"id": "yrZJlezKq-jd" "id": "yrZJlezKq-jd"
}, },
@@ -417,11 +458,37 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 24,
"metadata": { "metadata": {
"id": "CK1mYV84rLvb" "id": "CK1mYV84rLvb"
}, },
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dequeue the queue\n",
"item dequeued from queue: 0\n",
"queue: [1, 2, 3, 4]\n",
"length of queue: 4\n",
"item dequeued from queue: 1\n",
"queue: [2, 3, 4]\n",
"length of queue: 3\n",
"item dequeued from queue: 2\n",
"queue: [3, 4]\n",
"length of queue: 2\n",
"item dequeued from queue: 3\n",
"queue: [4]\n",
"length of queue: 1\n",
"item dequeued from queue: 4\n",
"queue: []\n",
"length of queue: 0\n",
"\n",
"Queue after enqueue: [5]\n",
"Length of queue: 1\n"
]
}
],
"source": [ "source": [
"# dequeue the queue\n", "# dequeue the queue\n",
"print(\"Dequeue the queue\")\n", "print(\"Dequeue the queue\")\n",
@@ -477,34 +544,92 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 35,
"metadata": { "metadata": {
"id": "k50yGJZ2IbUm" "id": "k50yGJZ2IbUm"
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Write class definition of stack below this line\n", "# Write class definition of stack below this line\n",
"class Stack():\n", "class Stack:\n",
"\n", "\n",
" items: list[Item] = []\n", " items: list[Item] = []\n",
"\n", " head: Node = None\n",
" def __init__(self) -> None:\n",
" pass\n",
"\n", "\n",
" def push(self, item: Item) -> None:\n", " def push(self, item: Item) -> None:\n",
" self.items.append(item)\n", " if not self.head:\n",
" self.head = Node()\n",
" self.head.item = item\n",
" return None\n",
"\n",
" next: Node | None = self.head.next\n",
" if not next:\n",
" self.head.next = Node()\n",
" self.head.next.item = item\n",
" return None\n",
"\n",
" while next:\n",
" if not next.next:\n",
" next.next = Node()\n",
" next.next.item = item\n",
" return None\n",
" next = next.next\n",
"\n", "\n",
" def pop(self) -> Item:\n", " def pop(self) -> Item:\n",
" return self.items.pop()\n", " node = self.head\n",
" last = None\n",
"\n",
" while node.next:\n",
" last = node\n",
" node = node.next\n",
"\n",
" if not last:\n",
" self.head = None\n",
" else:\n",
" last.next = None\n",
" return node.item\n",
"\n", "\n",
" def isEmpty(self) -> bool:\n", " def isEmpty(self) -> bool:\n",
" return not bool(self.items)\n", " return not self.head\n",
"\n", "\n",
" def peek(self) -> Item:\n", " def peek(self) -> Item:\n",
" return self.items[-1]\n", " node = None\n",
" for i in self:\n",
" node = i\n",
"\n",
" return node\n",
"\n", "\n",
" def __len__(self) -> int:\n", " def __len__(self) -> int:\n",
" return len(self.items)\n", " l = 0\n",
" for i in self:\n",
" l += 1\n",
"\n",
" return l\n",
"\n",
" def __str__(self) -> str:\n",
" s = \"[\"\n",
"\n",
" for item in self:\n",
" s += f'{str(item)}, '\n",
"\n",
" if len(self) > 0:\n",
" s = s[:-2]\n",
"\n",
" s += \"]\"\n",
"\n",
" return s\n",
"\n",
" def __iter__(self):\n",
" self.__iter_node = self.head\n",
" return self\n",
"\n",
" def __next__(self):\n",
" if self.__iter_node is not None:\n",
" x = self.__iter_node.item\n",
" self.__iter_node = self.__iter_node.next\n",
" return x\n",
" else:\n",
" raise StopIteration\n",
"\n", "\n",
"\n" "\n"
] ]
@@ -520,7 +645,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 36,
"metadata": { "metadata": {
"id": "CNXtkCLrA6pK" "id": "CNXtkCLrA6pK"
}, },
@@ -529,8 +654,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"The stack after initialization is <__main__.Stack object at 0x000001D262283450>\n", "The stack after initialization is []\n",
"The stack after push is <__main__.Stack object at 0x000001D262283450>\n" "The stack after push is [0, 1, 2, 3, 4]\n"
] ]
} }
], ],
@@ -568,7 +693,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 37,
"metadata": { "metadata": {
"id": "1y8IlB2OA6b3" "id": "1y8IlB2OA6b3"
}, },
@@ -579,22 +704,22 @@
"text": [ "text": [
"Pop the stack\n", "Pop the stack\n",
"item dequeued from stack: 4\n", "item dequeued from stack: 4\n",
"stack: <__main__.Stack object at 0x000001D262283450>\n", "stack: [0, 1, 2, 3]\n",
"length of stack: 4\n", "length of stack: 4\n",
"item dequeued from stack: 3\n", "item dequeued from stack: 3\n",
"stack: <__main__.Stack object at 0x000001D262283450>\n", "stack: [0, 1, 2]\n",
"length of stack: 3\n", "length of stack: 3\n",
"item dequeued from stack: 2\n", "item dequeued from stack: 2\n",
"stack: <__main__.Stack object at 0x000001D262283450>\n", "stack: [0, 1]\n",
"length of stack: 2\n", "length of stack: 2\n",
"item dequeued from stack: 1\n", "item dequeued from stack: 1\n",
"stack: <__main__.Stack object at 0x000001D262283450>\n", "stack: [0]\n",
"length of stack: 1\n", "length of stack: 1\n",
"item dequeued from stack: 0\n", "item dequeued from stack: 0\n",
"stack: <__main__.Stack object at 0x000001D262283450>\n", "stack: []\n",
"length of stack: 0\n", "length of stack: 0\n",
"\n", "\n",
"stack after push: <__main__.Stack object at 0x000001D262283450>\n", "stack after push: [5]\n",
"length of stack: 1\n" "length of stack: 1\n"
] ]
} }
@@ -672,7 +797,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 38,
"metadata": { "metadata": {
"id": "brGkmH0dIuFS" "id": "brGkmH0dIuFS"
}, },
@@ -682,23 +807,33 @@
" def __init__(self, homepage: str):\n", " def __init__(self, homepage: str):\n",
" self.current_page = homepage\n", " self.current_page = homepage\n",
" # create a backward stack\n", " # create a backward stack\n",
" self.backward = Stack()\n",
" # create a forward stack\n", " # create a forward stack\n",
" self.forward_h = Stack()\n",
"\n", "\n",
" def visit(self, url: str) -> None:\n", " def visit(self, url: str) -> None:\n",
" # function to visit a new page, updating the history accordingly.\n", " # function to visit a new page, updating the history accordingly.\n",
" pass # remove this\n", " self.backward.push(self.current_page)\n",
" self.current_page = url\n",
"\n", "\n",
" def back(self, steps: int) -> str:\n", " def back(self, steps: int) -> str:\n",
" # function to oves back a specified number of steps in the browsing history.\n", " # function to oves back a specified number of steps in the browsing history.\n",
" pass\n", " for i in range(steps):\n",
" if not self.backward.isEmpty():\n",
" self.forward_h.push(self.current_page)\n",
" self.current_page = self.backward.pop()\n",
"\n",
"\n", "\n",
" def forward(self, steps: int) -> str:\n", " def forward(self, steps: int) -> str:\n",
" # function to moves forward a specified number of steps in the browsing history.\n", " # function to moves forward a specified number of steps in the browsing history.\n",
" pass\n", " for i in range(steps):\n",
" if not self.forward_h.isEmpty():\n",
" self.backward.push(self.current_page)\n",
" self.current_page = self.forward_h.pop()\n",
"\n", "\n",
" def current(self) -> str:\n", " def current(self) -> str:\n",
" # function to get URL of the current active page\n", " # function to get URL of the current active page\n",
" pass\n", " return self.current_page\n",
"\n" "\n"
] ]
}, },
@@ -713,7 +848,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 39,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@@ -721,15 +856,7 @@
"id": "vRyiPCpagKE0", "id": "vRyiPCpagKE0",
"outputId": "c11dc0b3-594f-42a0-c585-9097a0bdaac2" "outputId": "c11dc0b3-594f-42a0-c585-9097a0bdaac2"
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Browser initialized with homepage: uark-aicv.github.io\n"
]
}
],
"source": [ "source": [
"history = BrowserHistory(\"uark-aicv.github.io\")\n", "history = BrowserHistory(\"uark-aicv.github.io\")\n",
"history.visit(\"uark-aicv.github.io/team\")\n", "history.visit(\"uark-aicv.github.io/team\")\n",
@@ -750,7 +877,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": 40,
"metadata": { "metadata": {
"id": "vrimx6IJgKBZ" "id": "vrimx6IJgKBZ"
}, },
@@ -771,7 +898,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 41,
"metadata": { "metadata": {
"id": "lvQrZwdr9p89" "id": "lvQrZwdr9p89"
}, },
@@ -807,7 +934,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.3" "version": "3.12.0"
} }
}, },
"nbformat": 4, "nbformat": 4,