Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pattern matching code #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions gaurav_pattern_matching.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def match(pat, text):\n",
" if not pat and not text:\n",
" return True\n",
" if (not pat and text):\n",
" return False\n",
"\n",
" # Check if the first character is not ., *\n",
" if pat[0] not in ['.', '*']:\n",
" if pat[0] == text[0]:\n",
" return match(pat[1:], text[1:])\n",
" else:\n",
" return False\n",
"\n",
" # Check for . condition\n",
" if pat[0] == '.':\n",
" return match(pat[1:], text[1:])\n",
"\n",
" # Check for * condition\n",
" if pat[0] == '*':\n",
" if not pat[1:]:\n",
" return True\n",
" else:\n",
" return match(pat[1:], text) or (text and match(pat, text[1:])) "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"code_folding": []
},
"outputs": [],
"source": [
"def tester(test_cases):\n",
" for test_case in test_cases:\n",
" arr, expected = test_case\n",
" actual = match(arr[0],arr[1])\n",
" print(\"Pattren:\\t\\t\", arr[0],\"\\nMatch String:\\t\\t\",arr[1])\n",
" if actual == expected:\n",
" print(\"result\\t\\t\\tSuccess!\\n\\n \")\n",
" else:\n",
" print(f\"\\tFailed!. Expected {expected} but got {actual}\\n\\n\") "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pattren:\t\t a...* \n",
"Match String:\t\t abcdx\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t abc \n",
"Match String:\t\t abc\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t a* \n",
"Match String:\t\t abcde\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t a*b.. \n",
"Match String:\t\t abcg\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t a*b. \n",
"Match String:\t\t abc\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t a.. \n",
"Match String:\t\t abc\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t . \n",
"Match String:\t\t bc\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t . \n",
"Match String:\t\t b\n",
"result\t\t\tSuccess!\n",
"\n",
" \n",
"Pattren:\t\t .* \n",
"Match String:\t\t \n",
"result\t\t\tSuccess!\n",
"\n",
" \n"
]
}
],
"source": [
"test_cases = [\n",
" ((\"a...*\", \"abcdx\"),True),\n",
" ((\"abc\", \"abc\"),True),\n",
" ((\"a*\", \"abcde\"),True),\n",
" ((\"a*b..\", \"abcg\"),True),\n",
" ((\"a*b.\", \"abc\"),True),\n",
" ((\"a..\", \"abc\"),True),\n",
" ((\".\", \"bc\"),False),\n",
" ((\".\", \"b\"),True),\n",
" ((\".*\", \"\"),True),\n",
"]\n",
"tester(test_cases)"
]
}
],
"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.6.8"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}