Finish A5

This commit is contained in:
Isaac Shoebottom 2024-03-01 20:30:43 -04:00
parent fb6c21adcb
commit 0d76b15bbb

View File

@ -2,30 +2,44 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 38, "outputs": [],
"id": "initial_id", "source": [
"import automata.regex.regex as re\n",
"from automata.fa.nfa import NFA\n",
"from IPython.display import Math\n",
"\n",
"debug = False\n",
"\n",
"def regex_test(regex: str, inputs: list[str]):\n",
" re.validate(regex)\n",
" nfa = NFA.from_regex(regex)\n",
" for i in inputs:\n",
" print(f\"{i}: {nfa.accepts_input(i)}\")\n",
" \n",
"def latexify(regex: str):\n",
" steps = regex\n",
" steps = steps.replace('()', '\\\\epsilon')\n",
" steps = steps.replace('(', '\\\\left(')\n",
" steps = steps.replace(')', '\\\\right)')\n",
" steps = steps.replace('|', '\\\\cup')\n",
" steps = steps.replace('&', '\\\\land')\n",
" steps = steps.replace('*', '^*')\n",
" steps = '$' + steps + '$'\n",
" return steps"
],
"metadata": { "metadata": {
"collapsed": true, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-01T17:17:50.546658700Z", "end_time": "2024-03-02T00:28:44.229475Z",
"start_time": "2024-03-01T17:17:50.518948Z" "start_time": "2024-03-02T00:28:44.047364Z"
} }
}, },
"outputs": [ "id": "aa9fd8c82c66ac9a",
"execution_count": 1
},
{ {
"name": "stdout", "cell_type": "code",
"output_type": "stream", "outputs": [],
"text": [
"{'aaab', 'aaaab', 'aab', 'a', 'aaaa', 'ab', 'b'}\n",
"['baaab', 'baa', 'baaaaa', 'baab', 'ba', 'baaaa']\n",
"['', 'ba', 'bab', 'b', 'bba', 'bb', 'bbb']\n",
"\n",
"Yes because the kleen star of l1 includes all original strings in \n",
"the language, and the string 'b', where n is zero is included in l1*, \n",
"so it is possible for l1* to have a string where there is more bs than as. \n"
]
}
],
"source": [ "source": [
"l1 = ['b', 'ba']\n", "l1 = ['b', 'ba']\n",
"l2 = ['a', 'aab', 'aaaa']\n", "l2 = ['a', 'aab', 'aaaa']\n",
@ -39,12 +53,14 @@
"\n", "\n",
"# l2 union l3\n", "# l2 union l3\n",
"l2.union(l3)\n", "l2.union(l3)\n",
"if debug:\n",
" print(l2.union(l3))\n", " print(l2.union(l3))\n",
"# l1 times l2\n", "# l1 times l2\n",
"a = []\n", "a = []\n",
"for i in l1:\n", "for i in l1:\n",
" for j in l2:\n", " for j in l2:\n",
" a.append(i + j)\n", " a.append(i + j)\n",
"if debug:\n",
" print(a)\n", " print(a)\n",
"\n", "\n",
"# kleen star of l1 up to length 3\n", "# kleen star of l1 up to length 3\n",
@ -57,85 +73,484 @@
" a.append(i + j + k)\n", " a.append(i + j + k)\n",
"# remove all strings of length greater than 3\n", "# remove all strings of length greater than 3\n",
"a = [x for x in a if len(x) <= 3]\n", "a = [x for x in a if len(x) <= 3]\n",
"print(a)\n", "if debug:\n",
" print(a)"
],
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.236163Z",
"start_time": "2024-03-02T00:28:44.231091Z"
}
},
"id": "initial_id",
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"# Question 28\n",
"## b.\n",
"aaab, aaaa, a, aaaab, b, ab, aab\n",
"\n", "\n",
"print(\"\"\"\n", "## c.\n",
"Yes because the kleen star of l1 includes all original strings in \n", "baaaaa, baaab, baa, baaaa, baab, ba\n",
"the language, and the string 'b', where n is zero is included in l1*, \n", "\n",
"so it is possible for l1* to have a string where there is more bs than as. \n", "## e. \n",
"\"\"\")" "$\\epsilon$, ba, bab, b, bba, bb, bbb\n",
] "\n",
"## h.\n",
"Yes, because the Kleen star of $L_1$ includes all original strings in the language, and the string 'b', where n is zero is included in $(L_1)^*$, so it is possible for $(L_1)^*$ to have a string where there is more bs than as. "
],
"metadata": {
"collapsed": false
},
"id": "823eb4c275b95911"
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "ae5527f9dab42f59"
},
{
"cell_type": "markdown",
"source": [
"# Question 30\n",
"## c."
],
"metadata": {
"collapsed": false
},
"id": "5f0f62a85e3cddd0"
}, },
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{ {
"name": "stdout", "data": {
"output_type": "stream", "text/plain": "<IPython.core.display.Math object>",
"text": [ "text/latex": "$\\displaystyle \\left(aa\\right)^*\\left(bbbb\\right)^*b$"
"False\n", },
"False\n", "execution_count": 3,
"True\n", "metadata": {},
"False\n", "output_type": "execute_result"
"True\n",
"\n",
"True\n",
"False\n",
"True\n",
"False\n",
"True\n",
"False\n",
"False\n",
"True\n",
"True\n",
"False\n",
"True\n"
]
} }
], ],
"source": [ "source": [
"# Define a regex where a is a multiple of 2 and b is a multiple of 4 + 1\n", "# Define a regex where 'a' is a multiple of 2 and 'b' is a multiple of 4 + 1\n",
"# a = (aa)*\n", "q31c = '(aa)*(bbbb)*b'\n",
"# b = (bbbb)*b\n", "inputs = [\n",
"# (aa)*(bbbb)*b\n", "\t'aabb',\n",
"import automata.regex.regex as regex\n", "\t'aabbbb',\n",
"from automata.fa.nfa import NFA\n", "\t'aabbbbb',\n",
"regex.validate('(aa)*(bbbb)*b')\n", "\t'aaabbbbbb',\n",
"nfa = NFA.from_regex('(aa)*(bbbb)*b')\n", "\t'aaaabbbbbbbbb',\n",
"\n", "]\n",
"print(nfa.accepts_input('aabb'))\n", "if debug:\n",
"print(nfa.accepts_input('aabbbb'))\n", " regex_test(q31c, inputs)\n",
"print(nfa.accepts_input('aabbbbb'))\n", "Math(latexify(q31c))"
"print(nfa.accepts_input('aaabbbbbb'))\n",
"print(nfa.accepts_input('aaaabbbbbbbbb'))\n",
"\n",
"print()\n",
"# Define a regex where the number of as is 1 and the number of bs is odd\n",
"a = \"((bb)*ba(bb)*)|((bb)*ab(bb)*)\"\n",
"regex.validate(a)\n",
"nfa = NFA.from_regex(a)\n",
"\n",
"print(nfa.accepts_input('ab'))\n",
"print(nfa.accepts_input('bab'))\n",
"print(nfa.accepts_input('bbab'))\n",
"print(nfa.accepts_input('bbbab'))\n",
"print(nfa.accepts_input('bbbabb'))\n",
"print(nfa.accepts_input('bbbabbb'))\n",
"print(nfa.accepts_input('abb'))\n",
"print(nfa.accepts_input('abbb'))\n",
"print(nfa.accepts_input('ba'))\n",
"print(nfa.accepts_input('bba'))\n",
"print(nfa.accepts_input('bbba'))"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-01T17:17:50.562591200Z", "end_time": "2024-03-02T00:28:44.245325Z",
"start_time": "2024-03-01T17:17:50.534567600Z" "start_time": "2024-03-02T00:28:44.237457Z"
}
},
"id": "c0d81b8fb61431de",
"execution_count": 3
},
{
"cell_type": "markdown",
"source": [
"## d."
],
"metadata": {
"collapsed": false
},
"id": "53e09c458eafb680"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle \\left(\\left(bb\\right)^*ba\\left(bb\\right)^*\\right)\\cup\\left(\\left(bb\\right)^*ab\\left(bb\\right)^*\\right)$"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a regex where the number of as is 1 and the number of bs is odd\n",
"q31e = \"((bb)*ba(bb)*)|((bb)*ab(bb)*)\"\n",
"inputs = [\n",
" 'ab',\n",
" 'bab',\n",
" 'bbab',\n",
" 'bbbabb',\n",
" 'bbbabbb',\n",
" 'abb',\n",
" 'abbb',\n",
" 'ba',\n",
" 'bba',\n",
" 'bbba',\n",
"]\n",
"if debug:\n",
" regex_test(q31e, inputs)\n",
"Math(latexify(q31e))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.251244Z",
"start_time": "2024-03-02T00:28:44.246625Z"
} }
}, },
"id": "75f2af38daefc05b", "id": "75f2af38daefc05b",
"execution_count": 39 "execution_count": 4
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "9271ff95c0925816"
},
{
"cell_type": "markdown",
"source": [
"# Question 31\n",
"## c."
],
"metadata": {
"collapsed": false
},
"id": "c4c41d43969e22bb"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle \\epsilon\\cup\\left(0\\cup1\\right)\\cup\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\cup\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\cup\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\left(0\\cup1\\right)$"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a regex where the number of 1s or 0s is less than or equal to 4\n",
"q31c = '()|(0|1)|(0|1)(0|1)|(0|1)(0|1)(0|1)|(0|1)(0|1)(0|1)(0|1)'\n",
"inputs = [\n",
" '',\n",
" '0',\n",
" '1',\n",
" '00',\n",
" '01',\n",
" '10',\n",
" '11',\n",
" '000',\n",
" '001',\n",
" '010',\n",
" '011',\n",
" '100',\n",
" '101',\n",
" '0000',\n",
" '0001',\n",
" '0010',\n",
" '0011',\n",
" '0100',\n",
" '0101',\n",
" '0110',\n",
" '0111',\n",
" '1000',\n",
" '00000',\n",
" '11111',\n",
"]\n",
"if debug:\n",
" regex_test(q31c, inputs)\n",
"Math(latexify(q31c))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.258942Z",
"start_time": "2024-03-02T00:28:44.253012Z"
}
},
"id": "404b569fb1f1b3dd",
"execution_count": 5
},
{
"cell_type": "markdown",
"source": [
"## e."
],
"metadata": {
"collapsed": false
},
"id": "64279b55169daadf"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle 0^*1\\left(00000^*1\\right)^*\\left(00000^*1\\right)0^*$"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a regex where the number of 1s is greater or equal to 2 and there are at least four zeros between each 1 and the next 1\n",
"q31e = '0*1(00000*1)*(00000*1)0*'\n",
"# 0*1 = 0 or more zeros followed by a 1\n",
"# (00000*1) = 4 or more zeros followed by a 1\n",
"inputs = [\n",
" '0100001',\n",
" '0100000100001',\n",
" '1000001',\n",
" '00000010000100000',\n",
" '1000100001',\n",
"]\n",
"if debug:\n",
" regex_test(q31e, inputs)\n",
"Math(latexify(q31e))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.265618Z",
"start_time": "2024-03-02T00:28:44.260279Z"
}
},
"id": "91ecde46ccfd6037",
"execution_count": 6
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "a5ed03840b4cc1a0"
},
{
"cell_type": "markdown",
"source": [
"# Question 32"
],
"metadata": {
"collapsed": false
},
"id": "bf475fac64302b91"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle 00100$"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let R1 be the regular expression 1* 0 1* 0 1* 0 1* 0 1*\n",
"# Let R2 be the regular expression 0* (1 ε) 0* (1 ε) 0* (1 ε) 0* (1 ε) 0*\n",
"# Let R3 be the regular expression (0 1)* 00 (0 1)\n",
"r1 = '1*01*01*01*01*'\n",
"r2 = '0*(1|())0*(1|())0*(1|())0*(1|())0*'\n",
"r3 = '(0|1)*00(0|1)'\n",
"inputs = ['00100']\n",
"if debug:\n",
" regex_test(r1, inputs)\n",
" regex_test(r2, inputs)\n",
" regex_test(r3, inputs)\n",
"Math(latexify(inputs[0]))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.272009Z",
"start_time": "2024-03-02T00:28:44.267238Z"
}
},
"id": "e3c432e6556167c7",
"execution_count": 7
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "77d9bf7018723f74"
},
{
"cell_type": "markdown",
"source": [
"# Question 33"
],
"metadata": {
"collapsed": false
},
"id": "afd92f4bb203df3a"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle \\left(c^*pc^*\\right)^*$"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a regex where no p is touching another p, and any number of c's can be between any two p's\n",
"q33 = '(c*pc*)*'\n",
"inputs = [\n",
" 'p',\n",
" 'cp',\n",
" 'pc',\n",
" 'cpc',\n",
" 'ccpcc',\n",
" 'ccpccpcc',\n",
" 'ccpccpccpcc',\n",
" 'ccpccpccpccpcc',\n",
" 'cpcpcpcpcpc',\n",
" '',\n",
"]\n",
"if debug:\n",
" regex_test(q33, inputs)\n",
"Math(latexify(q33))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.278114Z",
"start_time": "2024-03-02T00:28:44.273285Z"
}
},
"id": "5b8c9ad34523ea81",
"execution_count": 8
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "567a0f904e3a8b66"
},
{
"cell_type": "markdown",
"source": [
"# Question 34"
],
"metadata": {
"collapsed": false
},
"id": "8580d9b65031d9f7"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle \\left(\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\left(0\\cup1\\right)\\right)^*\\left(\\left(0\\cup1\\right)\\cup\\left(00\\cup01\\cup10\\cup11\\right)\\right)$"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a regex where the length of the string is not a multiple of 3, on input {0, 1}\n",
"q34 = '((0|1)(0|1)(0|1))*((0|1)|(00|01|10|11))'\n",
"inputs = [\n",
" '0', # 1\n",
" '1', # 1\n",
" '00', # 2\n",
" '01', # 2\n",
" '10', # 2 \n",
" '11', # 2\n",
" '000', # 3\n",
" '001', # 3\n",
" '010', # 3\n",
" '0000', # 4\n",
" '00001', # 5\n",
" '001100', # 6\n",
" '011001110', # 9\n",
"]\n",
"if debug:\n",
" regex_test(q34, inputs)\n",
"Math(latexify(q34))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-02T00:28:44.284200Z",
"start_time": "2024-03-02T00:28:44.279548Z"
}
},
"id": "6aa37e012a2b072d",
"execution_count": 9
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "28524e4a3d3c6e8e"
},
{
"cell_type": "markdown",
"source": [
"# Question 35\n",
"## a.\n",
"$$L = \\{ w \\in \\{ 0, 1 \\}^* \\ | n_0(w) \\text{ is a multiple of } 4 \\}$$\n",
"\n",
"## b.\n",
"$$L = \\{ a^x b^y c^z \\ | x \\geq 1, y \\geq 2, z \\geq 3 \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "1f0065878ddf68f8"
} }
], ],
"metadata": { "metadata": {