{"id":9306,"date":"2016-06-07T23:30:16","date_gmt":"2016-06-08T03:30:16","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9306"},"modified":"2017-06-28T19:15:53","modified_gmt":"2017-06-28T23:15:53","slug":"zh-16","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-16\/","title":{"rendered":"16: \u9012\u63a8"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: Graeme Kemkes, gdkemkes@alumni.uwaterloo.ca, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p>\u6211\u4eec\u4e4b\u524d\u5df2\u7ecf\u89c1\u5230\u4e86\u65b9\u7a0b\u53ef\u4ee5\u7528\u6765\u7ec4\u7ec7\uff0c\u91cd\u590d\u4f7f\u7528\u90e8\u5206\u4ee3\u7801\u3002\u6211\u4eec\u4e5f\u5df2\u7ecf\u770b\u5230\u4e86\u65b9\u7a0b\u53ef\u4ee5\u6839\u636e\u5176\u4ed6\u65b9\u7a0b\u88ab\u5b9a\u4e49\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b66\u4e60\u4e00\u4e2a\u65b9\u7a0b\u662f\u5982\u4f55\u6839\u636e\u81ea\u8eab\u88ab\u5b9a\u4e49\u7684\uff01\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\u53eb\u505a<em>\u9012\u63a8<\/em>\u3002\u6709\u4e00\u53e5\u4fd7\u8bed\uff1a\"\u4e3a\u4e86\u7406\u89e3\u9012\u63a8\uff0c\u4f60\u9996\u5148\u8981\u7406\u89e3\u9012\u63a8\u3002\"<\/p>\n<h1>\u8303\u4f8b<\/h1>\n<p>\u5728\u5173\u4e8e\u5faa\u73af\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8fc7<code>while<\/code>\u5faa\u73af\u6765\u521b\u9020\u4e0b\u5217\u8f93\u51fa\u3002<\/p>\n<pre>5\r\n4\r\n3\r\n2\r\n1\r\nBlastoff!<\/pre>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9012\u63a8\u7684\u7a0b\u5e8f\uff0c\u80fd\u591f\u53d6\u5f97\u76f8\u540c\u7684\u6548\u679c\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox0'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u4f7f\u7528\u9012\u63a8\u5012\u6570<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 214px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=8 readonly='readonly'  style = 'height : 214px;'  class='pyboxCode RO'>\ndef countdown(n):\n  if n == 0:\n    print('Blastoff!')\n  else:\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"fb83afad73e49bd03eb146e614438f7c\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit0\").value = \"Run program\";document.getElementById(\"inputInUse0\").value = \"N\";<\/script>\n<\/p>\n<p>\u8ba9\u6211\u4eec\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u6253\u5370\u8bed\u53e5\u6765\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7248\u672c\u4e5f\u4ece\u8f93\u5165\u503c\u8bfb\u53d6\u65f6\u95f4\u671f\u9650\u3002<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u4f7f\u7528\u9012\u63a8\u5012\u6570<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 292px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=11 readonly='readonly'  style = 'height : 292px;'  class='pyboxCode RO'>\ndef countdown(n):\n  print('Entering countdown(',n,')')\n  if n == 0:\n    print('Blastoff!')\n  else:\n    print(n)\n    countdown(n - 1)\n  print('Exiting from countdown(',n,')')\n\nlimit = int(input())\ncountdown(limit)<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput1\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch1\" value=\"Input Switch\" onclick=\"pbInputSwitch(1,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(1)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(1,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"05187437d86d37a181341f0ec0c7c999\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(1,\"N\");<\/script>\n<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528<strong>\u8f93\u5165<\/strong>\uff0c\u5c1d\u8bd5\u5176\u4ed6\u8f93\u5165\u503c\u3002\u5148\u5c1d\u8bd5<code>0<\/code>\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff0c\u7136\u540e\u5c1d\u8bd5<code>1<\/code>\u3002<\/p>\n<p>\u5f53\u8f93\u5165\u662f<code>5<\/code>\uff0c\u7a0b\u5e8f\u9996\u5148\u8fd0\u884c<code>countdown<\/code>\u65b9\u7a0b\u3002\u5176\u4e2d<code>n=5<\/code>\uff0c\u6253\u5370\u51fa<code>5<\/code>\u5e76\u8fd0\u884c<code>countdown(4)<\/code>\u3002\u6b64\u64cd\u4f5c\u91cd\u590d\u8fdb\u884c\uff0c\u76f4\u5230<code>countdown(0)<\/code>\u3002\u6700\u540e\u6253\u5370\u51fa<code>\"Blastoff!\"<\/code>\uff0c\u505c\u6b62\u8fd0\u884c<code>countdown<\/code>\u3002\u5f53Python\u4ee5<code>n=0<\/code>\u7ed3\u675f<code>countdown<\/code>\u65b9\u7a0b\u65f6\uff0cPython\u56de\u5230\u8fd0\u884c\u5b83\u7684\u65b9\u7a0b\uff0c\u4e5f\u5c31\u662f<code>n=1<\/code>\u65f6\u7684\u65b9\u7a0b<code>countdown<\/code>\u3002\u7136\u540e\u6211\u4eec\u56de\u5230<code>n=2<\/code>\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u9012\u63a8\u4ee3\u7801\u53ef\u89c6\u5316\uff1a<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=def+countdown%28n%29%3A%0A++if+n+%3D%3D+0%3A%0A++++print%28%27Blastoff%21%27%29%0A++else%3A%0A++++print%28n%29%0A++++countdown%28n+-+1%29%0A%0Acountdown%283%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p>\u8fd9\u4e2a\u6539\u53d8\u4f7f\u5f97\u9012\u63a8\u548c\u6211\u4eec\u6240\u89c1\u8fc7\u7684\u65b9\u7a0b\u4e0d\u540c\u3002\u5728\u8fd9\u91cc\uff0c\u65b9\u7a0b\u7684\u51e0\u4e2a\u4e0d\u540c\u7248\u672c\u5728\u540c\u4e00\u65f6\u95f4\u8fd0\u884c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6bcf\u6b21\u6709\u8d85\u8fc7\u4e00\u4e2a\u7684\u7801\u6846\u4e0e\u8fd9\u4e2a\u540c\u6837\u7684\u65b9\u7a0b\u76f8\u5bf9\u5e94\u3002\u8fd9\u4e0e<a href=\"http:\/\/cscircles.cemc.uwaterloo.ca\/10-def\/#funcfunc\">what we saw in the visualization where one function called another<\/a>\u5f88\u76f8\u4f3c\uff0c\u552f\u4e00\u7684\u4e0d\u540c\u5c31\u662f\u5728\u8fd9\u91cc\u88ab\u8fd0\u884c\u7684\u65b9\u7a0b\u548c\u8c03\u7528\u65b9\u7a0b\u662f\u540c\u4e00\u4e2a\u65b9\u7a0b\u3002\u6ce8\u610f\uff0c\u6bcf\u4e00\u6b65\u53ea\u6709\"\u5f53\u524d\"\u53d8\u91cf\uff08\u6700\u65b0\u7684\/bottom-most frame\uff09\u88ab\u4f7f\u7528\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u975e\u5e95\u7aef\u7684\u90e8\u5206\"\u6682\u505c\"\uff0c\u5b83\u4eec\u7684\u503c\u65e0\u6cd5\u5f97\u5230\u3002<\/p>\n<p>\u73b0\u5728\u8f6e\u5230\u4f60\u6765\u5199\u4e00\u4e9b\u4ee3\u7801\u4e86\u3002\u5bf9<code>countdown<\/code>\u65b9\u7a0b\u8fdb\u884c\u6539\u52a8\uff0c\u4f7f\u5176\u53d8\u4e3a\u4ece\u5c0f\u6570\u5230\u5927\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox2'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Blast Up<\/span><\/div>\u7f16\u5199\u4e00\u4e2a\u9012\u5f52\u65b9\u7a0b<code>countup(n)<\/code>\uff0c\u6253\u5370\u51fa'Blastoff!'\uff0c\u7136\u540e\u5206\u522b\u5728\u6bcf\u4e00\u884c\u51fa\u73b0\u6570\u5b57<code>1<\/code>\u5230<code>n<\/code>\u3002<a class=\"hintlink\"  id=\"hintlink3\">\u63d0\u793a<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=20   class='pyboxCode RW'>\ndef countdown(n):\n if n == 0:\n print('Blastoff!')\n else:\n print(n)\n countdown(n - 1)\n<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode2' value='def countdown(n):\\n if n == 0:\\n print(\\u0027Blastoff!\\u0027)\\n else:\\n print(n)\\n countdown(n - 1)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(2,'16.countup')\" ><\/td>\n<\/tr><\/table><select id='pbSelect2' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(2,descape($('#defaultCode2').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(2);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"889922e771c65e9710d7d34c0ac24b8a\"\/>\n<div id='pbresults2' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(2);});document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<p>\u8ba9\u6211\u4eec\u518d\u6539\u53d8\u4e00\u4e0b<code>countdown<\/code>\uff0c\u4f7f\u5f97\u6bcf\u6b21\u589e\u91cf\u662f2\u3002\u90a3\u4e48\u8f93\u51fa\u5e94\u4e3a5, 3, 1, Blastoff! \u6211\u4eec\u8981\u6539\u53d8\u4e00\u4e0b\u65b9\u7a0b\u7684\u5b9e\u53c2\uff0c\u628a<code>n-1<\/code>\u6539\u4e3a<code>n-2<\/code>\u3002\u9664\u6b64\u4ee5\u5916\u8fd8\u6709\u4ec0\u4e48\u9700\u8981\u6539\u7684\u4e48\uff1f<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox4'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u5c1d\u8bd5\u6bcf\u6b21\u589e\u91cf\u662f2\u7684\u5012\u6570<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 214px;'><textarea wrap='off' name='usercode4' id='usercode4'  cols=10 rows=8 readonly='readonly'  style = 'height : 214px;'  class='pyboxCode RO'>\ndef countdownBy2(n):\n  if n == 0:\n    print('Blastoff!')\n  else:\n    print(n)\n    countdownBy2(n - 2)\n\ncountdownBy2(5)<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(4)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(4,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"33f97a92cecd10ba94680c59d48b6f7c\"\/>\n<div id='pbresults4' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit4\").value = \"Run program\";document.getElementById(\"inputInUse4\").value = \"N\";<\/script>\n<\/p>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u5e76\u4e0d\u50cf\u6211\u4eec\u60f3\u8c61\u7684\u90a3\u6837\u8fd0\u884c\u3002\u7a0b\u5e8f\u6253\u5370\u51fa<code>5, 3, 1<\/code>\uff0c\u8fd9\u6ca1\u95ee\u9898\u3002\u4f46\u662f\u8fd8\u4f1a\u7ee7\u7eed\u6253\u5370\u51fa<code>-1, -3, -5<\/code>\uff0c\u6c38\u65e0\u6b62\u5883\u3002\uff08\u66f4\u5177\u4f53\u6765\u8bf4\uff0c\u5b83\u4f1a\u8fd0\u884c\u4e0b\u53bb\uff0c\u76f4\u5230\u6ca1\u6709\u65f6\u95f4\u548c\u50a8\u5b58\u5bb9\u91cf\uff0c\u56e0\u4e3a\u6bcf\u8c03\u7528\u4e00\u6b21\u9012\u63a8\u5c31\u4f1a\u591a\u5360\u7528\u4e00\u4e9b\u8fd0\u884c\u5bb9\u91cf\uff1b\u8be6\u89c1<a href=\"http:\/\/cscircles.cemc.uwaterloo.ca\/visualize\/#code=def%20countdownBy2(n)%3A%0A%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20print('Blastoff!')%0A%20%20else%3A%0A%20%20%20%20print(n)%0A%20%20%20%20countdownBy2(n%20-%202)%0A%0AcountdownBy2(5)\">\u548c\u53ef\u89c6\u5316\u7a0b\u5e8f\u4e2d\u4e00\u6837\u7684\u4f8b\u5b50<\/a>\u3002)<\/p>\n<p>\u5728\u8bbe\u8ba1\u4e00\u4e2a\u9012\u63a8\u65b9\u7a0b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u6ce8\u610f\u8c03\u7528\u987a\u5e8f\uff0c\u907f\u514d\u8bbe\u8ba1\u6210\u6c38\u8fdc\u8fd0\u884c\uff01\u518d\u6b21\u66f4\u6539\u4e0a\u9762\u7684<code>countdownBy2<\/code>\u7a0b\u5e8f\uff0c\u8ba9\u5b83\u6b63\u786e\u5730\u505c\u57281\uff08\u62162\uff0c\u5982\u679c<code>n<\/code>\u662f\u5076\u6570\uff09\u5e76\u6253\u5370\u51fa'Blastoff!'\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox5'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Double Time<\/span><\/div>\u6539\u52a8\u6b64\u9012\u63a8\u7a0b\u5e8f\uff0c\u4f7f\u5176\u6b63\u786e\u5730\u4ee5\u589e\u91cf2\u5012\u6570\u3002<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=20   class='pyboxCode RW'>\ndef countdownBy2(n):\n if n == 0:\n print('Blastoff!')\n else:\n print(n)\n countdownBy2(n - 2)\n<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode5' value='def countdownBy2(n):\\n if n == 0:\\n print(\\u0027Blastoff!\\u0027)\\n else:\\n print(n)\\n countdownBy2(n - 2)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(5)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(5,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(5,'16.countdownby2')\" ><\/td>\n<\/tr><\/table><select id='pbSelect5' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(5,descape($('#defaultCode5').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(5);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"1e9694764bd648c1f97bb018b7c2dfe0\"\/>\n<div id='pbresults5' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(5);});document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<h1>\u8bbe\u8ba1\u9012\u63a8\u65b9\u7a0b<\/h1>\n<p><em>\u9012\u63a8\u65b9\u7a0b<\/em>\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u7a0b\u3002\u4f46\u662f\u4e00\u5b9a\u4f1a\u6709\u8c03\u7528\u4e0d\u4e86\u81ea\u8eab\u7684\u60c5\u51b5\uff0c\u6216\u8005\u8bf4\u7a0b\u5e8f\u4f1a\u6c38\u8fdc\u8fd0\u884c\u4e0b\u53bb\u3002\u6bd4\u5982\u4e0a\u9762\u7684\u90a3\u4e2a\u4f8b\u5b50\u3002<strong>\u57fa\u672c\u6848\u4f8b<\/strong>\u662f\u9012\u63a8\u65b9\u7a0b\u7684\u4e00\u4e2a\u4e0d\u4f1a\u81ea\u8eab\u8c03\u7528\u7684\u90e8\u5206\u3002\u5728\u4e0a\u9762\u7684\u90a3\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u57fa\u672c\u6848\u4f8b\u662f<code>n&lt;=0<\/code>\u3002\u8bbe\u8ba1\u4e00\u4e2a\u9012\u63a8\u65b9\u7a0b\u9700\u8981\u8c28\u614e\u5730\u9009\u62e9\u57fa\u672c\u6848\u4f8b\uff0c\u4ee5\u6b64\u786e\u4fdd\u65b9\u7a0b\u7684\u6bcf\u4e00\u6b65\u6700\u540e\u90fd\u80fd\u591f\u8fbe\u5230\u57fa\u672c\u6848\u4f8b\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u7ec3\u4e60\u4e2d\uff0c\u57fa\u672c\u6848\u4f8b\u5df2\u7ecf\u4e3a\u4f60\u51c6\u5907\u597d\u4e86\uff0c\u4f46\u662f\u5269\u4f59\u7684\u90e8\u5206\u9700\u8981\u4f60\u6765\u7f16\u5199\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox6'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>\u6570\u5b57\u7684\u548c<\/span><\/div>\u4e00\u4e2a\u6570<code>n<\/code>\u7684<em>\u6570\u5b57\u7684\u548c<\/em>\u662f\u7ec4\u6210\u8fd9\u4e2a\u6570\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\u7684\u548c\u3002\u7f16\u5199\u4e00\u4e2a\u9012\u5f52\u65b9\u7a0b<code>digitalSum(n)<\/code>\uff0c\u4f7f\u5f97\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570<code>n<\/code>\uff0c\u8fd4\u8fd8\u5b83\u7684\u6570\u5b57\u7684\u548c\u3002\u6bd4\u5982\uff0c<code>digitalSum(2019)<\/code>\u5e94\u8be5\u8fd4\u8fd812\uff0c\u56e0\u4e3a2+0+1+9=12\u3002<a class=\"hintlink\"  id=\"hintlink7\">\u63d0\u793a #1<\/a>\u00a0<a class=\"hintlink\"  id=\"hintlink8\">\u63d0\u793a #2<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=20   class='pyboxCode RW'>\ndef digitalSum(n):\n if n &lt; 10:\n return n\n else:\n # recursive case\n<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode6' value='def digitalSum(n):\\n if n < 10:\\n return n\\n else:\\n # recursive case\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(6)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(6,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(6,'16.digitalsum')\" ><\/td>\n<\/tr><\/table><select id='pbSelect6' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(6,descape($('#defaultCode6').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(6);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"d50e325444aef7ece9e819259bb4a2ee\"\/>\n<div id='pbresults6' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(6);});document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<p>\u73b0\u5728\u5199\u4e00\u4e2a\u9012\u63a8\u65b9\u7a0b\uff0c\u5c06<code>digitalSum<\/code>\u4f5c\u4e3a\u5b50\u7a0b\u5e8f\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox9'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>\u6570\u5b57\u6839<\/span><\/div>\u4e00\u4e2a\u975e\u8d1f\u6574\u6570<code>n<\/code>\u7684\u6570\u5b57\u6839\u662f\u4ee5\u5982\u4e0b\u65b9\u5f0f\u8ba1\u7b97\u7684\u3002\u4ece\u5c06\u7ec4\u6210<code>n<\/code>\u7684\u6570\u5b57\u76f8\u52a0\u5f00\u59cb\uff0c\u5f97\u5230\u7684\u6570\u5b57\u518d\u6b21\u8fdb\u884c\u6570\u5b57\u7684\u76f8\u52a0\uff0c\u76f4\u5230\u83b7\u5f97\u4e00\u4e2a\u4e00\u4f4d\u6570\u3002\u6bd4\u5982\u8bf4\uff0c2019\u7684\u6570\u5b57\u548c\u662f3\uff0c\u56e0\u4e3a2+0+1+9=12\uff0c1+2=3\u3002\u7f16\u5199\u4e00\u4e2a\u9012\u63a8\u65b9\u7a0b<code>digitalRoot(n)<\/code>\uff0c\u8fd4\u8fd8<code>n<\/code>\u7684\u6570\u5b57\u6839\u3002<br\/><\/p>\n<p><strong>\u5047\u8bbe<\/strong>\u5df2\u6709<code>digitalSum<\/code>\u65b9\u7a0b\u3002<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(9)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(9,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(9,'16.digitalroot')\" ><\/td>\n<\/tr><\/table><select id='pbSelect9' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(9);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"cbd55de683476c316285ec4cc6053750\"\/>\n<div id='pbresults9' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(9);});document.getElementById(\"submit9\").value = \"Run program\";document.getElementById(\"inputInUse9\").value = \"N\";<\/script>\n<\/p>\n<h1>\u4e60\u9898<\/h1>\n<div class='pybox modeNeutral' id='pybox10'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Short Answer Exercise: <\/span><span class='title'>GCD<\/span><\/div>\u4e0b\u5217\u9012\u63a8\u7a0b\u5e8f\u7684\u8f93\u51fa\u662f\u4ec0\u4e48?<\/p>\n<pre>def gcd(a, b):\r\n \u00a0if b == 0: return a\r\n \u00a0return gcd(b, a % b)\r\nprint(gcd(20, 12))<\/pre><label for=\"pyShortAnswer10\">Your answer (enter a number): <\/label><input type=\"text\" onkeypress=\"{if (event.keyCode==13) pbShortCheck(10)}\" id=\"pyShortAnswer10\"><div class=\"pyboxbuttons\"><input type=\"hidden\" name=\"type\" value=\"number\"\/><input type=\"hidden\" name=\"correct\" value=\"4\"\/><input type=\"hidden\" name=\"slug\" value=\"16.shortgcd\"\/><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick = 'pbShortCheck(10)'\/><\/div><div class=\"pbresults\" id=\"pyShortResults10\"><\/div><div class=\"epilogue\">\u6b63\u786e\uff01\u6b64\u7a0b\u5e8f\u975e\u5e38\u77ed\uff0c\u80fd\u591f\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684<strong>\u6700\u5927<\/strong><strong>\u516c<\/strong><strong>\u7ea6\u6570<\/strong>\u3002\u8fd9\u4e5f\u88ab\u53eb\u505a<a>\u6b27\u51e0\u91cc\u5fb7\u7b97\u6cd5<\/a>\uff0c\u662f\u6700\u53e4\u8001\u7684\u7b97\u6cd5\u4e4b\u4e00\u3002<\/div><\/div>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform11\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox11'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Hailstone<\/span><\/div>\u4e00\u4e2a\u4ee5\u6b63\u6574\u6570n \u5f00\u59cb\u7684\u51b0\u96f9<br \/>\n\u5e8f\u5217\u662f\u6309\u7167\u5982\u4e0b\u4e24\u4e2a\u7b80\u5355\u89c4\u5219\u751f\u6210\u7684\u3002\u5982\u679c<code>n<\/code>\u662f\u5076\u6570\uff0c\u5e8f\u5217\u4e2d\u4e0b\u4e00\u9879\u662f<code>n\/2<\/code>\u3002\u5982\u679c<code>n<\/code>\u662f\u5947\u6570\uff0c\u5e8f\u5217\u4e2d\u4e0b\u4e00\u9879\u662f<code>3*n+1<\/code>\u3002\u91cd\u590d\u8be5\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u51b0\u96f9\u5e8f\u5217\u3002\u7f16\u5199\u4e00\u4e2a\u9012\u63a8\u65b9\u7a0b<code>hailstone(n)<\/code>\uff0c\u6253\u5370\u4ece<code>n<\/code>\u5f00\u59cb\u7684\u51b0\u96f9\u5e8f\u5217\u3002\u76f4\u5230\u5e8f\u5217\u4e2d\u7684\u67d0\u4e00\u9879\u5f97\u5230<code>1<\/code>\u65f6\u505c\u6b62\uff08\u56e0\u4e3a\u5982\u679c\u4e0d\u505c\u6b62\uff0c\u6211\u4eec\u5c31\u4f1a\u5faa\u73af1, 4, 2, 1, 4, 2, ...\uff09<br\/> \u6bd4\u5982\uff0c\u5f53<code>n=5<\/code>\uff0c\u7a0b\u5e8f\u5e94\u8be5\u8f93\u51fa\u4e0b\u5217\u5e8f\u5217\uff1a<\/p>\n<pre>5\r\n16\r\n8\r\n4\r\n2\r\n1<\/pre><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode11' id='usercode11'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory11' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit11' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(11)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(11,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(11,'16.hailstone')\" ><\/td>\n<\/tr><\/table><select id='pbSelect11' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(11);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse11\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"11\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"299c7c6ff8832bad3839231662840da4\"\/>\n<div id='pbresults11' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(11);});document.getElementById(\"submit11\").value = \"Run program\";document.getElementById(\"inputInUse11\").value = \"N\";<\/script>\n<\/p>\n<p>\u6570\u5b66\u5bb6\u4eec\u8ba4\u4e3a\uff0c\u6bcf\u4e00\u4e2a\u51b0\u96f9\u5e8f\u5217\u6700\u540e\u90fd\u4f1a\u8fbe\u52301\uff0c\u4e0d\u8bba<code>n<\/code>\u503c\u5f00\u59cb\u4e3a\u591a\u5c11\u3002\u7136\u800c\uff0c\u8fd8\u6ca1\u6709\u4eba\u80fd\u8bc1\u660e\u8fd9\u4e2a\u5b9a\u7406\u3002<\/p>\n<h2>\u5d4c\u5957\u5e8f\u5217<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u9012\u63a8\u5faa\u73af\u7684\u5e94\u7528\u3002<em>\u5d4c\u5957\u5e8f\u5217<\/em>\u662f\u6307\u5c06\u4e00\u4e9b\u5e8f\u5217\u653e\u5728\u53e6\u4e00\u4e9b\u5e8f\u5217\u91cc\u9762\uff0c\u4e0d\u8bba\u91cd\u590d\u6b21\u6570\u3002\u6bd4\u5982\uff0c\u6709\u5173\u6574\u6570\u7684\u5d4c\u5957\u5e8f\u5217\u662f<code>[[1, 2], [9, 10]]<\/code>\uff0c<code>[[1], 2]<\/code>\u548c<code>x\u00a0= [[1], 2, [3, [[4]]]]<\/code>\u3002\u6700\u540e\u4e00\u4e2a\u5d4c\u5957\u5e8f\u5217\u662f\u4e00\u4e2a\u62e5\u6709\u4e09\u9879\u7684\u5e8f\u5217\uff1a<code>x[0]==[1]<\/code>\u662f\u7b2c\u4e00\u9879\uff0c<code>x[1]==2<\/code>\u662f\u7b2c\u4e8c\u9879\uff0c<code>x[2]==[3, [[4]]]<\/code>\u662f\u6700\u540e\u4e00\u9879\uff08\u6240\u4ee5<code>x<\/code>\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a3\u7684\u5e8f\u5217\uff09\u3002\u6ce8\u610f\uff0c\u4e00\u4e2a\u7c7b\u4f3c\u4e8e<code>[1, 2, 3]<\/code>\u7684\u5e8f\u5217\u4e5f\u88ab\u8ba4\u4e3a\u662f\u5d4c\u5957\u5e8f\u5217\u3002<strong>\u6211\u4eec\u662f\u5426\u80fd\u5199\u4e00\u4e2a\u65b9\u7a0b\uff0c\u8ba1\u7b97\u51fa<em>\u4efb\u4f55<\/em>\u5d4c\u5957\u5e8f\u5217\u7684\u6574\u6570\u7684\u548c\u4e48\uff1f<\/strong>\u6bd4\u5982\uff0c\u8f93\u5165\u503c\u662f<code>[[5], 2, [10, 8, [[7]]]]<\/code>\u7684\u8bdd\uff0c\u8f93\u51fa\u503c\u5c31\u5e94\u8be5\u662f<code>32<\/code>\u3002<\/p>\n<p>\u8fd9\u4e2a\u4efb\u52a1\u5982\u679c\u7528<code>while<\/code>\u5faa\u73af\u6216<code>for<\/code>\u5faa\u73af\u5c31\u6709\u4e9b\u96be\uff0c\u56e0\u4e3a\u6211\u4eec\u60f3\u8981\u4e00\u4e2a\u80fd\u5904\u7406\u4efb\u4f55\u5f62\u5f0f\u7684\u5d4c\u5957\u5e8f\u5217\u7684\u65b9\u7a0b\u3002\u7136\u800c\uff0c\u5d4c\u5957\u5e8f\u5217\u6709\u4e00\u4e2a\u9012\u63a8\u7ed3\u6784\uff1a<em>\u4e00\u4e2a\u5d4c\u5957\u5e8f\u5217\u662f\u4e00\u4e2a\u5e8f\u5217\uff0c\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u9879\u8981\u4e48\u662f(a)\u4e00\u4e2a\u6574\u6570\uff0c\u8981\u4e48\u662f(b)\u4e00\u4e2a\u5d4c\u5957\u5e8f\u5217<\/em>\u3002\u5e76\u4e14\uff0c\u4e00\u65e6\u6211\u4eec\u8ba1\u7b97\u6bcf\u4e2a\u4e3b\u5e8f\u5217\u4e2d\u7684\u4e00\u90e8\u5206\u7684\u548c\u65f6\uff0c\u8fd9\u4e9b\u503c\u7684\u548c\u4e5f\u5c31\u662f\u603b\u548c\u3002\u4e0b\u5217\u4ee3\u7801\u53ef\u7528\u6765\u8868\u8fbe\u4e0a\u8ff0\u6240\u8bf4\uff1b<code>isinstance(x, int)<\/code>\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u544a\u8bc9\u6211\u4eec<code>x<\/code>\u662f\u5426\u662f\u4e00\u4e2a\u6574\u6570\uff08\u6216\u5e8f\u5217\uff09\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform12\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox12'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>\u5d4c\u5957\u5e8f\u5217\u6c42\u548c<\/span><\/div>\u8fd0\u7528\u9012\u63a8\u65b9\u7a0b\u8ba1\u7b97\u4e00\u4e2a\u5d4c\u5957\u5e8f\u5217\u4e2d\u6240\u6709\u9879\u7684\u548c\u3002\u70b9\u51fb<strong>Run<\/strong>\u5c31\u53ef\u770b\u5230\u8be5\u65b9\u7a0b\u5728\u4e00\u4e9b\u6d4b\u8bd5\u4e2d\u7684\u8f93\u51fa\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 214px;'><textarea wrap='off' name='usercode12' id='usercode12'  cols=10 rows=8 readonly='readonly'  style = 'height : 214px;'  class='pyboxCode RO'>\ndef nestedListSum(NL):\n    if isinstance(NL, int):     # \u4f8b(a): NL\u662f\u4e00\u4e2a\u6574\u6570\n        return NL               # \u57fa\u672c\u6848\u4f8b\n\n    sum = 0                     # \u4f8b(b): NL\u662f\u4e00\u5e8f\u5217\u7684\u5d4c\u5957\u5e8f\u5217\n    for i in range(0, len(NL)): # \u4e3b\u5e8f\u5217\u7684\u6bcf\u4e00\u90e8\u5206\u90fd\u52a0\u5230\u4e00\u8d77\n        sum = sum + nestedListSum(NL[i])\n    return sum                  # \u7ed3\u675f<\/textarea><\/div>\n<div id='pbhistory12' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput12\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit12' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch12\" value=\"Input Switch\" onclick=\"pbInputSwitch(12,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(12)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(12,'Y')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse12\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"12\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"d6a8d7a48246943f83fc9614735e68c0\"\/>\n<div id='pbresults12' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(12,\"Y\");<\/script>\n<\/p>\n<p>\u9012\u63a8\u7528\u6765\u5c06\u6bcf\u4e2a\u5d4c\u5957\u5e8f\u5217\u5316\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\u3002\u6bd4\u5982\u8bf4\uff0c<code>nestedListSum([1, [3, 4], 5])<\/code>\u603b\u5171\u4f1a\u6267\u884c6\u6b21\u9012\u63a8\u8c03\u7528\uff1b\u7b2c\u4e00\u6b21\uff1b\u7136\u540e\u662f<code>1<\/code>\uff1b\u7136\u540e\u662f<code>[3, 4]<\/code>\uff1b\u7136\u540e\u662f<code>3<\/code>\uff1b\u7136\u540e\u662f<code>4<\/code>\uff08\u7136\u540e<code>[3, 4]<\/code>\u7684\u548c\u662f<code>7<\/code>\uff09\uff1b\u6700\u540e\u662f<code>5<\/code>\uff08\u7136\u540e\u603b\u548c\u662f<code>13<\/code>\uff09\u3002\u8fd9\u4e0e\u5728\u53ef\u89c6\u5316\u7a97\u53e3\u4e2d\u7684\u7a0b\u5e8f\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=def+nestedListSum%28NL%29%3A%0A++if+isinstance%28NL%2C+int%29%3A%0A++++return+NL%0A%0A++sum+%3D+0%0A++for+i+in+range%280%2C+len%28NL%29%29%3A%0A++++sum+%3D+sum+%2B+nestedListSum%28NL%5Bi%5D%29%0A++return+sum%0A%0A%23+some+examples%0AnestedListSum%28129%29%0AnestedListSum%28%5B400%2C+50%2C+6%5D%29%0AnestedListSum%28%5B%5B1%2C+2%5D%2C+%5B3%2C+4%5D%2C+5%5D%29%0AnestedListSum%28%5B%5B1%2C+%5B2%2C+3%5D%2C+4%2C+5%5D%5D%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform13\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox13'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>\u641c\u7d22\u4e00\u4e2a\u5d4c\u5957\u5e8f\u5217<\/span><\/div>\u901a\u8fc7\u7f16\u5199\u7c7b\u4f3c\u4e8e<code>nestedListSum<\/code>\u7684\u7a0b\u5e8f\uff0c\u5b9a\u4e49\u4e00\u4e2a\u9012\u63a8\u65b9\u7a0b<\/p>\n<pre>nestedListContains(NL, target)<\/pre>\uff0c\u5176\u4e2d<code>NL<\/code>\u662f\u4e00\u4e2a\u7531\u6574\u6570\u7ec4\u6210\u7684\u5d4c\u5957\u5e8f\u5217\uff0c<code>target<\/code>\u662f\u4e00\u4e2a\u6574\u6570\u3002\u8be5\u65b9\u7a0b\u9700\u8981\u8868\u660e\u5d4c\u5957\u5e8f\u5217\u4e2d\u662f\u5426\u5b58\u5728<code>target<\/code>\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53<code>target<\/code>\u786e\u5b9e\u5b58\u5728\uff0c\u8fd4\u8fd8\u5e03\u5c14\u503c<code>True<\/code>\uff1b\u5f53<code>target<\/code>\u4e0d\u5b58\u5728\uff0c\u8fd4\u8fd8<code>False<\/code>\u3002<br\/>\u6bd4\u5982\uff0c<code>nestedListContains([1, [2, [3], 4]], 3)<\/code>\u5e94\u8be5\u8fd4\u8fd8<code>True<\/code>\uff0c<code>nestedListContains([1, [2, [3], 4]], 5)<\/code>\u5e94\u8be5\u8fd4\u8fd8<code>False<\/code>\u3002<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode13' id='usercode13'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory13' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit13' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(13)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(13,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(13,'17.nestedFind')\" ><\/td>\n<\/tr><\/table><select id='pbSelect13' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(13);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse13\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"13\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"9714ade1a82ade6a44f70430ca3624fd\"\/>\n<div id='pbresults13' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(13);});document.getElementById(\"submit13\").value = \"Run program\";document.getElementById(\"inputInUse13\").value = \"N\";<\/script>\n<\/p>\n<h2>\u89c4\u5219<\/h2>\n<p>\u9012\u63a8\u548c\u4e0d\u89c4\u5219\u56fe\u5f62\u6709\u5173--\u56fe\u7247\u7531\u591a\u4e2a\u5c0f\u56fe\u7247\u7ec4\u6210\u3002\u8fd9\u4e2a\u7f51\u9875\u7684\u9876\u7aef\u7684\u6a2a\u5e45\u5c31\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002\u4e0b\u4e00\u4e2a\u7ec3\u4e60\u9898\u662f\u901a\u8fc7\u9012\u63a8\u521b\u9020\u4e00\u4e2a\u7b80\u5355\u7684\u91cd\u590d\u5f62\u5f0f\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform14\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox14'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>\u5206\u5c3a<\/span><\/div>\u6574\u7406\u884c\u7684\u987a\u5e8f\u6765\u521b\u5efa\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u5c3a\u5b50\u72b6\u7684\u56fe\u6848\u3002\u6bd4\u5982\uff0c\u5f53<code>n=3<\/code>\u65f6\uff0c\u7a0b\u5e8f\u5e94\u8f93\u51fa\u4e0b\u5217\u8bbe\u8ba1\u3002<\/p>\n<pre>-\r\n--\r\n-\r\n---\r\n-\r\n--\r\n-<\/pre><ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble14\">\n <li class=\"pyscramble\">   ruler(n - 1)<\/li>\n <li class=\"pyscramble\">  else:<\/li>\n <li class=\"pyscramble\">def ruler(n):<\/li>\n <li class=\"pyscramble\">   ruler(n - 1)<\/li>\n <li class=\"pyscramble\">   print(n * &#039;-&#039;)<\/li>\n <li class=\"pyscramble\">  if n == 1:<\/li>\n <li class=\"pyscramble\">   print(&#039;-&#039;)<\/li>\n<\/ul>\n<input type='hidden' id='usercode14' name='usercode14'\/>\n<div id='pbhistory14' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit14' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse14\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"14\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"672cba3d5cfd81b28eb5d9b6ac12954a\"\/>\n<div id='pbresults14' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit14\").value = \"Run program\";document.getElementById(\"inputInUse14\").value = \"N\";<\/script>\n<\/p>\n<p>\u606d\u559c\uff01\u4f60\u53ef\u4ee5\u8fdb\u884c\u4e0b\u4e00\u8bfe\u7684\u5b66\u4e60\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u4e4b\u524d\u5df2\u7ecf\u89c1\u5230\u4e86\u65b9\u7a0b\u53ef\u4ee5\u7528\u6765\u7ec4\u7ec7\uff0c\u91cd\u590d\u4f7f\u7528\u90e8\u5206\u4ee3\u7801\u3002\u6211\u4eec\u4e5f\u5df2\u7ecf\u770b\u5230\u4e86\u65b9\u7a0b\u53ef\u4ee5\u6839\u636e\u5176\u4ed6\u65b9\u7a0b\u88ab\u5b9a\u4e49\u3002\u5728\u8fd9\u8282\u8bfe\u4e2d\uff0c\u6211\u4eec\u4f1a\u5b66\u4e60\u4e00\u4e2a\u65b9\u7a0b\u662f\u5982\u4f55\u6839\u636e\u81ea\u8eab\u88ab\u5b9a\u4e49\u7684\uff01\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\u53eb\u505a\u9012\u63a8\u3002\u6709\u4e00\u53e5\u4fd7\u8bed\uff1a\"\u4e3a\u4e86\u7406\u89e3\u9012\u63a8\uff0c\u4f60\u9996\u5148\u8981\u7406\u89e3\u9012\u63a8\u3002\" \u8303\u4f8b \u5728\u5173\u4e8e\u5faa\u73af\u7684\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8fc7while\u5faa\u73af\u6765\u521b\u9020\u4e0b\u5217\u8f93\u51fa\u3002 5 4 3 2 1 Blastoff!\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9012\u63a8\u7684\u7a0b\u5e8f\uff0c\u80fd\u591f\u53d6\u5f97\u76f8\u540c\u7684\u6548\u679c\u3002 Example\u4f7f\u7528\u9012\u63a8\u5012\u6570 def countdown(n): if n == 0: print('Blastoff!') else: print(n) countdown(n - 1) countdown(5) \u8ba9\u6211\u4eec\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u6253\u5370\u8bed\u53e5\u6765\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u7248\u672c\u4e5f\u4ece\u8f93\u5165\u503c\u8bfb\u53d6\u65f6\u95f4\u671f\u9650\u3002 Example\u4f7f\u7528\u9012\u63a8\u5012\u6570 def countdown(n): print('Entering countdown(',n,')') if n == 0: print('Blastoff!') else: print(n) countdown(n - 1) print('Exiting &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-16\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-9306","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/comments?post=9306"}],"version-history":[{"count":13,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9306\/revisions"}],"predecessor-version":[{"id":10238,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9306\/revisions\/10238"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}