{"id":56,"date":"2013-01-14T21:33:19","date_gmt":"2013-01-15T05:33:19","guid":{"rendered":"http:\/\/willclausen.com\/?p=56"},"modified":"2013-01-14T21:33:19","modified_gmt":"2013-01-15T05:33:19","slug":"solution-2013-equations-jan-1-2013","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=56","title":{"rendered":"Solution: 2013 Equations, Jan. 1, 2013"},"content":{"rendered":"<p>Here&#8217;s my solution to the problem for Jan. 1, 2013. The solution is written in Python.<\/p>\n<p>The <a title=\"Happy New Year Problem\" href=\"http:\/\/programmingpraxis.com\/2013\/01\/01\/happy-new-year\/\" target=\"_blank\">task<\/a>:<\/p>\n<p style=\"padding-left: 30px;\">As we begin the new year, we note that 109-8*7+654*3-2\/1 = 2013. There are three other combinations of the numbers 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, in order, combined with the five operators NIL, +, -, * and \/ that also evaluate to 2013.<\/p>\n<p style=\"padding-left: 30px;\">Your task is to write a program that finds all four expressions that evaluate to 2013.<\/p>\n<p>My solution:<\/p>\n<pre class=\"brush: python; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n# This function generates equations based on the positions of the\r\n# operations in a list. After producing an equation, it sends the\r\n# equation to another function to be evaluated. It would be faster\r\n# if I only generated and checked solutions that didn't start with\r\n# '+', '*', or '\/' (as solutions of this sort are not valid in my\r\n# scheme) but this presumably doesn't add a significant amount of\r\n# computation time, so it's unnecessary to worry about.\r\ndef generateEqsAndSolve():\r\n\tOps = &#x5B;'-', '', '+', '*', '\/']\r\n\tfor x in (itertools.product(Ops, repeat=10)):\r\n\t\tprocessEq(list(x))\r\n\r\n# Create an equation from the ordered list of operations.\r\n# Then solve for the value of the equation.\r\ndef processEq(L):\r\n\tif L&#x5B;0] == '*' or L&#x5B;0] == '\/' or L&#x5B;0] == '+':\r\n\t\treturn\r\n\r\n\t# Get the equation as a string.\r\n\tequation = ''\r\n\tfor x in range(len(L)):\r\n\t\tif (L&#x5B;x] == '-' or L&#x5B;x] == '+') and x != 0:\r\n\t\t\t\tequation += ' '\r\n\t\tequation += L&#x5B;x]\r\n\t\tequation += str(10-x)\r\n\r\n\tequationLen = len(equation)\r\n\r\n\t# Evaluate the equation...p\r\n\tfinalVal = 0.0\r\n\tnum = 0.0\r\n\ti = 0\r\n\twhile i &lt; equationLen:\r\n\t\teqPart = ''\r\n\t\tif equation&#x5B;i] == '-':\r\n\t\t\ti += 1\r\n\t\t\twhile equation&#x5B;i] != ' ' and i &lt; equationLen:\r\n\t\t\t\teqPart += equation&#x5B;i]\r\n\t\t\t\ti += 1\r\n\t\t\t\tif not i &lt; equationLen:\r\n\t\t\t\t\tbreak\r\n\r\n\t\t\tnum = processNum(eqPart)\r\n\t\t\tnum *= -1\r\n\t\telif equation&#x5B;i] == '+' or equation&#x5B;i] == '':\r\n\t\t\ti += 1\r\n\t\t\twhile equation&#x5B;i] != ' ' and i &lt; equationLen:\r\n\t\t\t\teqPart += equation&#x5B;i]\r\n\t\t\t\ti += 1\r\n\t\t\t\tif not i &lt; equationLen:\r\n\t\t\t\t\tbreak\r\n\r\n\t\t\tnum = processNum(eqPart)\r\n\r\n\t\tfinalVal += num\r\n\t\ti += 1\r\n\r\n\t# If the equation has the value of 2013, print it!\r\n\tif math.fabs(finalVal - 2013) &lt; .004:\r\n\t\tprint equation\r\n\r\n\treturn\r\n\r\n# Helper function that takes string representing\r\n# part of an eqaution produced in processEq and\r\n# returns the value of the part of the equation\r\ndef processNum(eqPart):\r\n\tpartLen = len(eqPart)\r\n\ti = 0\r\n\tcount = 0\r\n\tstrNum = ''\r\n\tnum = 0\r\n\tstoredNum = 1.0\r\n\top = ''\r\n\twhile i &lt; partLen:\r\n\t\tif not eqPart&#x5B;i].isdigit():\r\n\t\t\tbreak\r\n\t\tstrNum += eqPart&#x5B;i]\r\n\t\ti += 1\r\n\r\n\tstoredNum = float(strNum)\r\n\tstrNum = ''\r\n\r\n\twhile i &lt; partLen:\r\n\t\top = eqPart&#x5B;i]\r\n\t\ti += 1\r\n\r\n\t\twhile eqPart&#x5B;i].isdigit() and i &lt; partLen:\r\n\t\t\tstrNum += eqPart&#x5B;i]\r\n\t\t\ti += 1\r\n\t\t\tif not i &lt; partLen:\r\n\t\t\t\tbreak\r\n\t\tnum = int(strNum)\r\n\r\n\t\tif op == '*':\r\n\t\t\tstoredNum *= num\r\n\t\telse:\r\n\t\t\tstoredNum \/= num\r\n\r\n\t\tstrNum = ''\r\n\r\n\treturn storedNum\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s my solution to the problem for Jan. 1, 2013. The solution is written in Python. The task: As we begin the new year, we note that 109-8*7+654*3-2\/1 = 2013. There are three other combinations of the numbers 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, in order, combined with the five operators [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[9,16,11,12],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-programming-praxis","tag-january2013","tag-new-year","tag-programming-praxis-2","tag-solution"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":2,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":58,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions\/58"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}