{"id":2985,"date":"2012-01-26T17:58:50","date_gmt":"2012-01-26T22:58:50","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/?page_id=2985"},"modified":"2012-01-26T17:58:50","modified_gmt":"2012-01-26T22:58:50","slug":"2x-extra-practice","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/2x-extra-practice\/","title":{"rendered":"2X: Extra Practice"},"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>This lesson contains extra practice exercises, some of which are challenging. If you get stuck, feel free to skip to the next problem or lesson and come back later. You can always use the <a href=\"http:\/\/cscircles.cemc.uwaterloo.ca\/user-page\/\">My Progress<\/a> page to review all past exercises.<\/p>\n<p>The first exercise is about debugging. It tries to use a formula that is not quite correct to compute population growth.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox0'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Growth Debugging<\/span><\/div>Fix the logic error in this program: it should calculate the population of your country for the next 3 years, assuming it starts with 1000 people in 2012, and the number of people increases by 10% each year. You can change at most three characters. <br\/> <a class=\"hintlink\"  id=\"hintlink1\">If you get stuck, click here for a hint.<\/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='usercode0' id='usercode0'  cols=10 rows=20   class='pyboxCode RW'>\npopulationIn2012 = 1000\npopulationIn2013 = populationIn2012 * 0.1\npopulationIn2014 = populationIn2013 * 0.1\npopulationIn2015 = populationIn2014 * 0.1\n<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode0' value='populationIn2012 = 1000\\npopulationIn2013 = populationIn2012 * 0.1\\npopulationIn2014 = populationIn2013 * 0.1\\npopulationIn2015 = populationIn2014 * 0.1\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(0,'1e.growth')\" ><\/td>\n<td><input type='button' name='default' value=\"Reset code to default\" onclick=\"pbSetText(0,descape($('#defaultCode0').val()))\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(0);\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"901b6a9f7589e01f8dee33bc9984d01e\"\/>\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>The rest of the exercises in this lesson are about the <code>min<\/code> and <code>max<\/code> functions.<\/p>\n<h2>Simplifying a complex expression<\/h2>\n<div class='pybox modeNeutral' id='pybox2'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Multiple Choice Exercise: <\/span><span class='title'>Simplification<\/span><\/div><div>What is a simplification of the following expression? <\/p>\n<pre>max(x - 3, min(x + 10, x + 5))<\/pre> <\/div><label>Your choice: <\/label><select id=\"pyselect2\"><option value=\"d\" selected>Select one<\/option><option value=\"r\">x + 5<\/option><option value=\"w\">x - 3<\/option><option value=\"w\">x + 10<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" name=\"slug\" value=\"2X.simplify\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(2)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults2\"><\/div><div class=\"epilogue\">Correct! Since <code>x + 5<\/code> is always smaller than <code>x + 10<\/code>, we can simplify <code>min(x + 10, x + 5)<\/code> to just <code>x + 5<\/code>. Then working our way outwards, the entire expression is <code>max(x - 3, x + 5)<\/code>. Since <code>x + 5<\/code> is always the maximum of these two numbers, that is the result.<\/div><\/div>\n<h2>Complicating a simple expression<\/h2>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox3'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Complication<\/span><\/div>Assume that the grader defines two variables <code>A<\/code> and <code>B<\/code>\u00a0for you. Write a program which prints out the value <\/p>\n<pre>min(A, B)<\/pre> However, there is a catch: your program is not allowed to use the <code>min<\/code>\u00a0function. Instead, use <code>max<\/code> in a clever way to simulate <code>min<\/code>. <a class=\"hintlink\"  id=\"hintlink4\">Hint, Method 1<\/a><a class=\"hintlink\"  id=\"hintlink5\">Hint, Method 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='usercode3' id='usercode3'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(3,'2X.simplifyagain')\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(3);\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"72cf297adff9dd12ef34afe32bf84e7d\"\/>\n<div id='pbresults3' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/p>\n<h2>Payment Calculator<\/h2>\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:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Payment Calculator<\/span><\/div>A credit card company computes a customer's \"minimum payment\" according to the following rule. The minimum payment is equal to either $10 or 2.1% of the customer's balance, whichever is greater; but if this exceeds the balance, then the minimum payment is the balance. Write a program to print out the minimum payment using <code>min<\/code> and <code>max<\/code>. Assume that the variable\u00a0<code>balance<\/code>\u00a0contains the customer's balance. Your program does not need to print the dollar sign.<br\/> <em>Example 1<\/em>: if your <code>balance<\/code> is <code>1000<\/code>, then your program should print <code>21<\/code>. <br\/> <em>Example 2<\/em>: if your <code>balance<\/code>\u00a0is <code>600<\/code>, then your program should print <code>12.6<\/code>. <br\/> <em>Example 3<\/em>: if your <code>balance<\/code> is <code>25<\/code>, then your program should print <code>10<\/code>. <br\/> <em>Example 4<\/em>: if your <code>balance<\/code> is <code>8<\/code>, then your program should print <code>8<\/code>. <br\/> <a class=\"hintlink\"  id=\"hintlink7\">Hint<\/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<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(6,'2X.balance')\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(6);\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"31b2c12366938ec6e67dd96b7adbd635\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<h2>Sorting Scramble<\/h2>\n<p>The final problem is a challenging problem, about sorting numbers in a weird way. There are better, simpler, and faster\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Sorting_algorithm\">sorting methods<\/a>\u00a0that you can learn about after completing our introductory lessons.<\/p>\n<p>If you get stuck, feel free to skip the problem. You can always come back to it later. Additionally, you can keep track of what you have or have not finished by visiting the <a href=\"\/user-page\">My Progress<\/a> page.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox8'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>Sorting Scramble<\/span><\/div><strong>Code scramble<\/strong>: make the program\u00a0<strong>sort<\/strong>\u00a0the three numbers\u00a0<code>x<\/code>,\u00a0<code>y<\/code>\u00a0and\u00a0<code>z<\/code>\u00a0into increasing order, so that\u00a0<code>x<\/code>\u00a0has the smallest value,\u00a0<code>y<\/code>\u00a0has the next smallest value, and\u00a0<code>z<\/code>\u00a0has the largest value. <br\/><em>Drag and drop with your mouse to rearrange the lines.\u00a0<\/em><a class=\"hintlink\"  id=\"hintlink9\">Click for a hint.<\/a><ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble8\">\n <li class=\"pyscramble\">tmp = max(y, z)<\/li>\n <li class=\"pyscramble\">y = tmp<\/li>\n <li class=\"pyscramble\">z = tmp<\/li>\n <li class=\"pyscramble\">y = tmp<\/li>\n <li class=\"pyscramble\">y = min(y, z)<\/li>\n <li class=\"pyscramble\">x = min(x, y)<\/li>\n <li class=\"pyscramble\">tmp = max(x, y)<\/li>\n <li class=\"pyscramble\">tmp = max(x, y)<\/li>\n <li class=\"pyscramble\">x = min(x, y)<\/li>\n<\/ul>\n<input type='hidden' id='usercode8' name='usercode8'\/>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"6dee87aa5f8a7b67253d4de0cd55dd6f\"\/>\n<div id='pbresults8' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit8\").value = \"Run program\";document.getElementById(\"inputInUse8\").value = \"N\";<\/script>\n<\/p>\n<p>Continue on to lesson 3!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lesson contains extra practice exercises, some of which are challenging. If you get stuck, feel free to skip to the next problem or lesson and come back later. You can always use the My Progress page to review all &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/2x-extra-practice\/\">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-2985","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/2985"}],"collection":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/comments?post=2985"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/2985\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=2985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}