Skip to content

Instantly share code, notes, and snippets.

@emanuelfeld
Created July 8, 2016 03:33
Show Gist options
  • Save emanuelfeld/f2a55497243bd856f4b4e0c92ca34adc to your computer and use it in GitHub Desktop.
Save emanuelfeld/f2a55497243bd856f4b4e0c92ca34adc to your computer and use it in GitHub Desktop.
MIT 6.01 State Machines
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class SM:\n",
" def start(self):\n",
" self.state = self.startState\n",
" def step(self, inp):\n",
" (s, o) = self.getNextValues(self.state, inp)\n",
" self.state = s\n",
" return o\n",
" def transduce(self, inputs):\n",
" self.start()\n",
" return [self.step(inp) for inp in inputs]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['pay', 'enter', 'enter', 'pay', 'pay', 'enter', 'enter']\n"
]
}
],
"source": [
"class Turnstile(SM):\n",
" startState = 'locked'\n",
" def getNextValues(self, state, inp):\n",
" if inp == 'coin':\n",
" return ('unlocked', 'enter')\n",
" elif inp == 'turn':\n",
" return ('locked', 'pay')\n",
" elif state == 'locked':\n",
" return ('locked', 'pay')\n",
" else:\n",
" return ('unlocked', 'enter')\n",
"\n",
"testInput = [None, 'coin', None, 'turn', 'turn', 'coin', 'coin']\n",
"ts = Turnstile()\n",
"print(ts.transduce(testInput))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test1: [100, 10, 1, 0, 2, 0, 0, 3, 0, 0]\n",
"Test2: [10, 100, 0, 0, 0, 0, 0]\n",
"Test3: [-1, 0, 1, 2, -3, 1]\n",
"Test4: [100, 10, 1, 0, 2, 0, 0, 3, 0, 0]\n"
]
}
],
"source": [
"class Delay2Machine(SM):\n",
" def __init__(self, val0, val1):\n",
" self.startState = (val0, val1)\n",
" def getNextValues(self, state, inp):\n",
" return ((state[1], inp), state[0])\n",
"\n",
"def runTestsDelay():\n",
" print('Test1:', Delay2Machine(100, 10).transduce([1,0,2,0,0,3,0,0,0,4]))\n",
" print('Test2:', Delay2Machine(10, 100).transduce([0,0,0,0,0,0,1]))\n",
" print('Test3:', Delay2Machine(-1, 0).transduce([1,2,-3,1,2,-3]))\n",
" # Test that self.state is not being changed.\n",
" m = Delay2Machine(100, 10)\n",
" m.start()\n",
" [m.getNextValues(m.state, i) for i in [-1,-2,-3,-4,-5,-6]]\n",
" print('Test4:', [m.step(i) for i in [1,0,2,0,0,3,0,0,0,4]])\n",
"\n",
"runTestsDelay()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test1: ['#', ' ', 'f', 'u', 'n', 'c', '#', ' ', 't', 'e', 's', 't', '#', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't']\n",
"Test2: ['#', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', '#', ' ', 'f', 'u', 'n', 'c', '#', ' ', 't', 'e', 's', 't', '#', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't']\n",
"Test3: ['#', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', '#', ' ', 'f', 'u', 'n', 'c', '#', ' ', 't', 'e', 's', 't', '#', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't']\n"
]
}
],
"source": [
"class CommentsSM(SM):\n",
" startState = False\n",
" def getNextValues(self, state, inp):\n",
" if inp == '\\n':\n",
" return (False, None)\n",
" elif inp == '#':\n",
" return (True, inp)\n",
" elif state == True:\n",
" return (state, inp)\n",
" else:\n",
" return (state, None)\n",
" \n",
"x1 = '''def f(x): # func\n",
" if x: # test\n",
" # comment\n",
" return 'foo' '''\n",
"\n",
"x2 = '''#initial comment\n",
"def f(x): # func\n",
" if x: # test\n",
" # comment\n",
" return 'foo' '''\n",
"\n",
"def runTestsComm():\n",
" m = CommentsSM()\n",
" # Return only the outputs that are not None\n",
" print('Test1:', [c for c in CommentsSM().transduce(x1) if not c==None])\n",
" print('Test2:', [c for c in CommentsSM().transduce(x2) if not c==None])\n",
" # Test that self.state is not being changed.\n",
" m = CommentsSM()\n",
" m.start()\n",
" [m.getNextValues(m.state, i) for i in ' #foo #bar']\n",
" print('Test3:', [c for c in [m.step(i) for i in x2] if not c==None])\n",
"\n",
"runTestsComm()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test1: ['h', 'i', None, 'h', 'o']\n",
"Test2: [None, None, 'h', 'i', None, 'h', 'o']\n",
"Test3: [None, None, None, 'h', 'i', None, None, 'h', 'o', None, None, None, None, None, None, None, None, None, 'h', 'a', None, None, None, None, None, None]\n",
"Test 4 ['h', 'i', None, 'h', 'o']\n"
]
}
],
"source": [
"class FirstWordSM(SM):\n",
" startState = 'begin'\n",
" def getNextValues(self, state, inp):\n",
" if inp == ' ' and state == 'begin':\n",
" return (state, None)\n",
" elif inp == '\\n':\n",
" return ('begin', None)\n",
" elif state == 'begin':\n",
" return ('word', inp)\n",
" elif state == 'word':\n",
" if inp == ' ':\n",
" return ('rest', None)\n",
" return (state, inp)\n",
" else:\n",
" return ('rest', None)\n",
" \n",
"# Test 1\n",
"test1 = '''hi\n",
"ho'''\n",
"\n",
"#Test 2\n",
"test2 = ''' hi\n",
"ho'''\n",
"\n",
"#Test 3\n",
"test3 = '''\n",
"\n",
" hi\n",
" ho ho ho\n",
"\n",
" ha ha ha'''\n",
"\n",
"def runTestsFW():\n",
" m = FirstWordSM()\n",
" print('Test1:', m.transduce(test1))\n",
" print('Test2:', m.transduce(test2))\n",
" print('Test3:', m.transduce(test3))\n",
" m = FirstWordSM()\n",
" m.start()\n",
" [m.getNextValues(m.state, i) for i in '\\nFoo ']\n",
" print('Test 4', [m.step(i) for i in test1])\n",
"\n",
"runTestsFW()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment