- 27th Jan 2024
- 15:45 pm
This CIND830 (Assignment 2) from Toronto Metropolitan University is challenging and many students come to us for tutoring sessions on this assignment. We are providing a partial question and solution for this assignment in the post. To check for options on how to get complete sample solution along with doubt-clearing sessions from our Python tutors, please check the end of the blog post.
CIND830 (Assignment 2)- Question and Solution for Question 5
### **Question 5 [20 pts]**: "
],
"metadata": {
"id": "VxfumaAmOhHY"
}
},
{
"cell_type": "markdown",
"source": [
"**Question 5.a [10 pts]**: Write an algorithm that creates a Ragged Grid with four rows. The first row has 3 positions, the second row has 4 positions, the third row includes 6 positions, and the fourth row contains 1. You can modify the iArray ADT defined below or use any standard or third-party library to implement your algorithm.\n",
"\n",
"```\n",
"class iArray():\n",
" def __init__(self, capacity, fillValue = None):\n",
" self._items = list()\n",
" for count in range(capacity): \n",
" self._items.append(fillValue)\n",
" def __len__(self):\n",
" return len(self._items) \n",
" def __str__(self):\n",
" return str(self._items)\n",
" def __iter__(self):\n",
" return iter(self._items)\n",
" def __getitem__(self, index):\n",
" return self._items[index]\n",
" def __setitem__(self, index, newItem):\n",
" self._items[index] = newItem\n",
"```"
],
"metadata": {
"id": "JZj5rCeeOZtY"
}
},
{
"cell_type": "code",
"source": [
"#INSERT YOUR ANSWER HERE.\n",
"\n",
"class iArray():\n",
" def __init__(self, capacity, fillValue = None):\n",
" self._items = list()\n",
" for count in range(capacity): \n",
" self._items.append(fillValue)\n",
" def __len__(self):\n",
" return len(self._items) \n",
" def __str__(self):\n",
" return str(self._items)\n",
" def __iter__(self):\n",
" return iter(self._items)\n",
" def __getitem__(self, index):\n",
" return self._items[index]\n",
" def __setitem__(self, index, newItem):\n",
" self._items[index] = newItem\n",
"\n",
"class Ragged_Grid(iArray):\n",
" def __init__(self):\n",
" self._rgGrid = []\n",
" self._rgGrid.append(iArray(3))\n",
" self._rgGrid.append(iArray(4))\n",
" self._rgGrid.append(iArray(6))\n",
" self._rgGrid.append(iArray(1))\n",
"\n",
"#example\n",
"raggedGrid = Ragged_Grid()"
],
"outputs": [],
"execution_count": 54,
"metadata": {
"id": "ML2ryPyUOto-"
}
},
{
"cell_type": "markdown",
"source": [
"**Question 5.b [10 pts]**: Write an algorithm to create and display a two-dimensional array or a regular Grid of three rows and four columns. The Grid's elements are selected randomly from an inclusive interval of `[-100, 100]`. You can install and use any standard or third-party library to report your answer to this question."
],
"metadata": {
"id": "6-NzxR9DO8K8"
}
},
{
"cell_type": "code",
"source": [
"#INSERT YOUR ANSWER HERE.\n",
"\n",
"import random\n",
"\n",
"# Step 1\n",
"grid = []\n",
"\n",
"# Step 2\n",
"for i in range(3):\n",
" temp = []\n",
" # Step 3\n",
" for j in range(4):\n",
" # Step 4\n",
" num = random.randint(-100, 100)\n",
" temp.append(num)\n",
" # Step 5\n",
" grid.append(temp)\n",
"\n",
"# Step 6 is completed after the loop in Step 2\n",
"\n",
"# Step 7\n",
"for row in grid:\n",
" for element in row:\n",
" print(element, end=\" \")\n",
" print()"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"-13 18 100 -87 \n",
"-8 -51 16 -35 \n",
"90 -85 -25 -15 \n"
]
}
],
"execution_count": 56,
"metadata": {
"id": "hIZIWSAdPAs9",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "cb6c5515-1df8-4eca-ab23-44bada84f5ea"
}
},
{
"cell_type": "markdown",
"source": [
"**Question BONUS [10 pts]**: \n",
"Define and implement a [Double-ended queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) (Dequeue) ADT. The Dequeue is a sequence of items where addtions and deletions of items can be done at both ends of the sequence. The Dequeue can be implemnted using the iArray ADT with the following methods: \n",
"``` \n",
"constructor, append_left, append_right, pop_left, pop_right, peek_left, peek_right, __len__, and __str__.\n",
"```\n",
"```\n",
"class Deque(iArray):\n",
" def __init__(self, capacity = 10):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def append_left(self, newItem):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def append_right(self, newItem): \n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def pop_left(self):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def pop_right(self):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def peek_left(self):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def peek_right(self):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def __len__(self):\n",
" #INSERT YOUR ANSWER HERE.\n",
" \n",
" def __str__(self):\n",
" #INSERT YOUR ANSWER HERE.\n",
"```"
],
"metadata": {
"id": "A4M7odS0PH1m"
}
},
{
"cell_type": "code",
"source": [
"#INSERT YOUR ANSWER HERE.\n",
"class Dequeue:\n",
" def __init__(self, capacity=10):\n",
" self._items = iArray(capacity)\n",
" self._front = 0\n",
" self._rear = -1\n",
" self._size = 0\n",
"\n",
" def append_left(self, item):\n",
" if self._size == len(self._items):\n",
" self._resize(2 * len(self._items))\n",
" self._front = (self._front - 1) % len(self._items)\n",
" self._items[self._front] = item\n",
" self._size += 1\n",
"\n",
" def append_right(self, item):\n",
" if self._size == len(self._items):\n",
" self._resize(2 * len(self._items))\n",
" self._rear = (self._rear + 1) % len(self._items)\n",
" self._items[self._rear] = item\n",
" self._size += 1\n",
"\n",
" def pop_left(self):\n",
" if self.is_empty():\n",
" return None\n",
" item = self._items[self._front]\n",
" self._items[self._front] = None\n",
" self._front = (self._front + 1) % len(self._items)\n",
" self._size -= 1\n",
" if 0 < self._size < len(self._items) // 4:\n",
" self._resize(len(self._items) // 2)\n",
" return item\n",
"\n",
" def pop_right(self):\n",
" if self.is_empty():\n",
" return None\n",
" item = self._items[self._rear]\n",
" self._items[self._rear] = None\n",
" self._rear = (self._rear - 1) % len(self._items)\n",
" self._size -= 1\n",
" if 0 < self._size < len(self._items) // 4:\n",
" self._resize(len(self._items) // 2)\n",
" return item\n",
"\n",
" def peek_left(self):\n",
" if self.is_empty():\n",
" return None\n",
" return self._items[self._front]\n",
"\n",
" def peek_right(self):\n",
" if self.is_empty():\n",
" return None\n",
" return self._items[self._rear]\n",
"\n",
" def __len__(self):\n",
" return self._size\n",
"\n",
" def is_empty(self):\n",
" return len(self) == 0\n",
"\n",
" def _resize(self, capacity):\n",
" old_items = self._items\n",
" self._items = iArray(capacity)\n",
" for i in range(self._size):\n",
" self._items[i] = old_items[(self._front + i) % len(old_items)]\n",
" self._front = 0\n",
" self._rear = self._size - 1\n",
"\n",
" def __str__(self):\n",
" result = ''\n",
" for i in range(len(self)):\n",
" result += str(self._items[(self._front + i) % len(self._items)]) + ' '\n",
" return result\n",
"\n",
"#example\n",
"\n",
"dequeue = Dequeue()\n",
"dequeue.append_left(0)\n",
"dequeue.append_right(1)\n",
"dequeue.append_left(2)\n",
"dequeue.append_right(3)\n",
"dequeue.append_left(4)\n",
"dequeue.append_right(5)\n",
"dequeue.append_left(6)\n",
"dequeue.append_right(7)\n",
"\n",
"print(dequeue)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"6 4 2 0 1 3 5 7 \n"
]
}
],
"execution_count": 58,
"metadata": {
"id": "aLRJkPSRPNhJ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "61750f59-3e02-479f-8b18-48f0297f32b5"
}
},
{
"cell_type": "markdown",
"source": [
"*****"
],
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
},
"id": "GGb33L5LioN2"
}
},
{
"cell_type": "markdown",
"source": [
"#### This is the end of assignment 2"
],
"metadata": {
"id": "VfA8AP8qO0g7"
}
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.8.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"nteract": {
"version": "0.28.0"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Instructions on downloading the complete solution for CIND830 Assignment
This post shares a partial sample solution on CIND830 (Assignment 2). To get a complete solution along with doubt-clearing sessions and online tutoring, please talk to our Python experts.
Please check the below instructions
- Option 1 - To download the complete solution for this assignment free of cost - Please visit our Python Assignment Sample Solution page. The solution is available and posted on our website and is available free of cost for all students. You need to talk to our experts for a customized solution as per your requirements
- Option 2 - Reach out to our Python Tutors to get online tutoring related to this assignment and get your doubts cleared
- Option 3 - You can check the partial question/ solution for this Neural Networks assignment in this blog below
About The Author
Introducing Dr. Kimberly Adams, a seasoned data scientist and educator. Holding a Ph.D. in Computer Science, Dr. Adams combines a wealth of industry experience with a passion for teaching. Her commitment to fostering learning in Python programming for data science is evident in this comprehensive assignment solution. Drawing on real-world scenarios, Dr. Adams provides invaluable insights, guiding students to enhance their skills in data science through practical application.