Skip to content

Commit 4dac403

Browse files
committed
self assessment
1 parent ccb3947 commit 4dac403

1 file changed

Lines changed: 95 additions & 4 deletions

File tree

tutorials/scope_resolution_legb_rule.ipynb

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"- [1. LG - Local vs. Global](#section_1) \n",
6363
"- [2. LEG - Local, Enclosed, and Global scope](#section_2) \n",
6464
"- [3. LEGB - Local, Enclosed, Global, Built-in](#section_3) \n",
65+
"- [Self-assessment exercise](#assessment)\n",
6566
"- [Conclusion](#conclusion) \n",
6667
"- [Solutions](#solutions)"
6768
]
@@ -498,8 +499,6 @@
498499
"input": [
499500
"a_var = 'global variable'\n",
500501
"\n",
501-
"\n",
502-
"\n",
503502
"def len(in_var):\n",
504503
" print('called my len() function')\n",
505504
" l = 0\n",
@@ -511,8 +510,7 @@
511510
" len_in_var = len(in_var)\n",
512511
" print('Input variable is of length', len_in_var)\n",
513512
"\n",
514-
"#a_func('Hello, World!')\n",
515-
" "
513+
"#a_func('Hello, World!')"
516514
],
517515
"language": "python",
518516
"metadata": {},
@@ -550,6 +548,65 @@
550548
"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"
551549
]
552550
},
551+
{
552+
"cell_type": "markdown",
553+
"metadata": {},
554+
"source": [
555+
"<a name =\"assessment\"></a>\n",
556+
"<br>\n",
557+
"<br>"
558+
]
559+
},
560+
{
561+
"cell_type": "markdown",
562+
"metadata": {},
563+
"source": [
564+
"# Self-assessment exercise"
565+
]
566+
},
567+
{
568+
"cell_type": "markdown",
569+
"metadata": {},
570+
"source": [
571+
"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?"
572+
]
573+
},
574+
{
575+
"cell_type": "code",
576+
"collapsed": false,
577+
"input": [
578+
"a = 'global'\n",
579+
"\n",
580+
"def outer():\n",
581+
" \n",
582+
" def len(in_var):\n",
583+
" print('called my len() function: ', end=\"\")\n",
584+
" l = 0\n",
585+
" for i in in_var:\n",
586+
" l += 1\n",
587+
" return l\n",
588+
" \n",
589+
" a = 'local'\n",
590+
" \n",
591+
" def inner():\n",
592+
" global len\n",
593+
" nonlocal a\n",
594+
" a += ' variable'\n",
595+
" inner()\n",
596+
" print('a is', a)\n",
597+
" print(len(a))\n",
598+
"\n",
599+
"\n",
600+
"outer()\n",
601+
"\n",
602+
"print(len(a))\n",
603+
"print('a is', a)"
604+
],
605+
"language": "python",
606+
"metadata": {},
607+
"outputs": [],
608+
"prompt_number": 59
609+
},
553610
{
554611
"cell_type": "markdown",
555612
"metadata": {},
@@ -559,6 +616,13 @@
559616
"<br>"
560617
]
561618
},
619+
{
620+
"cell_type": "markdown",
621+
"metadata": {},
622+
"source": [
623+
"[[go to solution](#solutions)]"
624+
]
625+
},
562626
{
563627
"cell_type": "markdown",
564628
"metadata": {},
@@ -675,6 +739,33 @@
675739
"metadata": {},
676740
"outputs": [],
677741
"prompt_number": 9
742+
},
743+
{
744+
"cell_type": "code",
745+
"collapsed": false,
746+
"input": [
747+
"# Execute to run the self-assessment solution\n",
748+
"\n",
749+
"sol = \"000010100110111101110101011101000110010101110010001010\"\\\n",
750+
"\"0000101001001110100000101000001010011000010010000001101001011100110\"\\\n",
751+
"\"0100000011011000110111101100011011000010110110000100000011101100110\"\\\n",
752+
"\"0001011100100110100101100001011000100110110001100101000010100110001\"\\\n",
753+
"\"1011000010110110001101100011001010110010000100000011011010111100100\"\\\n",
754+
"\"1000000110110001100101011011100010100000101001001000000110011001110\"\\\n",
755+
"\"1010110111001100011011101000110100101101111011011100011101000100000\"\\\n",
756+
"\"0011000100110100000010100000101001100111011011000110111101100010011\"\\\n",
757+
"\"0000101101100001110100000101000001010001101100000101001100001001000\"\\\n",
758+
"\"0001101001011100110010000001100111011011000110111101100010011000010\"\\\n",
759+
"\"1101100\"\n",
760+
"\n",
761+
"sol_str =''.join(chr(int(sol[i:i+8], 2)) for i in range(0, len(sol), 8))\n",
762+
"for line in sol_str.split('\\n'):\n",
763+
" print(line)"
764+
],
765+
"language": "python",
766+
"metadata": {},
767+
"outputs": [],
768+
"prompt_number": 58
678769
}
679770
],
680771
"metadata": {}

0 commit comments

Comments
 (0)