Preliminary work on A5

This commit is contained in:
Isaac Shoebottom 2024-03-01 13:21:32 -04:00
parent 54c399dcb3
commit fb6c21adcb

162
Assignment 5.ipynb Normal file
View File

@ -0,0 +1,162 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 38,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-03-01T17:17:50.546658700Z",
"start_time": "2024-03-01T17:17:50.518948Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"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": [
"l1 = ['b', 'ba']\n",
"l2 = ['a', 'aab', 'aaaa']\n",
"l3 = []\n",
"for i in range(0, 5):\n",
" l3.append('a' * i + 'b')\n",
"# turn all of these into sets\n",
"l1 = set(l1)\n",
"l2 = set(l2)\n",
"l3 = set(l3)\n",
"\n",
"# l2 union l3\n",
"l2.union(l3)\n",
"print(l2.union(l3))\n",
"# l1 times l2\n",
"a = []\n",
"for i in l1:\n",
" for j in l2:\n",
" a.append(i + j)\n",
"print(a)\n",
"\n",
"# kleen star of l1 up to length 3\n",
"a = ['']\n",
"for i in l1:\n",
" a.append(i)\n",
" for j in l1:\n",
" a.append(i + j)\n",
" for k in l1:\n",
" a.append(i + j + k)\n",
"# remove all strings of length greater than 3\n",
"a = [x for x in a if len(x) <= 3]\n",
"print(a)\n",
"\n",
"print(\"\"\"\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",
"\"\"\")"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"False\n",
"True\n",
"False\n",
"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": [
"# Define a regex where a is a multiple of 2 and b is a multiple of 4 + 1\n",
"# a = (aa)*\n",
"# b = (bbbb)*b\n",
"# (aa)*(bbbb)*b\n",
"import automata.regex.regex as regex\n",
"from automata.fa.nfa import NFA\n",
"regex.validate('(aa)*(bbbb)*b')\n",
"nfa = NFA.from_regex('(aa)*(bbbb)*b')\n",
"\n",
"print(nfa.accepts_input('aabb'))\n",
"print(nfa.accepts_input('aabbbb'))\n",
"print(nfa.accepts_input('aabbbbb'))\n",
"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": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T17:17:50.562591200Z",
"start_time": "2024-03-01T17:17:50.534567600Z"
}
},
"id": "75f2af38daefc05b",
"execution_count": 39
}
],
"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
}