From 4dac403cdc97f8976d53fc1d4e1c5dd6304a7afe Mon Sep 17 00:00:00 2001 From: Sebastian Raschka Date: Tue, 29 Apr 2014 09:53:17 -0400 Subject: [PATCH] self assessment --- tutorials/scope_resolution_legb_rule.ipynb | 99 +++++++++++++++++++++- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/tutorials/scope_resolution_legb_rule.ipynb b/tutorials/scope_resolution_legb_rule.ipynb index c97a1e4..d1370e4 100644 --- a/tutorials/scope_resolution_legb_rule.ipynb +++ b/tutorials/scope_resolution_legb_rule.ipynb @@ -62,6 +62,7 @@ "- [1. LG - Local vs. Global](#section_1) \n", "- [2. LEG - Local, Enclosed, and Global scope](#section_2) \n", "- [3. LEGB - Local, Enclosed, Global, Built-in](#section_3) \n", + "- [Self-assessment exercise](#assessment)\n", "- [Conclusion](#conclusion) \n", "- [Solutions](#solutions)" ] @@ -498,8 +499,6 @@ "input": [ "a_var = 'global variable'\n", "\n", - "\n", - "\n", "def len(in_var):\n", " print('called my len() function')\n", " l = 0\n", @@ -511,8 +510,7 @@ " len_in_var = len(in_var)\n", " print('Input variable is of length', len_in_var)\n", "\n", - "#a_func('Hello, World!')\n", - " " + "#a_func('Hello, World!')" ], "language": "python", "metadata": {}, @@ -550,6 +548,65 @@ "Since the exact same names can be used to map names to different objects - as long as the names are in different name spaces - there is no problem of reusing the name `len` to define our own length function (this is just for demonstration pruposes, it is NOT recommended). As we go up in Python's L -> E -> G -> B hierarchy, the function `a_func()` finds `len()` already in the global scope first before it attempts" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "
\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Self-assessment exercise" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, after we went through a couple of exercises, let us quickly check where we are. So, one more time: What would the following code print out?" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 'global'\n", + "\n", + "def outer():\n", + " \n", + " def len(in_var):\n", + " print('called my len() function: ', end=\"\")\n", + " l = 0\n", + " for i in in_var:\n", + " l += 1\n", + " return l\n", + " \n", + " a = 'local'\n", + " \n", + " def inner():\n", + " global len\n", + " nonlocal a\n", + " a += ' variable'\n", + " inner()\n", + " print('a is', a)\n", + " print(len(a))\n", + "\n", + "\n", + "outer()\n", + "\n", + "print(len(a))\n", + "print('a is', a)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 59 + }, { "cell_type": "markdown", "metadata": {}, @@ -559,6 +616,13 @@ "
" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[[go to solution](#solutions)]" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -675,6 +739,33 @@ "metadata": {}, "outputs": [], "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Execute to run the self-assessment solution\n", + "\n", + "sol = \"000010100110111101110101011101000110010101110010001010\"\\\n", + "\"0000101001001110100000101000001010011000010010000001101001011100110\"\\\n", + "\"0100000011011000110111101100011011000010110110000100000011101100110\"\\\n", + "\"0001011100100110100101100001011000100110110001100101000010100110001\"\\\n", + "\"1011000010110110001101100011001010110010000100000011011010111100100\"\\\n", + "\"1000000110110001100101011011100010100000101001001000000110011001110\"\\\n", + "\"1010110111001100011011101000110100101101111011011100011101000100000\"\\\n", + "\"0011000100110100000010100000101001100111011011000110111101100010011\"\\\n", + "\"0000101101100001110100000101000001010001101100000101001100001001000\"\\\n", + "\"0001101001011100110010000001100111011011000110111101100010011000010\"\\\n", + "\"1101100\"\n", + "\n", + "sol_str =''.join(chr(int(sol[i:i+8], 2)) for i in range(0, len(sol), 8))\n", + "for line in sol_str.split('\\n'):\n", + " print(line)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 58 } ], "metadata": {}