{"id":1804,"date":"2011-05-25T20:13:51","date_gmt":"2011-05-25T20:13:51","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/?page_id=1804"},"modified":"2011-05-25T20:13:51","modified_gmt":"2011-05-25T20:13:51","slug":"15b-python-pushups","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/15b-python-pushups\/","title":{"rendered":"15B: Python Pushups"},"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>Exercises 15A, 15B, and 15C can be completed in any order.<\/em><\/p>\n<p>In this lesson, we give several medium-size exercises combining tools learned in earlier lessons.<\/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'>Forty Below In The Winter<\/span><\/div>In this exercise, you will create a temperature converter which will convert Fahrenheit values to Celsius and vice-versa. You will need the following two formulas which relate the temperature <em>f<\/em> in Fahrenheit to the temperature <em>c<\/em> in Celsius: <\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7Bf+%3D+c+%5Ctimes+%5Cfrac%7B9%7D%7B5%7D+%2B+32%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='\\displaystyle{f = c \\times \\frac{9}{5} + 32}' title='\\displaystyle{f = c \\times \\frac{9}{5} + 32}' class='latex' \/><\/p>\n<p> <\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7Bc+%3D+%28f+-32%29%5Ctimes%5Cfrac%7B5%7D%7B9%7D.%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='\\displaystyle{c = (f -32)\\times\\frac{5}{9}.}' title='\\displaystyle{c = (f -32)\\times\\frac{5}{9}.}' class='latex' \/><\/p>\n<p> The input will be a string consisting of a floating-point number followed immediately by the letter F or C, such as \"<code>13.2C<\/code>\". You should convert to the other temperature scale and print the converted value in the same format. For example, if the input is \"<code>8F<\/code>\" then the output should be (approximately) \"<code>-13.333C<\/code>\", and if the input is \"<code>12.5C<\/code>\" then the output should be \"<code>54.5F<\/code>\".<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'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput0\">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='submit0' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch0\" value=\"Input Switch\" onclick=\"pbInputSwitch(0,'N')\" ><\/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><select id='pbSelect0' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(0,'15b.degrees')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(0);\" >Help<\/option>\n<\/select><\/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=\"db492e874819be6e9557128166a02f25\"\/>\n<div id='pbresults0' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(0);});pbInputSwitch(0,\"N\");<\/script>\n<\/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:\/\/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'>Credit Check<\/span><\/div>You have been hired by MeisterCard to write a function which checks if a given credit card number is valid. Your function <code>check(S)<\/code> should take a string <code>S<\/code> as input. First, if the string does not follow the format <code>\"#### #### #### ####\"<\/code> where each\u00a0<code>#<\/code> is a digit, then it should return <code>False<\/code>. Then, if the sum of the digits is divisible by 10 (a \"checksum\" method), then the procedure should return <code>True<\/code>, else it should return <code>False<\/code>. For example, if <code>S<\/code> is the string\u00a0\"<code>9384 3495 3297 0123<\/code>\"\u00a0then although the format is correct, the digit sum is 72 so you should return <code>False<\/code>. <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'>\ndef check(S):\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput1\">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<input type='hidden' id='defaultCode1' value='def check(S):\\n'><\/input>\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,'Y')\" ><\/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,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect1' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(1,'15b.creditcard')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(1,descape($('#defaultCode1').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(1);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"942f1fa3590166bc5a3c693df1040a38\"\/>\n<div id='pbresults1' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(1);});pbInputSwitch(1,\"Y\");<\/script>\n<\/p>\n<p>In the next exercise, use the methods\u00a0<code>string.split()<\/code>, which removes the spaces from a word and returns a list of the words it contains, and\u00a0<code>string.lower()<\/code>\u00a0which converts a string to lower case. For example,<\/p>\n<ul>\n<li><code>\"Split these words!\".split()<\/code> returns the list\u00a0<code>[\"Split\", \"these\", \"words!\"]<\/code><\/li>\n<li><code>\"LOWERCase\".lower()<\/code> returns\u00a0<code>\"lowercase\"<\/code><\/li>\n<\/ul>\n<p>Note: <code>split()<\/code> can accept further options to split in other ways; see the <a href=\"http:\/\/docs.python.org\/py3k\/library\/stdtypes.html#str.split\">documentation<\/a>.<\/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:\/\/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'>Poetic Analysis<\/span><\/div>A writer is working on their newest poem, <em>Turing and the Machines<\/em>. They have hired you to determine the word which appears the most times. You can access the lines of the poem by calling <code>input()<\/code> repeatedly, and the last line contains the three characters\u00a0<code>###<\/code>. All lines consist of words separated by single spaces; there are no digits or punctuation. Convert all the words to lower-case, and <strong>print the word that occurs the most times<\/strong> (we guarantee there will not be a tie). For example, if the input is <\/p>\n<pre>Here is a line like sparkling wine<br\/>Line up fast or be the last<br\/>###<\/pre> Then the output should be <\/p>\n<pre>line<\/pre> since it appears twice and no other word appears twice.  <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'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput2\">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='submit2' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch2\" value=\"Input Switch\" onclick=\"pbInputSwitch(2,'N')\" ><\/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,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect2' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(2,'15b.poem')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(2);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"dda6e122a655d4148248c53ec78a8492\"\/>\n<div id='pbresults2' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(2);});pbInputSwitch(2,\"N\");<\/script>\n<\/p>\n<p>Suppose you have\u00a0<em>n<\/em>\u00a0flavours of ice cream, and want to make a sundae using exactly\u00a0<em>k<\/em>\u00a0of those flavours. How many different flavour combinations are possible? For example, if\u00a0<em>n<\/em>=4 and\u00a0<em>k<\/em>=2, there are 6 possibilities:<\/p>\n<p style=\"text-align: center\">(1) A and B, (2) A and C, (3) A and D, (4) B and C, (5) B and D, (6) C and D.<\/p>\n<p>(for example here the flavours are\u00a0<strong>A<\/strong>pricot,\u00a0<strong>B<\/strong>lueberry,\u00a0<strong>C<\/strong>hocolate, and\u00a0<strong>D<\/strong>ate).<\/p>\n<p>Similarly, you could be choosing 2 people out of 4 (<strong>A<\/strong>l,\u00a0<strong>B<\/strong>ob,\u00a0<strong>C<\/strong>arol,\u00a0<strong>D<\/strong>i) to make a committee. How many ways can the committee be built? The answer would still be 6. The next problem is about computing this number.<\/p>\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'>Be Choosy<\/span><\/div>The number of combinations of <em>k<\/em> things out of a total of\u00a0<em>n<\/em> things is equal to <\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7B%5Cfrac%7Bn%7D%7Bk%7D%5Ctimes%5Cfrac%7Bn-1%7D%7Bk-1%7D%5Ctimes%5Ccdots%5Ctimes%5Cfrac%7Bn-k%2B2%7D%7B2%7D%5Ctimes%5Cfrac%7Bn-k%2B1%7D%7B1%7D%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{\\frac{n}{k}\\times\\frac{n-1}{k-1}\\times\\cdots\\times\\frac{n-k+2}{2}\\times\\frac{n-k+1}{1}}' title='\\displaystyle{\\frac{n}{k}\\times\\frac{n-1}{k-1}\\times\\cdots\\times\\frac{n-k+2}{2}\\times\\frac{n-k+1}{1}}' class='latex' \/><\/p>\n<p> Write a function <code>choose(n, k)<\/code> which takes two integers <code>n<\/code> and <code>k<\/code>; we guarantee\u00a0<em>n<\/em>&gt;<em>k<\/em>&gt;0. The function should return the value given in the formula above.<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# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput3\">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='submit3' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch3\" value=\"Input Switch\" onclick=\"pbInputSwitch(3,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect3' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(3,'15b.choose')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(3);\" >Help<\/option>\n<\/select><\/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=\"75807b6dca3fbcc436440f9ba1e2a9cf\"\/>\n<div id='pbresults3' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(3);});pbInputSwitch(3,\"Y\");<\/script>\n<\/p>\n<p><span style=\"text-align: left\">In mathematics, the number you computed in the previous exercise is usually written<\/span><\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7B%5Cbinom%7Bn%7D%7Bk%7D%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{\\binom{n}{k}}' title='\\displaystyle{\\binom{n}{k}}' class='latex' \/><\/p>\n<p style=\"text-align: left\">and called \"<em>n<\/em> choose <em>k<\/em>.\" There are many interesting facts about these values such as<\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7B%5Cbinom%7Bn%7D%7Bk%7D+%3D+%5Cbinom%7Bn%7D%7Bn-k%7D%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{\\binom{n}{k} = \\binom{n}{n-k}}' title='\\displaystyle{\\binom{n}{k} = \\binom{n}{n-k}}' class='latex' \/> and <img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7B%5Cbinom%7Bn%7D%7Bk%7D+%3D+%5Cbinom%7Bn-1%7D%7Bk%7D+%2B+%5Cbinom%7Bn-1%7D%7Bk-1%7D%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{\\binom{n}{k} = \\binom{n-1}{k} + \\binom{n-1}{k-1}}' title='\\displaystyle{\\binom{n}{k} = \\binom{n-1}{k} + \\binom{n-1}{k-1}}' class='latex' \/><\/p>\n<p style=\"text-align: left\">This is the end of our exercise session; you can move on to another lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exercises 15A, 15B, and 15C can be completed in any order. In this lesson, we give several medium-size exercises combining tools learned in earlier lessons. Coding Exercise: Forty Below In The WinterIn this exercise, you will create a temperature converter &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/15b-python-pushups\/\">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-1804","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/1804"}],"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=1804"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/1804\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=1804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}