-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
776a2e5
commit 052a267
Showing
1 changed file
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Task 1. Code a priority queue\n", | ||
"## The elements in the queue are Latin letters (a, b, c, … , z) and the priority is given by which letter comes first in the ABC" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Regular queue\n", | ||
"h a d y r \n", | ||
"\n", | ||
"Priority queue\n", | ||
"y r h d a " | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# q = {r, y, d, a, h} This is the example\n", | ||
"q=[] # We are trying a queue implemented with a list\n", | ||
"q.append('r')\n", | ||
"q.append('y')\n", | ||
"q.append('d')\n", | ||
"q.append('a')\n", | ||
"q.append('h')\n", | ||
"# if we print these values we will see the typical behaviour of a queue: FIFO\n", | ||
"#In this case h , a, d, y , r. Remember that the first element to get in was r\n", | ||
"print('Regular queue')\n", | ||
"while q:\n", | ||
" print(q.pop(),end=\" \")\n", | ||
"print('\\n')\n", | ||
"#If we want to make a priority queue, where the priority is given by the ABC, we need to sort the queue first. \n", | ||
"# This is higly inneficient,and only recommended for small queues. But is what you can build right now with the tools you know. \n", | ||
"q.append('r')\n", | ||
"q.append('y')\n", | ||
"q.append('d')\n", | ||
"q.append('a')\n", | ||
"q.append('h')\n", | ||
"#Sort the queue\n", | ||
"q.sort()\n", | ||
"# Now pop the elements\n", | ||
"print('Priority queue')\n", | ||
"while q:\n", | ||
" print(q.pop(),end=\" \")\n", | ||
"#The first element to get into q was again r, but the first getting out now is a" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Task2. Code a calculator that uses postfix or Polish notation\n", | ||
"Follow this algorithm:\n", | ||
"1. Read the input and push numbers onto a stack until you reach an operator.\n", | ||
"2. When you see an operator, apply the operator to the two numbers popped from the stack.\n", | ||
"3. Push the resulting value back onto the stack.\n", | ||
"4. When the input is complete, the value left on the stack is the result." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"3.0\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#define a function to calculate an arithmetic expression in posfix notation\n", | ||
"\n", | ||
"\n", | ||
"def posfixN(inputs):\n", | ||
" stack = [] # Create an empty stack\n", | ||
" for a in inputs: # Step 1\n", | ||
" if type(a) is int:\n", | ||
" stack.append(a)\n", | ||
" continue\n", | ||
" op1, op2 = stack.pop(), stack.pop() # Step 2\n", | ||
"\n", | ||
" if a == '+':\n", | ||
" stack.append(op2 + op1) # Step 3\n", | ||
" elif a == '-':\n", | ||
" stack.append(op2 - op1) # Step 3\n", | ||
" elif a == '*':\n", | ||
" stack.append(op2 * op1) # Step 3\n", | ||
" elif a == '/':\n", | ||
" stack.append(op2 / op1) # Step 3\n", | ||
"\n", | ||
" return stack.pop() #Step 4\n", | ||
"\n", | ||
"# Example 1 2 + 3 * 6 + 2 3 + / \n", | ||
"expression = [1, 2, '+', 3, '*', 6, '+', 2, 3, '+', '/' ]\n", | ||
"result = posfixN(expression)\n", | ||
"print(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |