{"id":1006,"date":"2010-12-02T16:44:24","date_gmt":"2010-12-02T16:44:24","guid":{"rendered":"http:\/\/pchero21.com\/?p=1006"},"modified":"2010-12-02T16:44:24","modified_gmt":"2010-12-02T16:44:24","slug":"lec-7-mit-6-00-introduction-to-computer-science-and-programming-fall-2008","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=1006","title":{"rendered":"Lec 7 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008"},"content":{"rendered":"<p><\/p>\n<h1>\n        <span class=\"\" id=\"parent-fieldname-title\"><br \/>\n            7: Lists and Mutability, Dictionaries, Introduction to Efficiency<br \/>\n        <\/span><br \/>\n    <\/h1>\n<p><object width=\"480\" height=\"385\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/tuRYbBvOMRo?fs=1&amp;hl=ko_KR\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/tuRYbBvOMRo?fs=1&amp;hl=ko_KR\" type=\"application\/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"480\" height=\"385\"><\/embed><\/object><\/p>\n<blockquote><p>import math<\/p>\n<p>#Get base<br \/>inputOK = False<br \/>while not inputOK:<br \/>&nbsp; &nbsp; base = input(&#8216;Enter base: &#8216;)<br \/>&nbsp; &nbsp; if type(base) == type(1.0) : inputOK = True<br \/>&nbsp; &nbsp; else: print(&#8216;Error. Base must be floating point number.&#8217;)<\/p>\n<p>#Get Height<br \/>inputOK = False<br \/>while not inputOK:<br \/>&nbsp; &nbsp; height = input(&#8216;Enter height: &#8216;)<br \/>&nbsp; &nbsp; if type(height) == type(1.0) : inputOK = True<br \/>&nbsp; &nbsp; else: print(&#8216;Error. Height must be floating point number.&#8217;)<\/p>\n<p>hyp = math.sqrt (base * base + height * height)<\/p>\n<p>print &#8216;Base: &#8216; + str(base) + &#8216;, height: &#8216; + str(height) + &#8216;, hyp: &#8216; + str(hyp)<\/p>\n<p>def getFloat(requestMsg, errorMsg):<br \/>&nbsp; &nbsp; inputOK = False<br \/>&nbsp; &nbsp; while not inputOK:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; val = input(requestMsg)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; if type(val) == type(1.0) : inputOK = True<br \/>&nbsp; &nbsp; &nbsp; &nbsp; else: print(errorMsg)<br \/>&nbsp; &nbsp; return val<\/p>\n<p>base = getFloat(&#8216;Enter base: &#8216;, &#8216;Error: base must be a float&#8217;)<br \/>height = getFloat(&#8216;Enter height:&#8217;, &#8216;Error height must be a float&#8217;)<\/p>\n<p>hyp = math.sqrt(base * base + height * height)<\/p>\n<p>print &#8216;Base: &#8216; + str(base) + &#8216;, height: &#8216; + str(height) + &#8216;, hyp: &#8216; + str(hyp)<\/p>\n<p>def exp1(a, b):<br \/>&nbsp; &nbsp; ans = 1<br \/>&nbsp; &nbsp; while (b &gt; 0):<br \/>&nbsp; &nbsp; &nbsp; &nbsp; ans *= a<br \/>&nbsp; &nbsp; &nbsp; &nbsp; b -= 1<br \/>&nbsp; &nbsp; return ans<\/p>\n<p>def exp2(a, b):<br \/>&nbsp; &nbsp; if b == 1:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; return a<br \/>&nbsp; &nbsp; else:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; return a * exp2(a, b &#8211; 1)<\/p>\n<p>def exp3(a, b):<br \/>&nbsp; &nbsp; if b == 1:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; return a<br \/>&nbsp; &nbsp; if (b % 2) == b:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; return exp3(a * a, b \/ 2)<br \/>&nbsp; &nbsp; else:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; return a * exp3(a, b &#8211; 1)<\/p>\n<p>def g(n):<br \/>&nbsp; &nbsp; x = 0<br \/>&nbsp; &nbsp; for i in range(n):<br \/>&nbsp; &nbsp; &nbsp; &nbsp; for j in range(n):<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += 1<br \/>&nbsp; &nbsp; return x<br \/>def Towers(size, fromStack, toStack, spareStack):<br \/>&nbsp; &nbsp; if size == 1:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; print &#8216;Move disk from &#8216;, fromStack, &#8216;to &#8216;, toStack<br \/>&nbsp; &nbsp; else:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; Towers(size &#8211; 1, fromStack, spareStack, toStack)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; Towers(1, FromStack, toStack, spareStack)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; Towers(size &#8211; 1, spareStack, toStack, fromStack)<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>7: Lists and Mutability, Dictionaries, Introduction to Efficiency import math #Get baseinputOK = Falsewhile not inputOK:&nbsp; &nbsp; base = input(&#8216;Enter base: &#8216;)&nbsp; &nbsp; if type(base) == type(1.0) : inputOK = True&nbsp; &nbsp; else: print(&#8216;Error. Base must be floating point number.&#8217;) &hellip; <a href=\"http:\/\/pchero21.com\/?p=1006\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[235,259,293],"_links":{"self":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/1006"}],"collection":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1006"}],"version-history":[{"count":0,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}