{"id":9303,"date":"2016-06-07T23:30:10","date_gmt":"2016-06-08T03:30:10","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9303"},"modified":"2017-06-28T18:54:45","modified_gmt":"2017-06-28T22:54:45","slug":"zh-15a","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-15a\/","title":{"rendered":"15A: \u7a0b\u5e8f\u7ec8\u6b62\u7684\u6d4b\u5b9a"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p><em>\u7ec3\u4e6015A\u300115B\u548c15C\u4e0d\u5206\u5148\u540e\u5b66\u4e60\u987a\u5e8f\u3002<\/em><\/p>\n<p>\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u4f60\u5c06\u5b8c\u6210\u4e00\u4e2a\u957f\u800c\u590d\u6742\u7684\u95ee\u9898\uff0c\u6211\u4eec\u5df2\u7ecf\u628a\u5b83\u5206\u89e3\u6210\u4e86\u51e0\u4e2a\u90e8\u5206\uff0c\u6240\u4ee5\u53ef\u4ee5\u5206\u90e8\u5b8c\u6210\u3002\u672c\u8bfe\u8fd8\u5c06\u4ecb\u7ecd<code>str.split()<\/code>\uff0c\u5b83\u662f\u4e00\u4e2a\u6709\u7528\u7684\u5206\u5272\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1a\u5220\u9664\u6240\u6709\u7684\u7a7a\u683c\uff0c\u7136\u540e\u8fd4\u8fd8\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5355\u8bcd\u7684\u5e8f\u5217\uff0c\u5e76\u4e14\u539f\u59cb\u5b57\u7b26\u4e32\u4e0d\u4f1a\u88ab\u4fee\u6539\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><code>str.split()<\/code>\u7684\u4f8b\u5b50<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 188px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=7 readonly='readonly'  style = 'height : 188px;'  class='pyboxCode RO'>\nS = 'This is a string'\nwords = S.split()\nprint(words)\nprint(len(words))\nprint(words[1])\nprint(S)   # S\u6ca1\u6709\u88ab\u6539\u53d8\nprint('  Another example  of split '.split())<\/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=\"c73fccda15058f4f32cd5e7c71100705\"\/>\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><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> \u4f60\u53ef\u4ee5\u8c03\u7528<code>split()<\/code>\u5e76\u52a0\u4e0a\u7279\u6b8a\u7684\u53c2\u6570\u6765\u505a\u66f4\u590d\u6742\u7684\u5206\u5272\uff0c\u4f46\u76ee\u524d\u6211\u4eec\u4e0d\u9700\u8981\u8fd9\u4e48\u505a\u3002\u5982\u679c\u611f\u5174\u8da3\u7684\u8bdd\uff0c\u53ef\u4ee5\u67e5\u770b\u4e00\u4e0b<a href=\"http:\/\/docs.python.org\/py3k\/library\/stdtypes.html#str.split\">Python\u6587\u6863<\/a>. <\/span><\/td><\/table><\/p>\n<p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u5b8c\u6210\u7b2c\u4e00\u6b65\u3002\u65e7\u7684\u7f16\u7a0b\u8bed\u8a00 <a href=\"http:\/\/en.wikipedia.org\/wiki\/BASIC\">BASIC<\/a> \u56e0\u5176\u5e26\u6709\u7f16\u53f7\u7684\u4ee3\u7801\u884c\u548c<code>goto<\/code>\u8bed\u53e5\u800c\u51fa\u540d\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7ec3\u4e60\uff0c\u4f60\u5c06\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u4e0b\u9762\u8fd9\u4e9b\u529f\u80fd\u7684BASIC\u7b80\u5355\u7248\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7a0b\u5e8f\u7684\u8f93\u5165\u5c06\u7531\u4e0b\u9762\u683c\u5f0f\u4e2d\u7684\u51e0\u884c\u7ec4\u6210<\/p>\n<p style=\"text-align: center;\"><code>\u00ablabel\u00bb goto \u00abtarget\u00bb<\/code><\/p>\n<p><code>\u00ablabel\u00bb<\/code>\u548c<code>\u00abtarget\u00bb<\/code>\u90fd\u662f\u6b63\u6574\u6570\u3002<em>label<\/em>\uff08\u6807\u7b7e\uff09\u5c31\u50cf\u662f\u8fd9\u4e00\u884c\u7684\u540d\u5b57\u6216\u8005\u5730\u5740\uff1b<em>\u6240\u6709\u7684\u6807\u7b7e\u90fd\u662f\u552f\u4e00\u7684<\/em>\u3002<em>target<\/em>\uff08\u76ee\u6807\uff09\u4f1a\u544a\u8bc9\u4f60\u9700\u8981\u79fb\u52a8\u5230\u7684\u4e0b\u4e00\u884c\u4ee3\u7801\u7684\u6807\u7b7e\u662f\u4ec0\u4e48\u3002\u7a0b\u5e8f\u7684\u6700\u540e\u4e00\u884c\u662f<code>\u00ablabel\u00bb END<\/code>\uff0c\u8868\u660e\u4e00\u65e6\u8fd0\u884c\u5230\u8fd9\u4e00\u884c\uff0c\u7a0b\u5e8f\u5c31\u5e94\u8be5\u505c\u6b62\u3002\u4e0b\u9762\u662f\u4e00\u4e2aBASIC\u7a0b\u5e8f\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>5 GOTO 30\r\n10 GOTO 20\r\n20 GOTO 10\r\n30 GOTO 40\r\n40 END<\/pre>\u5f53\u8fd9\u4e2aBASIC\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6211\u4eec\u5728\u7b2c\u4e00\u884c\uff08\u6807\u7b7e5\uff09\u5904\u5f00\u59cb\u3002\u6807\u7b7e5\u8fd9\u884c\u4ee3\u7801\u7684\u76ee\u6807\u662f30\uff0c\u6240\u4ee5\u4e0b\u4e00\u6b65\u6211\u4eec\u524d\u5f80\u6807\u7b7e\u4e3a30\u7684\u8fd9\u4e00\u884c\u3002\u7136\u540e30\u8fd9\u4e00\u884c\u544a\u8bc9\u6211\u4eec\u53bb\u6807\u7b7e\u4e3a40\u7684\u4ee3\u7801\u884c\u3002\u6700\u540e\u8fd9\u884c\u4ee3\u7801\u544a\u8bc9\u6211\u4eec\u7a0b\u5e8f\u7ed3\u675f\u3002\u6240\u4ee5\uff0c\u7a0b\u5e8f\u5c31\u7ec8\u6b62\u4e86\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u4e00\u4e2aBASIC\u7a0b\u5e8f\u4e5f\u53ef\u4ee5\u5faa\u73af\u3002\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>10 GOTO 21\r\n21 GOTO 37\r\n37 GOTO 21\r\n40 END<\/pre>\u8be5\u7a0b\u5e8f\u5728\u7b2c10\u884c\u5f00\u59cb\uff0c\u4f46\u5b83\u4f1a\u6c38\u8fdc\u572821\u548c37\u884c\u4e4b\u95f4\u5faa\u73af\u3002<\/p>\n<p><strong>\u4f60\u7684\u4efb\u52a1<\/strong> \u662f\u8981\u7f16\u5199\u4e00\u4e2aPython\u7a0b\u5e8f, \u5b83\u53ef\u4ee5\u8bfb\u5165\u4e00\u4e2aBASIC\u7a0b\u5e8f\u4f5c\u4e3a\u8f93\u5165\u3002\u5982\u679c\u7a0b\u5e8f\u7ec8\u6b62\uff0c\u4f60\u7684\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370<code>success<\/code>\u3002\u5982\u679c\u7a0b\u5e8f\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370<code>infinite loop<\/code>\u3002\u5047\u8bbe\u6bcf\u4e2a\u76ee\u6807\u90fd\u6709\u6709\u6548\u7684\u6807\u7b7e\uff0c\u6240\u6709\u7684\u6807\u7b7e\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u6240\u4ee5\u4e0d\u5fc5\u68c0\u67e5\u3002<br \/>\n\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u4f46\u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5e76\u628a\u5b83\u5206\u89e3\u62103\u4e2a\u5b50\u4efb\u52a1\uff08\u8bfe\u7a0b15C\u4e2d\u6709\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u95ee\u9898\uff0c\u4f60\u9700\u8981\u81ea\u5df1\u8bbe\u8ba1\u5b50\u4efb\u52a1\uff09\u3002<\/p>\n<h2>\u5b50\u4efb\u52a11: \u8bfb\u53d6\u7a0b\u5e8f<\/h2>\n<p>\u60f3\u8981\u8bfb\u53d6BASIC\u7a0b\u5e8f\uff0c\u6211\u4eec\u9700\u8981\u4e0d\u505c\u5730\u8c03\u7528<code><a href=\"\/5-input\/\">input()<\/a><\/code>\u3002\u4f46\u662f\uff0c\u5f53\u6211\u4eec\u8fbe\u5230\u6700\u540e\u4e00\u884cBASIC\u4ee3\u7801(\u5e26\u6709<code>END<\/code>\u7684\u90a3\u4e00\u884c)\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8981\u505c\u6b62\u8c03\u7528<code>input()<\/code>\u6765\u907f\u514d<code>EOFError<\/code>\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox1'>\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'>\u8bfb\u53d6\u7a0b\u5e8f<\/span><\/div>\u7f16\u5199\u4e00\u4e2a\u6ca1\u6709\u5b9e\u53c2\u7684\u51fd\u6570 <code>getBASIC()<\/code>\uff0c\u4f7f\u5b83\u80fd\u591f\u505a\u4e0b\u5217\u8fd9\u4e9b: \u4f7f\u7528while\u5faa\u73af\u6765\u6301\u7eed\u9605\u8bfb\u8f93\u5165\u7684\u6bcf\u4e00\u884c\uff1b\u5f53\u5b83\u7ed3\u675f\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u5f62\u5f0f\u7684\u6574\u4f53\u7a0b\u5e8f\u3002(\u63d0\u793a: \u5173\u4e8e\u00a0<a class=\"hintlink\"  id=\"hintlink2\">\u5217\u8868<\/a> \u548c\u00a0<a class=\"hintlink\"  id=\"hintlink3\">\u7ed3\u675f<\/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='usercode1' id='usercode1'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/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<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(1,'15a.getbasic')\" ><\/td>\n<\/tr><\/table><select id='pbSelect1' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(1);\" >Help<\/option>\n<\/select><\/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=\"10fd1263f83e93ac606c8a709f5a623c\"\/>\n<div id='pbresults1' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(1);});document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<h2>\u5b50\u4efb\u52a12: Go to it!<\/h2>\n<p>\u4e00\u65e6\u8bfb\u53d6\u4e86\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u6211\u4eec\u5c31\u9700\u8981\u80fd\u591f\u5728\u7a0b\u5e8f\u4e2d\u8df3\u8dc3\u5230\u53e6\u4e00\u884c\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u4f60\u9700\u8981\u5b8c\u6210\u4ee5\u4e0b\u5b50\u7a0b\u5e8f\u3002<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox4'>\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'>Go to it!<\/span><\/div>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>findLine(prog, target)<\/code> \u6765\u5b8c\u6210\u4e0b\u9762\u8fd9\u4e9b\u5185\u5bb9\u3002\u5047\u8bbe<code>prog<\/code> \u662f\u4e00\u4e2a\u542b\u6709BASIC\u7a0b\u5e8f\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u4e0e<code>getBASIC()<\/code>\u751f\u6210\u7684\u7c7b\u578b\u4e00\u6837; \u5047\u8bbe <code>target<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u4e00\u4e2a\u4ee3\u7801\u884c\u6807\u7b7e\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u4f1a\u662f\u4e00\u4e2aGOTO\u8bed\u53e5\u7684target\uff08\u76ee\u6807\uff09\u3002\u8fd9\u4e2a\u51fd\u6570\u5e94\u8be5\u8fd4\u8fd8\u7d22\u5f15<code>i<\/code> (\u4e00\u4e2a <code>0<\/code> \u5230 <code>len(prog)-1<\/code>\u4e4b\u95f4\u7684\u6574\u6570)\u4f7f\u5f97<code>prog[i]<\/code> \u6210\u4e3a\u6807\u7b7e\u7b49\u4e8e\u00a0<code>target<\/code>\u7684\u8fd9\u4e00\u884c\u3002<a class=\"hintlink\"  id=\"hintlink5\">\u63d0\u793a<\/a>\u00a0<br\/><strong>\u8f93\u5165\/\u8f93\u51fa \u793a\u4f8b:<\/strong> \u5982\u679c\u4f60\u8c03\u7528<\/p>\n<pre>findLine(['10 GOTO 20','20 END'], '10')<\/pre>\u90a3\u4e48\u8f93\u51fa\u5e94\u8be5\u662f <code>0<\/code>\uff0c\u56e0\u4e3a\u5217\u8868\u4e2d\u9879\u76ee0\u662f\u6807\u7b7e\u4e3a10\u7684\u8fd9\u4e00\u884c\u4ee3\u7801\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='usercode4' id='usercode4'  cols=10 rows=20   class='pyboxCode RW'>\n<\/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,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(4,'15a.goto')\" ><\/td>\n<\/tr><\/table><select id='pbSelect4' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(4);\" >Help<\/option>\n<\/select><\/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=\"c42945ee564cac8c115a7af48d01fa4c\"\/>\n<div id='pbresults4' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(4);});document.getElementById(\"submit4\").value = \"Run program\";document.getElementById(\"inputInUse4\").value = \"N\";<\/script>\n<\/p>\n<h2>\u5b50\u4efb\u52a13: \u667a\u80fd\u6a21\u62df<\/h2>\n<p>\u5728\u524d\u4e24\u4e2a\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u5b8c\u6210\u4e86<em>\u8f93\u5165\u7a0b\u5e8f<\/em>\u548c<em>\u641c\u5bfb\u4efb\u52a1<\/em>\u3002\u8fd9\u5c06\u6709\u52a9\u4e8e\u4e3b\u7a0b\u5e8f\u7684\u7f16\u5199\u5e76\u4f7f<br \/>\n\u5176\u66f4\u52a0\u7b80\u7ec3\u3002\u7136\u800c\uff0c\u4ecd\u7136\u6709\u4e00\u4e2a\u4e3b\u8981\u7684\u95ee\u9898\u8981\u89e3\u51b3\uff1a\u5982\u4f55\u624d\u80fd\u771f\u6b63\u89e3\u51b3\u6700\u521d\u7684\u95ee\u9898\uff1f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u6a21\u62dfBASIC\u7a0b\u5e8f\u7684\u8fd0\u884c\uff1a<\/p>\n<ul>\n<li>\u8ba9 <code>prog<\/code> \u4f5c\u4e3aBASIC\u7a0b\u5e8f (\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868)<\/li>\n<li>\u5f00\u59cb\u540d\u4e3a<code>location<\/code>\u7684\u8ba1\u6570\u53d8\u91cf\uff1b\u56e0\u4e3a\u6211\u4eec\u662f\u4ece\u7a0b\u5e8f\u7684\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u6240\u4ee5\u8bbe\u5b9a\u521d\u59cb\u503c\u4e3a0<\/li>\n<li>\u6b63\u786e\u65f6,\n<ul>\n<li>\u5982\u679c<code>prog[location]<\/code>\u662f\u7ed3\u675f\u884c\uff0c\u8fd4\u8fd8\"success\"\u5e76\u7ec8\u6b62\u3002<\/li>\n<li>\u8ba9 <code>T<\/code> \u4f5c\u4e3a\u5728<code>prog[location]<\/code>\u4e2d\u6307\u51fa\u7684\u76ee\u6807\u5b57\u7b26\u4e32<\/li>\n<li>\u8ba9<code>findLine(prog, T)<\/code>\u4f5c\u4e3a<code>location<\/code>\u7684\u65b0\u503c<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4f46\u662f\u8fd8\u6709\u4e00\u4e2a\u4e3b\u8981\u95ee\u9898\u6ca1\u6709\u89e3\u51b3\uff1a\u65e0\u6cd5\u68c0\u6d4b\u65e0\u9650\u5faa\u73af\uff0c\u5982\u679cBASIC\u7a0b\u5e8f\u6709\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u90a3\u4e48Python\u7a0b\u5e8f\u4e5f\u5c06\u6c38\u8fdc\u5faa\u73af\u3002\u800c\u6211\u4eec\u60f3\u8981\u7684\u662f\uff1a\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8be5\u7a0b\u5e8f\u6253\u5370\u201c\u65e0\u9650\u5faa\u73af\u201d\u3002\u6211\u4eec\u628a\u8fd9\u4e2a\u95ee\u9898\u7559\u7ed9\u4f60\u89e3\u51b3\uff0c\u4f46\u662f\u4f1a\u7ed9\u4f60\u4e00\u4e2a\u63d0\u793a\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'>Smart Simulation<\/span><\/div>\u7f16\u5199\u4e00\u4e2a\u51fd\u6570 <code>execute(prog)<\/code> \u6765\u5b8c\u6210\u4e0b\u5217\u8fd9\u4e9b\u4e8b\u3002\u548c\u524d\u9762\u4e00\u6837\uff0c\u5047\u8bbe <code>prog<\/code> \u662f\u4e00\u4e2a\u542b\u6709BASIC\u7a0b\u5e8f\u7684\u5b57\u7b26\u4e32\u5217\u8868\u3002\u7136\u540e\u53d6\u51b3\u4e8e\u7a0b\u5e8f\u662f\u5426\u7ec8\u6b62\u6216\u5faa\u73af\uff0c\u7a0b\u5e8f\u5e94\u8be5\u8fd4\u8fd8 \"<code>success<\/code>\" \u6216\u8005 \"<code>infinite loop<\/code>\"\u3002<strong>\u91cd\u8981\u4fe1\u606f<\/strong>: \u5047\u8bbe\u8fd9\u4e2a\u51fd\u6570<code>findLine(prog, target)<\/code> \u7684\u5b9a\u4e49\u5728\u5b50\u4efb\u52a12\u4e2d\u5df2\u7ecf\u5b9a\u4e49\u597d\u4e86\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u5199\u3002 <a class=\"hintlink\"  id=\"hintlink7\">\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='usercode6' id='usercode6'  cols=10 rows=20   class='pyboxCode RW'>\n#\u8fd9\u662f\u4e00\u4e2a\u6709\u5f85\u4fee\u6539\u7b54\u6848\ndef execute(prog):\n location = 0\n while True:\n if location==len(prog)-1: return \"success\"\n #\u4f7f\u7528str.split\u4eceprog[location]\u5f97\u5230T\n location = findLine(prog, T)\n<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode6' value='#\\u8fd9\\u662f\\u4e00\\u4e2a\\u6709\\u5f85\\u4fee\\u6539\\u7b54\\u6848\\ndef execute(prog):\\n location = 0\\n while True:\\n if location==len(prog)-1: return \\\"success\\\"\\n #\\u4f7f\\u7528str.split\\u4eceprog[location]\\u5f97\\u5230T\\n location = findLine(prog, T)\\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,'15a.main')\" ><\/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=\"98adbd492d84cf7abb2d29127062ce40\"\/>\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<h2>\u6574\u5408<\/h2>\n<p>\u8981\u6d4b\u8bd5\u4f60\u7684\u4ee3\u7801\uff0c\u590d\u5236\u5e76\u7c98\u8d34\u4f60\u4ee5\u524d\u7684\u7b54\u6848\u5230\u4e0b\u9762\u7684\u6a21\u677f\u4e2d\u3002<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox8'>\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'>BASIC\u6a21\u62df\u5668<\/span><\/div>\u628a\u4f60\u7684BASIC\u6a21\u62df\u5668\u201c\u7ec4\u88c5\u201d\u597d\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='usercode8' id='usercode8'  cols=10 rows=20   class='pyboxCode RW'>\n#\u5b50\u4efb\u52a11\n\n#\u5b50\u4efb\u52a12\n\n#\u5b50\u4efb\u52a13\n\nprint(execute(getBASIC()))\n<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode8' value='#\\u5b50\\u4efb\\u52a11\\n\\n#\\u5b50\\u4efb\\u52a12\\n\\n#\\u5b50\\u4efb\\u52a13\\n\\nprint(execute(getBASIC()))\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(8)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(8,'N')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(8,'15a.overall')\" ><\/td>\n<\/tr><\/table><select id='pbSelect8' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(8,descape($('#defaultCode8').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(8);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"6455c85343eb23020d4d08e70dad96ef\"\/>\n<div id='pbresults8' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(8);});document.getElementById(\"submit8\").value = \"Run program\";document.getElementById(\"inputInUse8\").value = \"N\";<\/script>\n<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> \u5982\u679c\u4f60\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u518d\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\uff0c\u90a3\u4e48\u4f60\u662f<em>\u4e0d\u53ef\u80fd<\/em>\u5199\u51fa\u4e00\u4e2a\u5173\u4e8e\u8fd9\u4e2a\u8bed\u8a00\u7684\u7ec8\u6b62\u68c0\u67e5\u7a0b\u5e8f\u7684\u3002\u8fd9\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u6700\u65e9\u7684\u4e00\u4e2a\u91cd\u8981\u5b9a\u7406\uff0c\u8fd9\u4e2a\u5b9a\u7406\u572819\u4e16\u7eaa30\u5e74\u4ee3\u88ab<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E8%89%BE%E4%BC%A6%C2%B7%E5%9B%BE%E7%81%B5\">\u827e\u4f26-\u56fe\u7075<\/a>\u8bc1\u660e\uff0c\u73b0\u5728\u4eba\u4eec\u5c06\u8fd9\u4e2a\u7ed3\u679c\u79f0\u4e3a\"\u8fd9\u4e2a<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E5%81%9C%E6%9C%BA%E9%97%AE%E9%A2%98\">\u505c\u673a\u95ee\u9898<\/a>\u662f\u4e0d\u53ef\u5224\u5b9a\u7684\u3002\" <\/span><\/td><\/table><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec3\u4e6015A\u300115B\u548c15C\u4e0d\u5206\u5148\u540e\u5b66\u4e60\u987a\u5e8f\u3002 \u5728\u8fd9\u4e00\u8bfe\u4e2d\uff0c\u4f60\u5c06\u5b8c\u6210\u4e00\u4e2a\u957f\u800c\u590d\u6742\u7684\u95ee\u9898\uff0c\u6211\u4eec\u5df2\u7ecf\u628a\u5b83\u5206\u89e3\u6210\u4e86\u51e0\u4e2a\u90e8\u5206\uff0c\u6240\u4ee5\u53ef\u4ee5\u5206\u90e8\u5b8c\u6210\u3002\u672c\u8bfe\u8fd8\u5c06\u4ecb\u7ecdstr.split()\uff0c\u5b83\u662f\u4e00\u4e2a\u6709\u7528\u7684\u5206\u5272\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1a\u5220\u9664\u6240\u6709\u7684\u7a7a\u683c\uff0c\u7136\u540e\u8fd4\u8fd8\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5355\u8bcd\u7684\u5e8f\u5217\uff0c\u5e76\u4e14\u539f\u59cb\u5b57\u7b26\u4e32\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002 Examplestr.split()\u7684\u4f8b\u5b50 S = 'This is a string' words = S.split() print(words) print(len(words)) print(words[1]) print(S) # S\u6ca1\u6709\u88ab\u6539\u53d8 print(' Another example of split '.split()) \u4f60\u53ef\u4ee5\u8c03\u7528split()\u5e76\u52a0\u4e0a\u7279\u6b8a\u7684\u53c2\u6570\u6765\u505a\u66f4\u590d\u6742\u7684\u5206\u5272\uff0c\u4f46\u76ee\u524d\u6211\u4eec\u4e0d\u9700\u8981\u8fd9\u4e48\u505a\u3002\u5982\u679c\u611f\u5174\u8da3\u7684\u8bdd\uff0c\u53ef\u4ee5\u67e5\u770b\u4e00\u4e0bPython\u6587\u6863. \u73b0\u5728\u6211\u4eec\u9700\u8981\u5b8c\u6210\u7b2c\u4e00\u6b65\u3002\u65e7\u7684\u7f16\u7a0b\u8bed\u8a00 BASIC \u56e0\u5176\u5e26\u6709\u7f16\u53f7\u7684\u4ee3\u7801\u884c\u548cgoto\u8bed\u53e5\u800c\u51fa\u540d\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7ec3\u4e60\uff0c\u4f60\u5c06\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u4e0b\u9762\u8fd9\u4e9b\u529f\u80fd\u7684BASIC\u7b80\u5355\u7248\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7a0b\u5e8f\u7684\u8f93\u5165\u5c06\u7531\u4e0b\u9762\u683c\u5f0f\u4e2d\u7684\u51e0\u884c\u7ec4\u6210 \u00ablabel\u00bb goto \u00abtarget\u00bb \u00ablabel\u00bb\u548c\u00abtarget\u00bb\u90fd\u662f\u6b63\u6574\u6570\u3002label\uff08\u6807\u7b7e\uff09\u5c31\u50cf\u662f\u8fd9\u4e00\u884c\u7684\u540d\u5b57\u6216\u8005\u5730\u5740\uff1b\u6240\u6709\u7684\u6807\u7b7e\u90fd\u662f\u552f\u4e00\u7684\u3002target\uff08\u76ee\u6807\uff09\u4f1a\u544a\u8bc9\u4f60\u9700\u8981\u79fb\u52a8\u5230\u7684\u4e0b\u4e00\u884c\u4ee3\u7801\u7684\u6807\u7b7e\u662f\u4ec0\u4e48\u3002\u7a0b\u5e8f\u7684\u6700\u540e\u4e00\u884c\u662f\u00ablabel\u00bb END\uff0c\u8868\u660e\u4e00\u65e6\u8fd0\u884c\u5230\u8fd9\u4e00\u884c\uff0c\u7a0b\u5e8f\u5c31\u5e94\u8be5\u505c\u6b62\u3002\u4e0b\u9762\u662f\u4e00\u4e2aBASIC\u7a0b\u5e8f\u7684\u793a\u4f8b\uff1a 5 GOTO 30 10 GOTO 20 20 &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-15a\/\">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-9303","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9303","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=9303"}],"version-history":[{"count":17,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9303\/revisions"}],"predecessor-version":[{"id":10234,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9303\/revisions\/10234"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}