This commit is contained in:
Isaac Shoebottom 2024-04-06 23:15:11 -03:00
parent fb72fe93c6
commit a8c763010a
2 changed files with 279 additions and 21 deletions

View File

@ -8,7 +8,7 @@
"from automata.fa.nfa import NFA\n", "from automata.fa.nfa import NFA\n",
"from IPython.display import Math\n", "from IPython.display import Math\n",
"\n", "\n",
"debug = False\n", "debug = True\n",
"\n", "\n",
"def regex_test(regex: str, inputs: list[str]):\n", "def regex_test(regex: str, inputs: list[str]):\n",
" re.validate(regex)\n", " re.validate(regex)\n",
@ -30,8 +30,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.229475Z", "end_time": "2024-03-22T22:22:26.498255Z",
"start_time": "2024-03-02T00:28:44.047364Z" "start_time": "2024-03-22T22:22:26.217210Z"
} }
}, },
"id": "aa9fd8c82c66ac9a", "id": "aa9fd8c82c66ac9a",
@ -39,7 +39,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'aaaab', 'a', 'b', 'aaab', 'ab', 'aaaa', 'aab'}\n",
"['baaaa', 'ba', 'baab', 'baaaaa', 'baa', 'baaab']\n",
"['', 'b', 'bb', 'bbb', 'bba', 'ba', 'bab']\n"
]
}
],
"source": [ "source": [
"l1 = ['b', 'ba']\n", "l1 = ['b', 'ba']\n",
"l2 = ['a', 'aab', 'aaaa']\n", "l2 = ['a', 'aab', 'aaaa']\n",
@ -79,8 +89,8 @@
"metadata": { "metadata": {
"collapsed": true, "collapsed": true,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.236163Z", "end_time": "2024-03-22T22:22:26.505139Z",
"start_time": "2024-03-02T00:28:44.231091Z" "start_time": "2024-03-22T22:22:26.499628Z"
} }
}, },
"id": "initial_id", "id": "initial_id",
@ -131,6 +141,17 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"aabb: False\n",
"aabbbb: False\n",
"aabbbbb: True\n",
"aaabbbbbb: False\n",
"aaaabbbbbbbbb: True\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -158,8 +179,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.245325Z", "end_time": "2024-03-22T22:22:26.515494Z",
"start_time": "2024-03-02T00:28:44.237457Z" "start_time": "2024-03-22T22:22:26.506412Z"
} }
}, },
"id": "c0d81b8fb61431de", "id": "c0d81b8fb61431de",
@ -178,6 +199,22 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ab: True\n",
"bab: False\n",
"bbab: True\n",
"bbbabb: True\n",
"bbbabbb: False\n",
"abb: False\n",
"abbb: True\n",
"ba: True\n",
"bba: False\n",
"bbba: True\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -210,8 +247,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.251244Z", "end_time": "2024-03-22T22:22:26.522964Z",
"start_time": "2024-03-02T00:28:44.246625Z" "start_time": "2024-03-22T22:22:26.517188Z"
} }
}, },
"id": "75f2af38daefc05b", "id": "75f2af38daefc05b",
@ -241,6 +278,36 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
": True\n",
"0: True\n",
"1: True\n",
"00: True\n",
"01: True\n",
"10: True\n",
"11: True\n",
"000: True\n",
"001: True\n",
"010: True\n",
"011: True\n",
"100: True\n",
"101: True\n",
"0000: True\n",
"0001: True\n",
"0010: True\n",
"0011: True\n",
"0100: True\n",
"0101: True\n",
"0110: True\n",
"0111: True\n",
"1000: True\n",
"00000: False\n",
"11111: False\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -287,8 +354,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.258942Z", "end_time": "2024-03-22T22:22:26.531449Z",
"start_time": "2024-03-02T00:28:44.253012Z" "start_time": "2024-03-22T22:22:26.524511Z"
} }
}, },
"id": "404b569fb1f1b3dd", "id": "404b569fb1f1b3dd",
@ -307,6 +374,17 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0100001: True\n",
"0100000100001: True\n",
"1000001: True\n",
"00000010000100000: True\n",
"1000100001: False\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -336,8 +414,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.265618Z", "end_time": "2024-03-22T22:22:26.538114Z",
"start_time": "2024-03-02T00:28:44.260279Z" "start_time": "2024-03-22T22:22:26.532887Z"
} }
}, },
"id": "91ecde46ccfd6037", "id": "91ecde46ccfd6037",
@ -366,6 +444,15 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"00100: True\n",
"00100: True\n",
"00100: False\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -380,6 +467,7 @@
"# Let R1 be the regular expression 1* 0 1* 0 1* 0 1* 0 1*\n", "# 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 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", "# Let R3 be the regular expression (0 1)* 00 (0 1)\n",
"# Was not supposed to be accepted by all, only bt 2 and 3\n",
"r1 = '1*01*01*01*01*'\n", "r1 = '1*01*01*01*01*'\n",
"r2 = '0*(1|())0*(1|())0*(1|())0*(1|())0*'\n", "r2 = '0*(1|())0*(1|())0*(1|())0*(1|())0*'\n",
"r3 = '(0|1)*00(0|1)'\n", "r3 = '(0|1)*00(0|1)'\n",
@ -393,8 +481,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.272009Z", "end_time": "2024-03-22T22:22:26.547221Z",
"start_time": "2024-03-02T00:28:44.267238Z" "start_time": "2024-03-22T22:22:26.539392Z"
} }
}, },
"id": "e3c432e6556167c7", "id": "e3c432e6556167c7",
@ -423,6 +511,22 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p: True\n",
"cp: True\n",
"pc: True\n",
"cpc: True\n",
"ccpcc: True\n",
"ccpccpcc: True\n",
"ccpccpccpcc: True\n",
"ccpccpccpccpcc: True\n",
"cpcpcpcpcpc: True\n",
"pp: True\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -446,7 +550,7 @@
" 'ccpccpccpcc',\n", " 'ccpccpccpcc',\n",
" 'ccpccpccpccpcc',\n", " 'ccpccpccpccpcc',\n",
" 'cpcpcpcpcpc',\n", " 'cpcpcpcpcpc',\n",
" '',\n", " 'pp', # Not supposed to be here\n",
"]\n", "]\n",
"if debug:\n", "if debug:\n",
" regex_test(q33, inputs)\n", " regex_test(q33, inputs)\n",
@ -455,8 +559,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.278114Z", "end_time": "2024-03-22T22:22:26.555155Z",
"start_time": "2024-03-02T00:28:44.273285Z" "start_time": "2024-03-22T22:22:26.549229Z"
} }
}, },
"id": "5b8c9ad34523ea81", "id": "5b8c9ad34523ea81",
@ -485,6 +589,25 @@
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0: True\n",
"1: True\n",
"00: True\n",
"01: True\n",
"10: True\n",
"11: True\n",
"000: False\n",
"001: False\n",
"010: False\n",
"0000: True\n",
"00001: True\n",
"001100: False\n",
"011001110: False\n"
]
},
{ {
"data": { "data": {
"text/plain": "<IPython.core.display.Math object>", "text/plain": "<IPython.core.display.Math object>",
@ -520,8 +643,8 @@
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"ExecuteTime": { "ExecuteTime": {
"end_time": "2024-03-02T00:28:44.284200Z", "end_time": "2024-03-22T22:22:26.563180Z",
"start_time": "2024-03-02T00:28:44.279548Z" "start_time": "2024-03-22T22:22:26.556508Z"
} }
}, },
"id": "6aa37e012a2b072d", "id": "6aa37e012a2b072d",

135
Assignment 7.ipynb Normal file
View File

@ -0,0 +1,135 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a90ef9f8022df10e",
"metadata": {
"collapsed": false
},
"source": [
"# Question 42\n",
"## c.\n",
"$$S \\rightarrow 0S0 | 0S1 | 1S0 | 1S1 | 1$$\n",
"\n",
"## d.\n",
"$$S \\rightarrow X | \\epsilon$$\n",
"$$X \\rightarrow aXdd | Y$$\n",
"$$Y \\rightarrow bYd | Z$$\n",
"$$Z \\rightarrow cZ | \\epsilon$$\n",
"\n",
"## e.\n",
"$$S \\rightarrow 0S | 1S | 00S | 01S | 10S | 11S | 000S | 001S | 010S | 011S| 100S | 101S | 111S | \\epsilon$$"
]
},
{
"cell_type": "markdown",
"id": "2d1690ef",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "9a7c595f",
"metadata": {},
"source": [
"# Question 43\n",
"## a.\n",
"$$B = \\{ w \\in \\{a, b, c\\}^* | |w| = 4n+1, n \\in \\mathbb{Z}^{\\text{nonneg}} \\}$$\n",
"\n",
"## b.\n",
"$$B = \\{ 0^2x1^3y+2 | x, y \\in \\mathbb{Z}^{\\text{nonneg}} \\}$$\n",
"\n",
"## c.\n",
"$$B = \\{ 0^x1^y | x \\neq y \\text{ and } x, y \\in \\mathbb{Z}^{\\text{nonneg}} \\}$$"
]
},
{
"cell_type": "markdown",
"id": "c8d0ce5a",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "8d439d76",
"metadata": {},
"source": [
"# Question 44\n",
"## a.\n",
"$$B = \\{ 0^x1^y | y = x + 1 \\text{ and } x, y \\in \\mathbb{Z}^{\\text{nonneg}} \\}$$\n",
"\n",
"## c.\n",
"$$B = \\{ 0^x1^y | y = 3x \\text{ and } x, y \\in \\mathbb{Z}^{\\text{nonneg}} \\}$$"
]
},
{
"cell_type": "markdown",
"id": "323bea35",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "56466c7f",
"metadata": {},
"source": [
"# Question 45\n",
"1. First the machine should move left to right looking for a \"b\". If it does not find one it moves to the reject state and if it finds one it will:\n",
" - Erase the \"b\"\n",
" - Move back to the start\n",
" - Look for an \"a\". If it finds one it will repeat step 1. If it does not find one, it will move to the accept state"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "$\\pagebreak$",
"id": "e9fba2ef63041f08"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"# Question 47\n",
"1. First the machine should check if the length of the string is at least 6 and if not, reject\n",
"2. Copy 0 or more symbols to tape 2 from tape 1\n",
"3. Erase all of tape 2\n",
"4. Copy 3 symbols to tape 2 from tape 1\n",
"5. Copy 0 or more symbols to tape 3 from tape 1\n",
"6. Erase of all of tape 3\n",
"7. Copy 3 symbols to tape 3 from tape 1\n",
"8. Compare tapes 2 and 3 and if they match, accept, otherwise reject\n",
"\n",
"The nondeterminism of this machine is the copying 0 or more symbols, as this accounts for strings u and x, as they can be any length including 0, and the machine can make a choice of 0 or more."
],
"id": "7b6c771c1004546f"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}