Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERCEPTRONS EREL SAUL ASSIGNMENT #38

Open
wants to merge 8 commits into
base: gh-pages
Choose a base branch
from
59 changes: 46 additions & 13 deletions projects/2-state-machines/Python and State Machines.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -142,11 +142,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial state: s1\n",
"Accepting? False\n",
"After a: s2\n",
"After ab: s3\n",
"Next steps: {'a': 's3'}\n",
"Accepting? True\n"
]
}
],
"source": [
"ab_a_ = StateMachine(\n",
" #We can define the states and edges in one go\n",
Expand Down Expand Up @@ -182,7 +195,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {
"collapsed": true
},
Expand All @@ -201,7 +214,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -229,14 +242,17 @@
"assert not check(test1(), \"hel\")\n",
"assert not check(test1(), \"helloy\")\n",
"assert not check(test1(), \"hih\")\n",
"assert not check(test1(), \"heyhey\")"
"assert not check(test1(), \"heyhey\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"collapsed": true
},
"source": [
"Next, define a state machine which accepts email addresses consisting only of the letter \"x\" and the symbols \"@\" and \".\" (in other words, you don't need a ton of edges). Devise tests for it below (try to be sinister---get a partner to come up with adversarial examples too)."
"# Next, define a state machine which accepts email addresses consisting only of the letter \"x\" and the symbols \"@\" and \".\" (in other words, you don't need a ton of edges). Devise tests for it below (try to be sinister---get a partner to come up with adversarial examples too)."
]
},
{
Expand Down Expand Up @@ -292,11 +308,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ab\n",
"abc\n"
]
}
],
"source": [
"ab = \"a\" + \"b\"\n",
"print(ab)\n",
Expand All @@ -313,11 +338,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c']\n"
]
}
],
"source": [
"lst_abc = [\"a\",\"b\"] + [\"c\"]\n",
"print(lst_abc)"
Expand Down
93 changes: 93 additions & 0 deletions projects/2-state-machines/StateMachine_EREL_SAUL.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#ASSIGNMENT 1.1\n",
"def check(sm, string):\n",
" for s in string:\n",
"#for loop is used to go through each letter of the string \n",
" if s not in sm.out_edges():\n",
"#if statement here is used to check if the letter corresponds to the next edge. \n",
"#if it does NOT correspond to any out edges then it returns FALSE\n",
" return False\n",
"#this basically means that it doesn't pass the conditions of the state machine\n",
" sm.advance(s)\n",
"#if the selected letter does correspond to the any of the out edges we reassign the \n",
"#machine's state to be the target state denoted by that symbol, among the current out edges.\n",
" return sm.is_terminal()\n",
"#if the string gets to this point than it means that all letter of the string has \n",
"#successfully passed the requirments and the state machine is in a terminal state \n",
"#since its current state is terminal\n",
"\n",
"\n",
"\n",
"#ASSIGNMENT 1.2\n",
"def test2():\n",
" #(x+@x+\\.x+(\\.x+)*)\n",
" return StateMachine([(\"s1\", \"x\", \"s2\"), (\"s2\", \"x\", \"s2\"), (\"s2\", \"@\", \"s3\"),\n",
" (\"s3\", \"x\", \"s4\"), (\"s4\", \"x\", \"s4\"), (\"s4\", \".\", \"s5\"),\n",
" (\"s5\", \"x\", \"s6\"), (\"s6\", \"x\", \"s6\"), (\"s6\", \".\", \"s7\"),\n",
" (\"s7\", \"x\", \"s6\")],\n",
" \"s1\",\n",
" [\"s6\"])\n",
"#this function pretty much works the same with the previous one, however this time instead having\n",
"#the out edges, we have created ourselves. We will also use the previous check function to basically\n",
"#check if the input string matches with the conditions we set. First, we check if the first letter is x.\n",
"#This is our number one condition. If it is, we continue to the second out edge. There are 2 options for the\n",
"#second edge, meaning the second letter can either be, once again, x or it can be the @ sign. Notice how \n",
"#the second edge with the x option has both s2's, (\"s2\", \"x\", \"s2\"). That's because there could be\n",
"#as many x's as the mail provider allows. So, if it's x again, then it will stay at the same edge until @ sign\n",
"#or any other sign/letter is used. If @ sign is used then it will go through edge 3 which expects x. After that, \n",
"#it can either take as many x's it can take or it can take a dot, which is edge 5. The same process goes with edges\n",
"#6 and 7. When the loop ends if the conditions are met the state machine terminates, if not it returns false. \n",
"\n",
"#ASSIGNMENT 2\n",
"def sample2(sm, length, sofar):\n",
" # Each path of length 0 is either accepting or non-accepting;\n",
" # in the former case we return [sofar] (the path) and in the latter [] (no path)\n",
" if length == 0:\n",
" if sm.is_terminal():\n",
" return [sofar]\n",
" else:\n",
" return []\n",
" state = sm.state\n",
" results = []\n",
" for sym in sm.out_edges():\n",
" # for each edge: advance, sample, backtrack.\n",
" sm.advance(sym)\n",
" results = results + sample2(sm, length-1, sofar + sym)\n",
" sm.state = state\n",
" return results\n",
"\n"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading