Compare commits

...

28 Commits

Author SHA1 Message Date
7c80576cc4 A8 2024-04-11 23:20:56 -03:00
a8c763010a A7 2024-04-06 23:15:11 -03:00
fb72fe93c6 Whatever man 2024-03-16 23:26:32 -03:00
0d76b15bbb Finish A5 2024-03-01 20:30:43 -04:00
fb6c21adcb Preliminary work on A5 2024-03-01 13:21:32 -04:00
54c399dcb3 Add assignment 4 2024-02-16 20:52:28 -04:00
816c195ca9 Reorganize drawing stuff 2024-02-10 18:52:25 -04:00
8ebf6af159 Add A3 graph drawings 2024-02-10 18:51:03 -04:00
1a3a9140a7 Remove unused run configs 2024-02-10 18:41:32 -04:00
3f82e236d6 Add A3 2024-02-10 18:41:16 -04:00
95e4b72316 Save a2 build 2024-01-26 18:17:29 -04:00
d4f41fbd44 Add a3 skeleton 2024-01-26 18:17:03 -04:00
1e91ba5ba6 Finish a2 2024-01-26 18:16:56 -04:00
0b83fe9bfb Use wsl shell 2024-01-26 17:07:27 -04:00
487859e9d6 Reorganize 2024-01-26 15:05:29 -04:00
9990c0e8a1 pycharm shit 2024-01-26 00:00:24 -04:00
bf7f83f9cb Start A2 2024-01-25 23:59:58 -04:00
bae7ee7cd1 Stupid windows python shit 2024-01-25 23:59:46 -04:00
cec9e9104f Rephrase q3 2024-01-19 14:00:50 -04:00
fd4359943e pycache 2024-01-18 17:48:58 -04:00
20fdb9413d update a1 notebook 2024-01-18 17:48:05 -04:00
3fe5f5a9e3 run config 2024-01-18 17:46:11 -04:00
a62334b3a4 run config 2024-01-18 17:45:59 -04:00
65f5adc2ac Assignment 1 notebook 2024-01-18 17:27:31 -04:00
fd6f8c2a05 gitignore 2024-01-18 17:27:22 -04:00
79c423e059 pycharm shit 2024-01-18 17:27:07 -04:00
f7ddd04a42 Run configs 2024-01-18 17:26:44 -04:00
50518cc3c3 Finish hw 2 2024-01-18 17:25:55 -04:00
29 changed files with 3345 additions and 62 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.ipynb_checkpoints
*.pdf
__pycache__

2
.idea/.gitignore generated vendored
View File

@ -6,3 +6,5 @@
# Datasource local storage ignored files # Datasource local storage ignored files
/dataSources/ /dataSources/
/dataSources.local.xml /dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

9
.idea/CS2333.iml generated
View File

@ -2,9 +2,14 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/.ipynb_checkpoints" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.9.18 WSL (RockyLinux): (/home/isaac/.virtualenvs/CS2333/bin/python)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PackageRequirementsSettings">
<option name="removeUnused" value="true" />
<option name="keepMatchingSpecifier" value="false" />
</component>
</module> </module>

21
.idea/deployment.xml generated Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="ishoebot@cs3103:22 agent">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
<paths name="ishoebot@cs3103:22 agent (2)">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>

2
.idea/misc.xml generated
View File

@ -7,5 +7,5 @@
<option name="show" value="ASK" /> <option name="show" value="ASK" />
<option name="description" value="" /> <option name="description" value="" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (CS2333)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9.18 WSL (RockyLinux): (/home/isaac/.virtualenvs/CS2333/bin/python)" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A1 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 1.ipynb&quot; --to webpdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A2 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 2.ipynb&quot; --to webpdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A3 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 3.ipynb&quot; --to pdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A4 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 4.ipynb&quot; --to pdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A5 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 5.ipynb&quot; --to pdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A6 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 6.ipynb&quot; --to pdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A7 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 7.ipynb&quot; --to pdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Export A8 No Code" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="jupyter-nbconvert.exe &quot;Assignment 8.ipynb&quot; --to pdf --no-input" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="false" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -1,58 +0,0 @@
from util import *
def p1():
alphabet = ['a', 'b']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)
def number_of_a_is_even(s):
return s.count('a') % 2 == 0
strings = list(filter(number_of_a_is_even, strings))
return strings
def p2():
possible_m = [0, 1, 2, 3, 4]
possible_n = [0, 1, 2, 3, 4]
possible_p = [0, 1, 2, 3, 4]
strings = []
for m in possible_m:
for n in possible_n:
for p in possible_p:
# First rule
if m + n + p <= 4:
# Second rule
if m > n:
# Third rule
if n == p:
strings.append('a' * m + 'b' * n + 'c' * p)
return strings
def p3():
alphabet = ['a', 'b', 'c']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 4)
def number_of_a_is_greater_than_number_of_b(s):
return s.count('a') > s.count('b')
def number_of_b_is_equal_to_number_of_c(s):
return s.count('b') == s.count('c')
strings = list(filter(number_of_a_is_greater_than_number_of_b, strings))
strings = list(filter(number_of_b_is_equal_to_number_of_c, strings))
return strings
def main():
print_strings(p1())
print_strings(p2())
print_strings(p3())
if __name__ == '__main__':
main()

583
Assignment 1.ipynb Normal file
View File

@ -0,0 +1,583 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"### <center> Assignment 1 \n",
"#### <center> January 18, 2024\n",
"##### <center> Isaac Shoebottom (3429069)"
],
"metadata": {
"collapsed": false
},
"id": "9ec6bd31bcc8447b"
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7709a8186ebbb39a",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.812390600Z",
"start_time": "2024-01-19T18:00:01.808045100Z"
}
},
"outputs": [],
"source": [
"# Helper functions\n",
"def get_all_strings_with_a_given_alphabet_and_length(_alphabet, _length):\n",
"\t_strings = ['']\n",
"\tfor itr in range(_length):\n",
"\t\t_string = [s + c for s in _strings for c in _alphabet]\n",
"\t\t_strings += _string\n",
"\n",
"\t# Remove duplicates (set() isn't as nice as it doesn't preserve order, at least for verification purposes)\n",
"\t_strings = list(dict.fromkeys(_strings))\n",
"\treturn _strings\n",
"\n",
"\n",
"def replace_empty_string_with_symbol(_strings):\n",
"\tsymbol = 'ε'\n",
"\tfor itr in range(len(_strings)):\n",
"\t\tif _strings[itr] == '':\n",
"\t\t\t_strings[itr] = symbol\n",
"\treturn _strings\n",
"\n",
"\n",
"def print_strings(_strings):\n",
"\t_strings = replace_empty_string_with_symbol(_strings)\n",
"\t# print(len(strings))\n",
"\tprint(_strings)"
]
},
{
"cell_type": "markdown",
"source": [
"<div style=\"page-break-after: always;\"></div>\n",
"Question 1"
],
"metadata": {
"collapsed": false
},
"id": "cf6146860173f94f"
},
{
"cell_type": "markdown",
"source": [
"(a) $L_1 = \\{0^m 1^n | m,n \\in \\mathbb{Z}^*, n \\leq 2, m \\leq n\\}$"
],
"metadata": {
"collapsed": false
},
"id": "b12165c136baa772"
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a3ebc6e89e38cc93",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.833000200Z",
"start_time": "2024-01-19T18:00:01.813388900Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ε', '1', '01', '11', '011', '0011']\n"
]
}
],
"source": [
"# Language rule 1, n is less than or equal to 2\n",
"n = 2\n",
"# Language rule 2, m is less than or equal to n\n",
"m = n\n",
"strings = []\n",
"# Language rule 3, n and m are both non-negative integers\n",
"for i in range(0, n + 1):\n",
"\tfor j in range(0, m + 1):\n",
"\t\tif j > i:\n",
"\t\t\tcontinue\n",
"\t\tstrings.append('0' * j + '1' * i)\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "b821cc2e0085663a",
"metadata": {
"collapsed": false
},
"source": [
"(b) $L_2 = \\{a^m b^n | m,n \\in \\mathbb{Z}^*, 2 \\leq n \\leq 6, n \\text{ is a multiple of } m\\}$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1d32fddeec3be96f",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.833998900Z",
"start_time": "2024-01-19T18:00:01.819489500Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['abb', 'aabb', 'abbb', 'aaabbb', 'abbbb', 'aabbbb', 'aaaabbbb', 'abbbbb', 'aaaaabbbbb', 'abbbbbb', 'aabbbbbb', 'aaabbbbbb', 'aaaaaabbbbbb']\n"
]
}
],
"source": [
"possible_n = [2, 3, 4, 5, 6]\n",
"\n",
"strings = []\n",
"for i in possible_n:\n",
"\tinteger_divisors = []\n",
"\tfor j in range(1, i + 1):\n",
"\t\tif i % j == 0:\n",
"\t\t\tinteger_divisors.append(j)\n",
"\tfor j in integer_divisors:\n",
"\t\tstrings.append('a' * j + 'b' * i)\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "2056f304860447ea",
"metadata": {
"collapsed": false
},
"source": [
"(c) $L_3 = \\{a^n b^{2n-1} c^{3n-2} | n \\in \\mathbb{Z}^+, n \\leq 3\\}$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a5ba1ad20a2b94e0",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.833998900Z",
"start_time": "2024-01-19T18:00:01.824503300Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['abc', 'aabbbcccc', 'aaabbbbbccccccc']\n"
]
}
],
"source": [
"possible_n = [1, 2, 3]\n",
"\n",
"strings = []\n",
"for i in possible_n:\n",
"\tan = i\n",
"\tbn = 2 * i - 1\n",
"\tcn = 3 * i - 2\n",
"\tstrings.append('a' * an + 'b' * bn + 'c' * cn)\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "520023ded45b27da",
"metadata": {
"collapsed": false
},
"source": [
"(d) $L_4 = \\{w \\in \\{a,b\\}^* | |w| \\leq 3 \\text{ and } w \\text{ is a palindrome} \\}$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "452a62fd365b4f58",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.836315700Z",
"start_time": "2024-01-19T18:00:01.830000300Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ε', 'a', 'b', 'aa', 'bb', 'aaa', 'aba', 'bab', 'bbb']\n"
]
}
],
"source": [
"alphabet = ['a', 'b']\n",
"# include empty string\n",
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)\n",
"def is_palindrome(s):\n",
"\treturn s == s[::-1]\n",
"strings = list(filter(is_palindrome, strings))\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "9dff8b1af013d957",
"metadata": {
"collapsed": false
},
"source": [
"<div style=\"page-break-after: always;\"></div>\n",
"Question 2"
]
},
{
"cell_type": "markdown",
"id": "ad64b6ae580ba4dd",
"metadata": {
"collapsed": false
},
"source": [
"(a) $L_5 = \\{w \\in \\{a,b\\}^* | |w| \\leq 3 \\text{ and } n_a(w) \\text{ is even} \\}$"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d5167f62fda41135",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.843841300Z",
"start_time": "2024-01-19T18:00:01.834999100Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ε', 'b', 'aa', 'bb', 'aab', 'aba', 'baa', 'bbb']\n"
]
}
],
"source": [
"alphabet = ['a', 'b']\n",
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)\n",
"\n",
"def number_of_a_is_even(s):\n",
"\treturn s.count('a') % 2 == 0\n",
"\n",
"strings = list(filter(number_of_a_is_even, strings))\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "9f8b05d94c453058",
"metadata": {
"collapsed": false
},
"source": [
"(b) $L_6 = \\{a^m b^n c^p | m,n,p \\in \\mathbb{Z}^*, m + n + p \\leq 4, m > n, n = p\\}$"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9b3b29ce2400f4ab",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.924237300Z",
"start_time": "2024-01-19T18:00:01.838835700Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'aa', 'aabc', 'aaa', 'aaaa']\n"
]
}
],
"source": [
"possible_m = [0, 1, 2, 3, 4]\n",
"possible_n = [0, 1, 2, 3, 4]\n",
"possible_p = [0, 1, 2, 3, 4]\n",
"\n",
"strings = []\n",
"for m in possible_m:\n",
"\tfor n in possible_n:\n",
"\t\tfor p in possible_p:\n",
"\t\t\t# First rule\n",
"\t\t\tif m + n + p <= 4:\n",
"\t\t\t\t# Second rule\n",
"\t\t\t\tif m > n:\n",
"\t\t\t\t\t# Third rule\n",
"\t\t\t\t\tif n == p:\n",
"\t\t\t\t\t\tstrings.append('a' * m + 'b' * n + 'c' * p)\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "7626dea00c2aeda4",
"metadata": {
"collapsed": false
},
"source": [
"(c) $L_7 = \\{w \\in \\{a,b,c\\}^* | |w| \\leq 4, n_a(w) > n_b(w), n_b(w) = n_c(w)\\}$"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8cbf5de6f2655673",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.925239100Z",
"start_time": "2024-01-19T18:00:01.844838400Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'aa', 'aaa', 'aaaa', 'aabc', 'aacb', 'abac', 'abca', 'acab', 'acba', 'baac', 'baca', 'bcaa', 'caab', 'caba', 'cbaa']\n"
]
}
],
"source": [
"alphabet = ['a', 'b', 'c']\n",
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 4)\n",
"\n",
"def number_of_a_is_greater_than_number_of_b(s):\n",
"\treturn s.count('a') > s.count('b')\n",
"\n",
"def number_of_b_is_equal_to_number_of_c(s):\n",
"\treturn s.count('b') == s.count('c')\n",
"\n",
"strings = list(filter(number_of_a_is_greater_than_number_of_b, strings))\n",
"strings = list(filter(number_of_b_is_equal_to_number_of_c, strings))\n",
"\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "88faa9e0347d6fd8",
"metadata": {
"collapsed": false
},
"source": [
"<div style=\"page-break-after: always;\"></div>\n",
"Question 3"
]
},
{
"cell_type": "markdown",
"id": "efdcdcb4f9a0298",
"metadata": {
"collapsed": false
},
"source": [
"First, it could be helpful to find the set of all valid games to compare against. This is a little tricky as first you need to know the rules of the game and encode them correctly as a language. The rules that I have discerned are as follows:\n",
"1. Each valid game has 3 a's or 3 b's, denoting a victory for one side\n",
"2. Once either side has won, the game does not continue, and the string is complete\n",
"\n",
"$$L = \\{w \\in \\{a,b\\}^* | n_a(w) = 3 \\text{ or } n_b(w) = 3 \\text{ and } w \\text{ does not end with the opposite letter}\\}$$"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "660a123373b6ff39",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.926239100Z",
"start_time": "2024-01-19T18:00:01.849939800Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['aaa', 'bbb', 'aaba', 'abaa', 'abbb', 'baaa', 'babb', 'bbab', 'aabba', 'aabbb', 'ababa', 'ababb', 'abbaa', 'abbab', 'baaba', 'baabb', 'babaa', 'babab', 'bbaaa', 'bbaab']\n"
]
}
],
"source": [
"alphabet = ['a', 'b']\n",
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)\n",
"\n",
"def number_of_a_is_equal_to_3(s):\n",
"\treturn s.count('a') == 3\n",
"\n",
"def number_of_b_is_equal_to_3(s):\n",
"\treturn s.count('b') == 3\n",
"\n",
"# A game stops when a player has 3 a's or 3 b's, so there should be no trailing wins from the losing player\n",
"def valid_game(s):\n",
"\tif number_of_a_is_equal_to_3(s):\n",
"\t\treturn not s.endswith('b')\n",
"\telif number_of_b_is_equal_to_3(s):\n",
"\t\treturn not s.endswith('a')\n",
"\n",
"valid_strings = list(filter(valid_game, strings))\n",
"print_strings(valid_strings)"
]
},
{
"cell_type": "markdown",
"id": "bcbd29f7048b2af2",
"metadata": {
"collapsed": false
},
"source": [
"(a) $L_1 = \\{w \\in \\{a,b\\}^* | n_a(w) = 3 \\text{ or } n_b(w) = 3 \\}$\n",
"\n",
"This language is not representable with code, as the set is produces is infinite. This language places no restrictions on the length of the string, so \"aaabbbbbbbbbbbbbbbbbbbbbbbbbbb\" is a valid string in this language. So here is a few examples of valid strings in this language that are not valid in the original:\n",
"\n",
"1. \"aaabbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n",
"2. \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaa\"\n",
"3. \"bababaaaaaaaaaaaaaaaaaaaaaaaaaa\""
]
},
{
"cell_type": "markdown",
"id": "7afad368b9ad3a09",
"metadata": {
"collapsed": false
},
"source": [
"(b) $L_2 = \\{w \\in \\{a,b\\}^* | |w| \\leq 5, \\text{ and } n_a(w) = 3 \\text{ or } n_b(w) = 3 \\}$\n",
"\n",
"This language differs as it is not encoding the rule that the game stops once a player has 3 a's or 3 b's. This can be seen in these strings that are valid in this language but not in the original language L:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "53dc46669ffac28c",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.926239100Z",
"start_time": "2024-01-19T18:00:01.854940200Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['aaab', 'bbba', 'aaabb', 'aabab', 'abaab', 'abbba', 'baaab', 'babba', 'bbaba', 'bbbaa']\n"
]
}
],
"source": [
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)\n",
"\n",
"def number_of_a_or_b_is_equal_to_3(s):\n",
"\treturn s.count('a') == 3 or s.count('b') == 3\n",
"\n",
"strings = list(filter(number_of_a_or_b_is_equal_to_3, strings))\n",
"strings = [i for i in strings if i not in valid_strings] # strings - valid_strings\n",
"print_strings(strings)"
]
},
{
"cell_type": "markdown",
"id": "990482f3b9b9e5b6",
"metadata": {
"collapsed": false
},
"source": [
"(c) $L_3 = \\{w \\in \\{a,b\\}^* | (n_a(w) = 3 \\text{ and } n_b(w) < 3) \\text{ or } (n_b(w) = 3 \\text{ and } n_a(w) < 3) \\}$\n",
"\n",
"This language is similar to the previous one, in that it does not encode the rule that the game stops once a player has 3 a's or 3 b's. The difference in this language is that the limit of string length 5 is not explicitly stated, however it is encoded to be that way by saying that if there are 3 a's then there must be less than 3 b's, and vice versa. This means that the effective maximum length of the string is 5, and thus the language is equivalent to the language from the previous question.\n",
"$$\n",
"x, y \\in \\mathbb{Z}^* \\\\\n",
"x = 3 \\\\\n",
"y = < 3 \\\\\n",
"< 3 = \\{0, 1, 2\\} \\\\\n",
"x + y = \\{3, 4, 5\\}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "397defd138f4789a",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-19T18:00:01.927237Z",
"start_time": "2024-01-19T18:00:01.860724500Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['aaab', 'bbba', 'aaabb', 'aabab', 'abaab', 'abbba', 'baaab', 'babba', 'bbaba', 'bbbaa']\n"
]
}
],
"source": [
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)\n",
"\n",
"def opposite_number_of_winner_is_less_than_3(s):\n",
"\tif number_of_a_is_equal_to_3(s):\n",
"\t\treturn s.count('b') < 3\n",
"\telif number_of_b_is_equal_to_3(s):\n",
"\t\treturn s.count('a') < 3\n",
"\n",
"strings = list(filter(opposite_number_of_winner_is_less_than_3, strings))\n",
"strings = [i for i in strings if i not in valid_strings] # strings - valid_strings\n",
"print_strings(strings)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

282
Assignment 2.ipynb Normal file
View File

@ -0,0 +1,282 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Question 6\n",
"Let $S = \\{0,1\\}^*$ be the set of all strings of zero and ones, which includes the empty string $\\epsilon$.\n",
"Let $h : S \\rightarrow \\mathbb{Z}^*$ be the function defined by $h(x)$ equal the number of zeros in $x$ multiplied by the number of ones in $x$\n",
"For example, $h(00100011) = 5 \\times 3 = 15, and h(111) = 0 \\times 3 = 0$\n",
"\n",
"(a) Is $h$ one-to-one? No, because both strings $001$ and $110$ map to the same value, $2$\n",
"$2 \\times 1 = 2$ and $1 \\times 2 = 2$\n",
"\n",
"(b) Is $h$ onto? Yes, because you can find every non-negative integer by multiplying any number of ones by one zero $(1 \\times 1), (1 \\times 2), ...$\n",
"Let the number of zeros be exactly 1, and n be the number of ones, and m be any non-negative integer\n",
"$h(n) = m, 1n = m, n = m$ "
],
"metadata": {
"collapsed": false
},
"id": "b67364155fcb1072"
},
{
"cell_type": "markdown",
"source": [
"<div style=\"page-break-after: always;\"></div>"
],
"metadata": {
"collapsed": false
},
"id": "ec5269e5b4749d08"
},
{
"cell_type": "markdown",
"source": [
"# Question 8\n",
"(a)\n",
"This function is not one-to-one (111110, 1111100 are both 5)\n",
"This function is onto (10, 110, 1110, 11...0 is all non-negative integers)\n",
"\n",
"(b)\n",
"This function is not one-to-one (11111, 01111 are both 1)\n",
"This function is onto (All strings are mapped to, as you can simply pad any character with any 4 characters first)\n",
"\n",
"(c)\n",
"This function is not one-to-one (Quebec, Yukon both are 0)\n",
"This function is not onto (None of the provinces contain 3 or 5 a's"
],
"metadata": {
"collapsed": false
},
"id": "ead9052998c5edf6"
},
{
"cell_type": "markdown",
"source": [
"<div style=\"page-break-after: always;\"></div>"
],
"metadata": {
"collapsed": false
},
"id": "f27520443479cce9"
},
{
"cell_type": "markdown",
"source": [
"# Question 9\n",
"Consider the relation $R$ defined on the set $\\mathbb{Z}$ as follows:\n",
"$$R = \\{(m,n) | m,n \\in \\mathbb{Z}, mn < 0\\}$$\n",
"\n",
"(a) Is the relation reflexive? No\n",
"For every integer $x$, $x \\times x = x^2$, and by the definition of squares, can never be less than zero\n",
"\n",
"(b) Is the relation symmetric? Yes\n",
"Let $x, y$ be a pair of integers in the relation and $z$ be the product of $x and y$, so $x \\times y = -z$\n",
"Since multiplication is commutative, the position of $x$ and $y$ do not matter, so $(x,y)$ and $(y,x)$ are in the relation\n",
"(Both $x \\times y$ and $y \\times x$ equal $-z$)\n",
"\n",
"(c) Is the relation transitive? No\n",
"Let $a = 1, b = -2, c = 3$\n",
"$1 \\times -2 = -2$ and $-2 \\times 3 = -6$, however $1 \\times 3 = 3$, which is $> 0$\n",
"$\\therefore$ this relation is not transitive\n",
"\n",
"(d) Is this an equivalence relation? No, because these three conditions are not met"
],
"metadata": {
"collapsed": false
},
"id": "874c8dbcc345edad"
},
{
"cell_type": "markdown",
"source": [
"<div style=\"page-break-after: always;\"></div>"
],
"metadata": {
"collapsed": false
},
"id": "ca477f12a96f397a"
},
{
"cell_type": "markdown",
"source": [
"# Question 11\n",
"Consider the relation $R$ defined on the set $\\mathbb{Z}$ as follows:\n",
"$$\\forall m,n \\in \\mathbb{Z}, (m,n) \\in R \\text{ if and only if } m + n = 2k \\text{ for some integer } k$$\n",
"\n",
"(a) Is this relation reflexive? Yes\n",
"For every integer $x$, $x + x = 2x$, therefore it is reflexive\n",
"\n",
"(b) Is this relation symmetric? Yes\n",
"Let $x, y$ be a pair of integers in the relation and $z$ be the sum of these integers, so $x + y = 2z$\n",
"Since addition is commutative, the position of $x$ and $y$ do not matter, so $(x,y) and $(y,x) are in the relation\n",
"(Both $x + y$ and $y + x$ equal $2z$)\n",
"\n",
"(c) Is this relation transitive? Yes\n",
"Let $(a,b)$ and $(b,c)$ be valid pairs of integers of the relation $R$\n",
"$a + b = 2n$, and $b + c = 2p$, then $b = 2n - a$ and $b = 2p - c$\n",
"$-2n + a = 2p - c$\n",
"$a + c = 2p + 2n$\n",
"Since the sum of a and c is the sum of two even numbers (numbers multiplied by 2 must be even), then the result must be even, so the result is divisible by 2\n",
"$\\therefore$ The relationship is transitive"
],
"metadata": {
"collapsed": false
},
"id": "5fe823d05dd08b12"
},
{
"cell_type": "markdown",
"source": [
"<div style=\"page-break-after: always;\"></div>"
],
"metadata": {
"collapsed": false
},
"id": "889a2a7359618ee7"
},
{
"cell_type": "markdown",
"source": [
"# Question 14\n",
"There are 7 equivalence classes as the results are grouped by the integer returned by $n_0(x) - n_1(x)$/$n_0(y) - n_1(y)$ for any relation pair, the results are that being set of differences $\\{-3, -2, -1, 0, 1, 2, 3\\}$"
],
"metadata": {
"collapsed": false
},
"id": "fd69c73a15e8e6a0"
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{-3: [('111', '111')],\n",
" -2: [('11', '11')],\n",
" -1: [('1', '1'),\n",
" ('1', '011'),\n",
" ('1', '101'),\n",
" ('1', '110'),\n",
" ('011', '1'),\n",
" ('011', '011'),\n",
" ('011', '101'),\n",
" ('011', '110'),\n",
" ('101', '1'),\n",
" ('101', '011'),\n",
" ('101', '101'),\n",
" ('101', '110'),\n",
" ('110', '1'),\n",
" ('110', '011'),\n",
" ('110', '101'),\n",
" ('110', '110')],\n",
" 0: [('', ''),\n",
" ('', '01'),\n",
" ('', '10'),\n",
" ('01', ''),\n",
" ('01', '01'),\n",
" ('01', '10'),\n",
" ('10', ''),\n",
" ('10', '01'),\n",
" ('10', '10')],\n",
" 1: [('0', '0'),\n",
" ('0', '001'),\n",
" ('0', '010'),\n",
" ('0', '100'),\n",
" ('001', '0'),\n",
" ('001', '001'),\n",
" ('001', '010'),\n",
" ('001', '100'),\n",
" ('010', '0'),\n",
" ('010', '001'),\n",
" ('010', '010'),\n",
" ('010', '100'),\n",
" ('100', '0'),\n",
" ('100', '001'),\n",
" ('100', '010'),\n",
" ('100', '100')],\n",
" 2: [('00', '00')],\n",
" 3: [('000', '000')]}\n"
]
}
],
"source": [
"# Set of all strings containing 0 and 1 up to length 3\n",
"def get_all_strings_with_a_given_alphabet_and_length(_alphabet, _length):\n",
"\t_strings = ['']\n",
"\tfor i in range(_length):\n",
"\t\ts = [s + c for s in _strings for c in _alphabet]\n",
"\t\t_strings += s\n",
"\n",
"\t# Remove duplicates (set() isn't as nice as it doesn't preserve order, at least for verification purposes)\n",
"\t_strings = list(dict.fromkeys(_strings))\n",
"\treturn _strings\n",
"\n",
"alphabet = ['0', '1']\n",
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)\n",
"\n",
"# The relation is valid if the number of zeros from string x minus the number of ones from string x is equal to the number of zeros from string y minus the number of ones from string y\n",
"def is_valid_relation(_x, _y):\n",
"\t# Count the number of zeros and ones in each string\n",
"\tx_zeros = _x.count('0')\n",
"\tx_ones = _x.count('1')\n",
"\ty_zeros = _y.count('0')\n",
"\ty_ones = _y.count('1')\n",
"\n",
"\t# Return true if the difference between the number of zeros and ones is equal\n",
"\treturn (x_zeros - x_ones) == (y_zeros - y_ones)\n",
"\n",
"from collections import defaultdict\n",
"\n",
"# Print all valid relations\n",
"equivalence_classes = defaultdict(list)\n",
"for x in strings:\n",
"\tfor y in strings:\n",
"\t\tif is_valid_relation(x, y):\n",
"\t\t\t# create a tuple of the two strings\n",
"\t\t\tt = (x, y)\n",
"\t\t\t# calculate the difference between the number of zeros and ones\n",
"\t\t\td = x.count('0') - x.count('1')\n",
"\t\t\t# add the tuple a list under the key of the difference\n",
"\t\t\tequivalence_classes[d].append(t)\n",
"\t\t\t\t\n",
"# Print the dict\n",
"equivalence_classes = dict(equivalence_classes)\n",
"from pprint import pprint\n",
"pprint(equivalence_classes)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-26T22:03:01.411977200Z",
"start_time": "2024-01-26T22:03:01.364977700Z"
}
},
"id": "94712ae197e8639c",
"execution_count": 5
}
],
"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
}

562
Assignment 3.ipynb Normal file

File diff suppressed because one or more lines are too long

495
Assignment 4.ipynb Normal file

File diff suppressed because one or more lines are too long

700
Assignment 5.ipynb Normal file
View File

@ -0,0 +1,700 @@
{
"cells": [
{
"cell_type": "code",
"outputs": [],
"source": [
"import automata.regex.regex as re\n",
"from automata.fa.nfa import NFA\n",
"from IPython.display import Math\n",
"\n",
"debug = True\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": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-22T22:22:26.498255Z",
"start_time": "2024-03-22T22:22:26.217210Z"
}
},
"id": "aa9fd8c82c66ac9a",
"execution_count": 1
},
{
"cell_type": "code",
"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": [
"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",
"if debug:\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",
"if debug:\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",
"if debug:\n",
" print(a)"
],
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-03-22T22:22:26.505139Z",
"start_time": "2024-03-22T22:22:26.499628Z"
}
},
"id": "initial_id",
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"# Question 28\n",
"## b.\n",
"aaab, aaaa, a, aaaab, b, ab, aab\n",
"\n",
"## c.\n",
"baaaaa, baaab, baa, baaaa, baab, ba\n",
"\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",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"aabb: False\n",
"aabbbb: False\n",
"aabbbbb: True\n",
"aaabbbbbb: False\n",
"aaaabbbbbbbbb: True\n"
]
},
{
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$\\displaystyle \\left(aa\\right)^*\\left(bbbb\\right)^*b$"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a regex where 'a' is a multiple of 2 and 'b' is a multiple of 4 + 1\n",
"q31c = '(aa)*(bbbb)*b'\n",
"inputs = [\n",
"\t'aabb',\n",
"\t'aabbbb',\n",
"\t'aabbbbb',\n",
"\t'aaabbbbbb',\n",
"\t'aaaabbbbbbbbb',\n",
"]\n",
"if debug:\n",
" regex_test(q31c, inputs)\n",
"Math(latexify(q31c))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-22T22:22:26.515494Z",
"start_time": "2024-03-22T22:22:26.506412Z"
}
},
"id": "c0d81b8fb61431de",
"execution_count": 3
},
{
"cell_type": "markdown",
"source": [
"## d."
],
"metadata": {
"collapsed": false
},
"id": "53e09c458eafb680"
},
{
"cell_type": "code",
"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": {
"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-22T22:22:26.522964Z",
"start_time": "2024-03-22T22:22:26.517188Z"
}
},
"id": "75f2af38daefc05b",
"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": [
{
"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": {
"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-22T22:22:26.531449Z",
"start_time": "2024-03-22T22:22:26.524511Z"
}
},
"id": "404b569fb1f1b3dd",
"execution_count": 5
},
{
"cell_type": "markdown",
"source": [
"## e."
],
"metadata": {
"collapsed": false
},
"id": "64279b55169daadf"
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0100001: True\n",
"0100000100001: True\n",
"1000001: True\n",
"00000010000100000: True\n",
"1000100001: False\n"
]
},
{
"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-22T22:22:26.538114Z",
"start_time": "2024-03-22T22:22:26.532887Z"
}
},
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"00100: True\n",
"00100: True\n",
"00100: False\n"
]
},
{
"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",
"# Was not supposed to be accepted by all, only bt 2 and 3\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-22T22:22:26.547221Z",
"start_time": "2024-03-22T22:22:26.539392Z"
}
},
"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": [
{
"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": {
"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",
" 'pp', # Not supposed to be here\n",
"]\n",
"if debug:\n",
" regex_test(q33, inputs)\n",
"Math(latexify(q33))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-22T22:22:26.555155Z",
"start_time": "2024-03-22T22:22:26.549229Z"
}
},
"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": [
{
"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": {
"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-22T22:22:26.563180Z",
"start_time": "2024-03-22T22:22:26.556508Z"
}
},
"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": {
"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
}

121
Assignment 6.ipynb Normal file
View File

@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a90ef9f8022df10e",
"metadata": {
"collapsed": false
},
"source": [
"# Question 36\n",
"## a.\n",
"For language B which is non-regular, for example:\n",
"$$B = \\{ w \\in \\{0, 1\\}^* | w \\text{ is a palindrome} \\}$$\n",
"\n",
"A is a subset of Language B:\n",
"$$A = \\{ab, aabb, aaabbb\\}$$\n",
"\n",
"A is however regular, as it is a finite language, so not all subsets of non-regular languages must also be non-regular\n",
"\n",
"## b.\n",
"For language B which is regular:\n",
"$$B = \\{ 0^n | n \\in \\mathbb{Z}\\}$$\n",
"\n",
"Then the following language is a subset of B, but is not regular:\n",
"$$A = \\{0^n | n \\text{ is prime}\\}$$\n",
"\n",
"As all primes are a subset of set $\\mathbb{Z}$"
]
},
{
"cell_type": "markdown",
"id": "2d1690ef",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "9a7c595f",
"metadata": {},
"source": [
"# Question 37\n",
"Let $p \\geq 1$ be any integer \\\n",
"Choose any string $s = a^{p}b^{p}c^{2p}$. $|s|$ is $4p$ which must be greater than $p$ \\\n",
"Consider any decomposition $s = xyz$ that satisfies 1. $|y| \\geq 1$ and 2. $|xy| \\leq p$\n",
"\n",
"Because of 2, x and y must be only \"a\", and because of 1, y must have at least one \"a\"\n",
"\n",
"So for the decomposition \"xyyz\", the number of \"a\" is p+1, and the number of \"b\" is p, which violates the condition that the number of \"c\" is 2p, as the the number of \"a\" plus the number of \"b\" is 2p+1"
]
},
{
"cell_type": "markdown",
"id": "c8d0ce5a",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "8d439d76",
"metadata": {},
"source": [
"# Question 38\n",
"Let $p \\geq 1$ be any integer \\\n",
"Choose any string $s = a^{p}b^{3p}$. $|s|$ is $4p$ which must be greater than $p$ \\\n",
"Consider any decomposition $s = xyz$ that satisfies 1. $|y| \\geq 1$ and 2. $|xy| \\leq p$\n",
"\n",
"Because of 2, x and y must be only \"a\", and because of 1, y must have at least one \"a\"\n",
"\n",
"So for the decomposition \"xyyz\", the number of \"a\" is p+1, and the number of \"b\" is 3p, which contradicts the premise that $n_a(w) = 3n_b(w)$ as for any integers which satisfy this condtion, the $n_a(w)$ will always be 1 more than required"
]
},
{
"cell_type": "markdown",
"id": "323bea35",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "56466c7f",
"metadata": {},
"source": [
"# Question 41\n",
"Let $p \\geq 1$ be any integer \\\n",
"Choose any string $s = a^{p}b^{p}$. $|s|$ is $2p$ which must be greater than $p$ \\\n",
"Consider any decomposition $s = xyz$ that satisfies 1. $|y| \\geq 1$ and 2. $|xy| \\leq p$\n",
"\n",
"Because of 2, x and y must be only \"a\", and because of 1, y must have at least one \"a\"\n",
"\n",
"So for the decomposition \"xz\", the number of \"a\" is p-1, which contradicts the condition that $|x| \\geq p$"
]
}
],
"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
}

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
}

61
Assignment 8.ipynb Normal file
View File

@ -0,0 +1,61 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a90ef9f8022df10e",
"metadata": {
"collapsed": false
},
"source": [
"# Question 48\n",
"Given that $A_{DFA}$ is decidable, and it will finish in a finite number of steps, we know that any DFA that accepts at least one string of finite length 3, we know that it is decidable. The algorithm is as follows:\n",
"\n",
"1. Start in M's start state\n",
"2. Go through the symbols of w one at a time\n",
"3. For all symbols of length 3, and the DFA is an accept state, we accept M DFA, otherwise reject M"
]
},
{
"cell_type": "markdown",
"id": "2d1690ef",
"metadata": {},
"source": [
"$\\pagebreak$"
]
},
{
"cell_type": "markdown",
"id": "9a7c595f",
"metadata": {},
"source": [
"# Question 49\n",
"Let us assume that $EQ_{TM}$ is decidable, then we can:\n",
"\n",
"1. Pass two copies of $ALL_{TM}$ into $EQ_{TM}$\n",
"2. If $EQ_{TM}$ accepts the two copies of $ALL_{TM}$, this means that $ALL_{TM}$ is decidable, as $EQ_{TM}$ must decide on $ALL_{TM}$\n",
"3. Therefor it is impossible for $EQ_{TM}$ to be decidable given that $ALL_{TM}$ is undecidable"
]
}
],
"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
}

BIN
Drawing/A3.xcf Normal file

Binary file not shown.

BIN
Drawing/A4.xcf Normal file

Binary file not shown.

118
Homework/2024-1-15.py Normal file
View File

@ -0,0 +1,118 @@
from util import *
def p1():
alphabet = ['a', 'b']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)
def number_of_a_is_even(s):
return s.count('a') % 2 == 0
strings = list(filter(number_of_a_is_even, strings))
return strings
def p2():
possible_m = [0, 1, 2, 3, 4]
possible_n = [0, 1, 2, 3, 4]
possible_p = [0, 1, 2, 3, 4]
strings = []
for m in possible_m:
for n in possible_n:
for p in possible_p:
# First rule
if m + n + p <= 4:
# Second rule
if m > n:
# Third rule
if n == p:
strings.append('a' * m + 'b' * n + 'c' * p)
return strings
def p3():
alphabet = ['a', 'b', 'c']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 4)
def number_of_a_is_greater_than_number_of_b(s):
return s.count('a') > s.count('b')
def number_of_b_is_equal_to_number_of_c(s):
return s.count('b') == s.count('c')
strings = list(filter(number_of_a_is_greater_than_number_of_b, strings))
strings = list(filter(number_of_b_is_equal_to_number_of_c, strings))
return strings
def valid():
alphabet = ['a', 'b']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)
def number_of_a_is_equal_to_3(s):
return s.count('a') == 3
def number_of_b_is_equal_to_3(s):
return s.count('b') == 3
# A game stops when a player has 3 a's or 3 b's, so there should be no trailing wins from the losing player
def valid_game(s):
if number_of_a_is_equal_to_3(s):
return not s.endswith('b')
elif number_of_b_is_equal_to_3(s):
return not s.endswith('a')
strings = list(filter(valid_game, strings))
return strings
def p5():
alphabet = ['a', 'b']
valid_strings = valid()
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)
def number_of_a_or_b_is_equal_to_3(s):
return s.count('a') == 3 or s.count('b') == 3
strings = list(filter(number_of_a_or_b_is_equal_to_3, strings))
strings = [i for i in strings if i not in valid_strings] # strings - valid_strings
return strings
def p6():
alphabet = ['a', 'b']
valid_strings = valid()
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)
def number_of_a_is_equal_to_3(s):
return s.count('a') == 3
def number_of_b_is_equal_to_3(s):
return s.count('b') == 3
def opposite_number_of_winner_is_less_than_3(s):
if number_of_a_is_equal_to_3(s):
return s.count('b') < 3
elif number_of_b_is_equal_to_3(s):
return s.count('a') < 3
strings = list(filter(opposite_number_of_winner_is_less_than_3, strings))
strings = [i for i in strings if i not in valid_strings] # strings - valid_strings
return strings
def main():
print_strings(p1())
print_strings(p2())
print_strings(p3())
print_strings(valid())
print_strings(p5())
print_strings(p6())
if __name__ == '__main__':
main()

View File

@ -9,5 +9,15 @@ def get_all_strings_with_a_given_alphabet_and_length(alphabet, length):
return strings return strings
def replace_empty_string_with_symbol(strings):
symbol = 'ε'
for i in range(len(strings)):
if strings[i] == '':
strings[i] = symbol
return strings
def print_strings(strings): def print_strings(strings):
print(strings, len(strings)) strings = replace_empty_string_with_symbol(strings)
# print(len(strings))
print(strings)

3
freeze.sh Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
source ~/.virtualenvs/CS2333/bin/activate
pip freeze > requirements.txt

104
requirements.txt Normal file
View File

@ -0,0 +1,104 @@
anyio==4.2.0
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asttokens==2.4.1
async-lru==2.0.4
attrs==23.2.0
automata-lib==8.2.0
Babel==2.14.0
beautifulsoup4==4.12.3
bleach==6.1.0
cached_method==0.1.0
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.3.2
coloraide==2.16
comm==0.2.1
debugpy==1.8.0
decorator==5.1.1
defusedxml==0.7.1
exceptiongroup==1.2.0
executing==2.0.1
fastjsonschema==2.19.1
fqdn==1.5.1
frozendict==2.4.0
idna==3.6
importlib-metadata==7.0.1
ipykernel==6.29.0
ipython==8.18.1
ipywidgets==8.1.1
isoduration==20.11.0
jedi==0.19.1
Jinja2==3.1.3
json5==0.9.14
jsonpointer==2.4
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
jupyter==1.0.0
jupyter-console==6.6.3
jupyter-events==0.9.0
jupyter-lsp==2.2.2
jupyter_client==8.6.0
jupyter_core==5.7.1
jupyter_server==2.12.5
jupyter_server_terminals==0.5.2
jupyterlab==4.0.11
jupyterlab-widgets==3.0.9
jupyterlab_pygments==0.3.0
jupyterlab_server==2.25.2
MarkupSafe==2.1.4
matplotlib-inline==0.1.6
mistune==3.0.2
nbclient==0.9.0
nbconvert==7.14.2
nbformat==5.9.2
nest-asyncio==1.6.0
networkx==3.2.1
notebook==7.0.7
notebook_shim==0.2.3
overrides==7.6.0
packaging==23.2
pandocfilters==1.5.1
parso==0.8.3
pexpect==4.9.0
platformdirs==4.1.0
prometheus-client==0.19.0
prompt-toolkit==3.0.43
psutil==5.9.8
ptyprocess==0.7.0
pure-eval==0.2.2
pycparser==2.21
Pygments==2.17.2
pygraphviz==1.11
python-dateutil==2.8.2
python-json-logger==2.0.7
PyYAML==6.0.1
pyzmq==25.1.2
qtconsole==5.5.1
QtPy==2.4.1
referencing==0.32.1
requests==2.31.0
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rpds-py==0.17.1
Send2Trash==1.8.2
six==1.16.0
sniffio==1.3.0
soupsieve==2.5
stack-data==0.6.3
terminado==0.18.0
tinycss2==1.2.1
tomli==2.0.1
tornado==6.4
traitlets==5.14.1
types-python-dateutil==2.8.19.20240106
typing_extensions==4.9.0
uri-template==1.3.0
urllib3==2.1.0
wcwidth==0.2.13
webcolors==1.13
webencodings==0.5.1
websocket-client==1.7.0
widgetsnbextension==4.0.9
zipp==3.17.0