From 9ae7b1ef3281baea6b9998bbf67286637251103e Mon Sep 17 00:00:00 2001 From: gaugoy Date: Wed, 24 Jul 2024 22:54:01 +0530 Subject: [PATCH] add pattern matching code --- gaurav_pattern_matching.ipynb | 160 ++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 gaurav_pattern_matching.ipynb diff --git a/gaurav_pattern_matching.ipynb b/gaurav_pattern_matching.ipynb new file mode 100644 index 0000000..304633c --- /dev/null +++ b/gaurav_pattern_matching.ipynb @@ -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 +}