Skip to content

Commit

Permalink
Add course notebooks to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
sewade committed Nov 16, 2019
0 parents commit a0fcfd2
Show file tree
Hide file tree
Showing 20 changed files with 8,238 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Bootcamp
.ipynb_checkpoints
193 changes: 193 additions & 0 deletions Abubakr_py_exercise7.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Balanced Parentheses Check \n",
"\n",
"## Problem Statement\n",
"\n",
"Given a string of opening and closing parentheses, check whether it’s balanced. We have 3 types of parentheses: round brackets: (), square brackets: [], and curly brackets: {}. Assume that the string doesn’t contain any other character than these, no spaces words or numbers. As a reminder, balanced parentheses require every opening parenthesis to be closed in the reverse order opened. For example ‘([])’ is balanced but ‘([)]’ is not. \n",
"\n",
"\n",
"You can assume the input string has no spaces.\n",
"\n",
"## Solution\n",
"\n",
"Fill out your solution below:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"def balance_check(s):\n",
" if len(s)%2!=0:\n",
" return False\n",
" \n",
" # mapping closing and openning Parentheses of the same type\n",
" dic = {\"{\":\"}\",\"[\":\"]\",\"(\":\")\"}\n",
" \n",
" open_ = dic.keys()\n",
" close = dic.values()\n",
" \n",
" stack = list() # Create an Empty stack (we can treat a list as a stack)\n",
" \n",
" # The idea is to loop through the string and see if\n",
" # 1- each parentheses is opened and closed and opened before it's closed\n",
" # 2- each inner parentheses closes before an outer one does\n",
" # This can be done by having a stack that keeps a reverse order of \n",
" # the opened parentheses, and checks each time a parentheses is closed \n",
" # if it's the last one that have been opened\n",
" \n",
" for i in s: \n",
" if i in open_:\n",
" stack.append(i)\n",
" elif i in close:\n",
" if dic[stack[-1]]==i:\n",
" stack.pop()\n",
" else:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"balance_check('[]')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"balance_check('[](){([[[]]])}')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"balance_check('()(){]}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test Your Solution"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ALL TEST CASES PASSED\n"
]
}
],
"source": [
"\"\"\"\n",
"RUN THIS CELL TO TEST YOUR SOLUTION\n",
"\"\"\"\n",
"from nose.tools import assert_equal\n",
"\n",
"class TestBalanceCheck(object):\n",
" \n",
" def test(self,sol):\n",
" assert_equal(sol('[](){([[[]]])}('),False)\n",
" assert_equal(sol('[{{{(())}}}]((()))'),True)\n",
" assert_equal(sol('[[[]])]'),False)\n",
" print('ALL TEST CASES PASSED')\n",
" \n",
"# Run Tests\n",
"\n",
"t = TestBalanceCheck()\n",
"t.test(balance_check)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Good Job!"
]
},
{
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit a0fcfd2

Please sign in to comment.