<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dreamshire</title>
	<atom:link href="http://blog.dreamshire.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dreamshire.com</link>
	<description>Showing what&#039;s behind the curtain</description>
	<lastBuildDate>Sat, 18 May 2013 07:22:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Project Euler Problem 90 Solution</title>
		<link>http://blog.dreamshire.com/2013/05/16/project-euler-problem-90-solution/</link>
		<comments>http://blog.dreamshire.com/2013/05/16/project-euler-problem-90-solution/#comments</comments>
		<pubDate>Fri, 17 May 2013 01:27:56 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 90-99]]></category>
		<category><![CDATA[enumerate]]></category>
		<category><![CDATA[intertools]]></category>
		<category><![CDATA[list comprehensions]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2509</guid>
		<description><![CDATA[Cube digit pairs]]></description>
				<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers.</p>
<p>For example, the square number 64 could be formed:</p>
<div style="text-align:center;">
<img src="http://www.projecteuler.net/project/images/p_090.gif" alt="" />
</div>
<p>In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81.</p>
<p>For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other cube.</p>
<p>However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09.</p>
<p>In determining a distinct arrangement we are interested in the digits on each cube, not the order.</p>
<p style="margin-left:50px;">{1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5}<br />
{1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9}</p>
<p>But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2-digit numbers.</p>
<p>How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?</p>
<h4><u>Analysis</u></h4>
<p>Let&#8217;s reword this problem a bit:<br />
Take two cubes and inscribe each side of each cube with six numbers from the set {0-9} in such a way that when they are placed side-by-side they show a perfect square from the set {01, 04, 09, 16, 25, 36, 49, 64, 81}.</p>
<p>As an example, one possibility could be {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other.  The cubes can be positioned in any way that forms the perfect square.  Also, a cube with the combination {9, 8, 7, 0, 5, 6} is the same as (distinct from) another cube with the same numbers but in a different order {0, 5, 6, 7, 8, 9}.</p>
<p>To make the problem a bit more challenging, let&#8217;s say that you can turn a cube upside down so the 6 could serve as a 9.  This makes the numbers to inscribe on the cube as {0, 1, 2, 3, 4, 5, 6, 7, 8, 6}.</p>
<p>How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?</p>
<p>To solve this problem we are going to use:</p>
<ul>
<em>combinations</em> function from intertools<br />
<em>enumerate</em> function<br />
<em>all</em> function<br />
list comprehensions with a conditional
</ul>
<p>Step-by-step analysis:<br />
Firstly, we create a list of desired outcomes that include (0,1), (0,4), (0,6) (the 6 can be flipped for a 9), (1,6), (2,5), (3,6), (4,6) (the 6 can be flipped for a 9), and (8,1).  Note that (6,4) is missing because the cubes can be set side-by-side in either way so (4,6) is the same as (6,4).</p>
<p>Secondly, we build a list of tuples (which didn&#8217;t need to be tuples, but they looked cleaner in source) for all the possibilities of 10 numbers chosen 6 at a time.  This is 10c6 or 210 possible combinations.</p>
<p>As this may be confusing, here is a simplified version to look at using 4 numbers chosen 2 at a time (4c2 or 6 possible combinations):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">itertools</span> <span style="color: #ff7700;font-weight:bold;">import</span> combinations
<span style="color: #66cc66;">&gt;&gt;&gt;</span> cube <span style="color: #66cc66;">=</span> <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>combinations<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> cube
<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span></pre></td></tr></table></div>

<p>Notice that the combination function produces a list of tuples for each iteration.</p>
<p>We&#8217;ll hold off on the valid function for a moment and focus on the double list compression.  </p>
<p>The <em>enumerate</em> function is a cleaner and more efficient way of replacing:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>my_list<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> n<span style="color: #66cc66;">,</span> my_list<span style="color: black;">&#91;</span>n<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>with:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> n<span style="color: #66cc66;">,</span> item <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>my_list<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> n<span style="color: #66cc66;">,</span> item</pre></td></tr></table></div>

<p>So, <em>enumerate</em> returns both the index and the element.  This is useful for comparing every item in a list to every other item in the list.  Here&#8217;s an example continuing from above:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> i<span style="color: #66cc66;">,</span>c1 <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>cube<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'first loop'</span><span style="color: #66cc66;">,</span>i<span style="color: #66cc66;">,</span>c1
    <span style="color: #ff7700;font-weight:bold;">for</span> c2 <span style="color: #ff7700;font-weight:bold;">in</span> cube<span style="color: black;">&#91;</span>:i<span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;  second loop&quot;</span><span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span>c2</pre></td></tr></table></div>

<p>Output:
<pre>
first loop 0 (0, 1)
first loop 1 (0, 2)
  second loop 1 (0, 1)
first loop 2 (0, 3)
  second loop 2 (0, 1)
  second loop 2 (0, 2)
first loop 3 (1, 2)
  second loop 3 (0, 1)
  second loop 3 (0, 2)
  second loop 3 (0, 3)
first loop 4 (1, 3)
  second loop 4 (0, 1)
  second loop 4 (0, 2)
  second loop 4 (0, 3)
  second loop 4 (1, 2)
first loop 5 (2, 3)
  second loop 5 (0, 1)
  second loop 5 (0, 2)
  second loop 5 (0, 3)
  second loop 5 (1, 2)
  second loop 5 (1, 3)
</pre>
<p>Thirdly, the valid function simply returns True for those cases that a square can be made from a perspective set of two cubes in either direction.  The <em>all</em> function will return <strong>True</strong> only if all the generated list items are <strong>True</strong>.</p>
<p>Lastly, all that&#8217;s left to do is count valid combinations.  Duplicates are ignored inherently by only checking a possible cube against all other combinations that come before it.  That&#8217;s why we use <em>cube</em>[:<em>i</em>] in the second loop and not just <em>cube</em>.  Which is also why <em>enumerate</em> is so nice, because it gives us the index, <em>i</em>.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">itertools</span> <span style="color: #ff7700;font-weight:bold;">import</span> combinations
&nbsp;
squares <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
cube <span style="color: #66cc66;">=</span> <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>combinations<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">7</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> valid<span style="color: black;">&#40;</span>c1<span style="color: #66cc66;">,</span> c2<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">all</span><span style="color: black;">&#40;</span>x <span style="color: #ff7700;font-weight:bold;">in</span> c1 <span style="color: #ff7700;font-weight:bold;">and</span> y <span style="color: #ff7700;font-weight:bold;">in</span> c2 <span style="color: #ff7700;font-weight:bold;">or</span> x <span style="color: #ff7700;font-weight:bold;">in</span> c2 <span style="color: #ff7700;font-weight:bold;">and</span> y <span style="color: #ff7700;font-weight:bold;">in</span> c1 <span style="color: #ff7700;font-weight:bold;">for</span> x<span style="color: #66cc66;">,</span> y <span style="color: #ff7700;font-weight:bold;">in</span> squares<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE90 =&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">for</span> i<span style="color: #66cc66;">,</span>c1 <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>cube<span style="color: black;">&#41;</span>
                                    <span style="color: #ff7700;font-weight:bold;">for</span> c2 <span style="color: #ff7700;font-weight:bold;">in</span> cube<span style="color: black;">&#91;</span>:i<span style="color: black;">&#93;</span>
                                        <span style="color: #ff7700;font-weight:bold;">if</span> valid<span style="color: black;">&#40;</span>c1<span style="color: #66cc66;">,</span> c2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h4><u>Comments</u></h4>
<p>As an exercise, one may ask how many combinations are there if the 6/9 reversal was <u>NOT</u> allowed,   but instead, just the digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}?</p>
<p>By changing these two lines:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">squares <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
dice <span style="color: #66cc66;">=</span> <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>combinations<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">7</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">8</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">9</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>the answer would be: 205</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/05/16/project-euler-problem-90-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 1682. Deli Deli (DELI)</title>
		<link>http://blog.dreamshire.com/2013/02/21/spoj-problem-1682-deli-deli-deli/</link>
		<comments>http://blog.dreamshire.com/2013/02/21/spoj-problem-1682-deli-deli-deli/#comments</comments>
		<pubDate>Thu, 21 Feb 2013 22:08:31 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Tutorial Problems]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[SPOJ]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2545</guid>
		<description><![CDATA[Make plurals of words from the given input]]></description>
				<content:encoded><![CDATA[<p><strong>Description</strong><br />
<a href="http://www.spoj.com/problems/DELI/" title="Problem code: DELI" target="_blank">1682. Deli Deli</a><br />
Mrs. Deli is running the delicatessen store &#8220;Deli Deli&#8221;.  Last year Mrs. Deli has decided to expand her business and build up an online store. She has hired a programmer who has implemented the  online store.</p>
<p>Recently some of her new online customers complained about the electronic bills. The programmer had forgotten to use the plural form in case that an item is purchased multiple times. Unfortunately the programmer of Mrs. Deli is on holiday and now it is your task to implement this feature for Mrs. Deli. Here is a description how to make the plural form:</p>
<ol>
<li>If the word is in the list of irregular words replace it with the given plural. </li>
<li>Else if the word ends in a consonant followed by &#8220;y&#8221;, replace &#8220;y&#8221; with &#8220;ies&#8221;.</li>
<li>Else if the word ends in &#8220;o&#8221;, &#8220;s&#8221;, &#8220;ch&#8221;, &#8220;sh&#8221; or &#8220;x&#8221;, append &#8220;es&#8221; to the word.</li>
<li>Else append &#8220;s&#8221; to the word.</li>
</ol>
<p><strong>Input Specification</strong><br />
The first line of the input consists of two integers <b>L</b> and <b>N</b> (<i>0 &#8804; L &#8804; 20</i>, <i>1 &#8804; N &#8804; 100</i>).  The following <b>L</b> lines contain the description of the irregular words and their plural form.  Each line consists of two words separated by a space character, where the first word is the singular, the second word the plural form of some irregular word.  After the list of irregular words, the following <b>N</b> lines contain one word each, which you have to make plural. You may assume that each word consists of at most 20 lowercase letters from the English alphabet (&#8216;a&#8217; to &#8216;z&#8217;).</p>
<p><strong>Output Specification</strong><br />
Print <b>N</b> lines of output, where the <b>i<sup>th</sup></b> line is the plural form of the <b>i<sup>th</sup></b> input word.</p>
<p><strong>Ancillary Files</strong><br />
Input file: <a href="/data/SPOJ/DELI/deli.in" title="DELI input file: deli.in" target="_blank">deli.in</a><br />
Output file: <a href="/data/SPOJ/DELI/deli.out" title="DELI output file: deli.out" target="_blank">deli.out</a></p>
<p><strong>Solution</strong> (File: <a href="/data/SPOJ/DELI/1682.py" title="Python source for 1682. Deli Deli" target="_blank">1682.py</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
l<span style="color: #66cc66;">,</span> n <span style="color: #66cc66;">=</span> <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: #66cc66;">,</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
d <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  a <span style="color: #66cc66;">=</span> <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #ff7700;font-weight:bold;">in</span> d:
    <span style="color: #ff7700;font-weight:bold;">print</span> d<span style="color: black;">&#91;</span>a<span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'[^aeiou]y$'</span><span style="color: #66cc66;">,</span>a<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a<span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">'ies'</span>
  <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(o|s|ch|sh|x)$'</span><span style="color: #66cc66;">,</span>a<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+<span style="color: #483d8b;">'es'</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+<span style="color: #483d8b;">'s'</span></pre></td></tr></table></div>

<p><strong>Comments</strong><br />
Run from cmd prompt as: python 1682.py < deli.in > my_deli.out<br />
and compare my_ deli.out to deli.out.  Submit the program to the SPOJ server at the link provided under the description.<br />
The test cases are qualified to match the input specification.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/02/21/spoj-problem-1682-deli-deli-deli/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 4408. Build a Fence (FENCE1)</title>
		<link>http://blog.dreamshire.com/2013/02/21/spoj-problem-4408-build-a-fence-fence1/</link>
		<comments>http://blog.dreamshire.com/2013/02/21/spoj-problem-4408-build-a-fence-fence1/#comments</comments>
		<pubDate>Thu, 21 Feb 2013 17:43:50 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Classical Problems]]></category>
		<category><![CDATA[SPOJ]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2544</guid>
		<description><![CDATA[Build a fence to maximize an area enclosed between the fence and a fixed wall]]></description>
				<content:encoded><![CDATA[<p><strong>Description</strong><br />
<a href="http://www.spoj.com/problems/FENCE1/" title="Problem code: FENCE1" target="_blank">4408. Build a Fence</a><br />
There is a wall in your backyard. It is so long that you can’t see its endpoints. You want to build a fence of length L such that the area enclosed between the wall and the fence is maximized. The fence can be of arbitrary shape, but only its two endpoints may touch the wall.</p>
<p><strong>Input Specification</strong><br />
The input consists of several test cases.</p>
<p>For every test case, there is only one integer L (1<=L<=100), indicating the length of the fence.</p>
<p>The input ends with L=0.</p>
<p><strong>Output Specification</strong><br />
For each test case, output one line containing the largest area. Your answer should be rounded to 2 digits after the decimal point.</p>
<p><strong>Sample Input</strong><br />
1<br />
7<br />
0</p>
<p><strong>Sample Output</strong><br />
0.16<br />
7.80</p>
<p><strong>Solution</strong> (File: <a href="/data/SPOJ/FENCE1/4408.py" title="Python source for 4408. Adding Reversed Numbers" target="_blank">4408.py</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> pi
&nbsp;
c <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span>/<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>*pi<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  L <span style="color: #66cc66;">=</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> L<span style="color: #66cc66;">==</span><span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'%.2f'</span> % <span style="color: #008000;">round</span><span style="color: black;">&#40;</span>L*L*c<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Comments</strong><br />
The test cases are qualified to match the input specification.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/02/21/spoj-problem-4408-build-a-fence-fence1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 42. Adding Reversed Numbers (ADDREV)</title>
		<link>http://blog.dreamshire.com/2013/02/21/spoj-problem-42-adding-reversed-numbers-addrev/</link>
		<comments>http://blog.dreamshire.com/2013/02/21/spoj-problem-42-adding-reversed-numbers-addrev/#comments</comments>
		<pubDate>Thu, 21 Feb 2013 07:10:01 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Classical Problems]]></category>
		<category><![CDATA[SPOJ]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2543</guid>
		<description><![CDATA[Add two reversed numbers and output their reversed sum]]></description>
				<content:encoded><![CDATA[<p><strong>Description</strong><br />
<a href="http://www.spoj.com/problems/ADDREV/" title="Problem code: ADDREV" target="_blank">42. Adding Reversed Numbers</a><br />
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.</p>
<p>Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.</p>
<p>ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).</p>
<p><strong>Input Specification</strong><br />
The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.</p>
<p><strong>Output Specification</strong><br />
For each case, print exactly one line containing only one integer &#8211; the reversed sum of two reversed numbers. Omit any leading zeros in the output.</p>
<p><strong>Sample Input</strong><br />
3<br />
24 1<br />
4358 754<br />
305 794</p>
<p><strong>Sample Output</strong><br />
34<br />
1998<br />
1</p>
<p><strong>Solution</strong> (File: <a href="/data/SPOJ/ADDREV/42.py" title="Python source for 42. Adding Reversed Numbers" target="_blank">42.py</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> r_num<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
  i1<span style="color: #66cc66;">,</span> i2 <span style="color: #66cc66;">=</span> <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: #66cc66;">,</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> r_num<span style="color: black;">&#40;</span>r_num<span style="color: black;">&#40;</span>i1<span style="color: black;">&#41;</span> + r_num<span style="color: black;">&#40;</span>i2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Comments</strong><br />
The test cases are qualified to match the input specification.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/02/21/spoj-problem-42-adding-reversed-numbers-addrev/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 11. Factorial (FCTRL)</title>
		<link>http://blog.dreamshire.com/2013/02/20/spoj-problem-11-factorial-fctrl/</link>
		<comments>http://blog.dreamshire.com/2013/02/20/spoj-problem-11-factorial-fctrl/#comments</comments>
		<pubDate>Wed, 20 Feb 2013 07:05:12 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Classical Problems]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[SPOJ]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2541</guid>
		<description><![CDATA[Find the number of trailing zeros for any factorial]]></description>
				<content:encoded><![CDATA[<p><strong>Description</strong><br />
<a href="http://www.spoj.com/problems/FCTRL/" title="Problem code: FCTRL" target="_blank">11. Factorial</a><br />
The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.</p>
<p>ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called &#8220;Travelling Salesman Problem&#8221; and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4&#8230;.N. The number is very high even for a relatively small N.</p>
<p>The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.</p>
<p>For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1 &lt; N2, then Z(N1) &le; Z(N2). It is because we can never &#8220;lose&#8221; any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.</p>
<p><strong>Input Specification</strong><br />
There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 &le; N &le; 1000000000.</p>
<p><strong>Output Specification</strong><br />
For every number N, output a single line containing the single non-negative integer Z(N).</p>
<p><strong>Ancillary Files</strong><br />
Input file: <a href="/data/SPOJ/FCTRL/fctrl.in" title="FCTRL input file: fctrl.in" target="_blank">fctrl.in</a><br />
Output file: <a href="/data/SPOJ/FCTRL/fctrl.out" title="FCTRL output file: fctrl.out" target="_blank">fctrl.out</a></p>
<p><strong>Solution</strong> (File: <a href="/data/SPOJ/FCTRL/11.py" title="Python source for 11. Factorial" target="_blank">11.py</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Z<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  c <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> n<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span>:
	n /<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">5</span>
	c +<span style="color: #66cc66;">=</span> n
  <span style="color: #ff7700;font-weight:bold;">return</span> c
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> Z<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Comments</strong><br />
Run from cmd prompt as: python 11.py < fctrl.in > my_fctrl.out<br />
and compare my_ fctrl.out to fctrl.out.  Submit the program to the SPOJ server at the link provided under the description.<br />
The test cases are qualified to match the input specification.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/02/20/spoj-problem-11-factorial-fctrl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 1681. Cylinder (CYLINDER)</title>
		<link>http://blog.dreamshire.com/2013/02/19/spoj-problem-1681-cylinder-cylinder/</link>
		<comments>http://blog.dreamshire.com/2013/02/19/spoj-problem-1681-cylinder-cylinder/#comments</comments>
		<pubDate>Tue, 19 Feb 2013 18:15:01 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Classical Problems]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[SPOJ]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2540</guid>
		<description><![CDATA[Find the largest volume of a cylinder cut and formed from a sheet of paper]]></description>
				<content:encoded><![CDATA[<p><strong>Description</strong><br />
<a href="http://www.spoj.com/problems/CYLINDER/" title="Problem code: CYLINDER" target="_blank">1681. Cylinder</a><br />
Using a sheet of paper and scissors, you can cut out two faces to form a cylinder in the following way: </p>
<ol>
<li>Cut the paper horizontally (parallel to the shorter side) to get two rectangular parts.</li>
<li>From the first part, cut out a circle of maximum radius. The circle will form the bottom of the cylinder.</li>
<li>Roll the second part up in such a way that it has a perimeter of equal length with the circle&#8217;s circumference, and attach one end of the roll to the circle.  Note that the roll may have some overlapping parts in order to get the required length of the perimeter.</li>
</ol>
<p>Given the dimensions of the sheet of paper, can you calculate the biggest possible volume of a cylinder which can be constructed using the procedure described above?</p>
<p><strong>Input Specification</strong><br />
The input consists of several test cases. Each test case consists of two numbers w and h (1 ≤ w ≤ h ≤ 100), which indicate the width and height of the sheet of paper.</p>
<p>The last test case is followed by a line containing two zeros.</p>
<p><strong>Output Specification</strong><br />
For each test case, print one line with the biggest possible volume of the cylinder. Round this number to 3 places after the decimal point.</p>
<p><strong>Ancillary Files</strong><br />
Input file: <a href="/data/SPOJ/CYLINDER/cylinder.in" title="CYLINDER input file: cylinder.in" target="_blank">cylinder.in</a><br />
Output file: <a href="/data/SPOJ/CYLINDER/cylinder.out" title="CYLINDER output file: cylinder.out" target="_blank">cylinder.out</a></p>
<p><strong>Solution</strong> (File: <a href="/data/SPOJ/CYLINDER/1681.py" title="Python source for 1681. CYLINDER" target="_blank">1681.py</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> pi
pi2 <span style="color: #66cc66;">=</span> pi*pi
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  w<span style="color: #66cc66;">,</span> h <span style="color: #66cc66;">=</span> <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: #66cc66;">,</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>h+w <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  res <span style="color: #66cc66;">=</span> h/<span style="color: #ff4500;">2</span>./<span style="color: black;">&#40;</span>pi+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">if</span> h<span style="color: #66cc66;">&lt;</span>w*<span style="color: black;">&#40;</span>pi+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">else</span> w/<span style="color: #ff4500;">2</span>.
  <span style="color: black;">res2</span> <span style="color: #66cc66;">=</span> pi * res*res * w
  res3 <span style="color: #66cc66;">=</span> w*w/<span style="color: #ff4500;">4</span>. * <span style="color: black;">&#40;</span>h*pi - w<span style="color: black;">&#41;</span> / pi2
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'%.3f'</span> % <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>res2<span style="color: #66cc66;">,</span>res3<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Comments</strong><br />
Run from cmd prompt as: python 1681.py < cylinder.in > my_cylinder.out<br />
and compare my_ cylinder.out to cylinder.out.  Submit the program to the SPOJ server at the link provided under the description.<br />
The test cases are qualified to match the input specification.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/02/19/spoj-problem-1681-cylinder-cylinder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 1680. Black and white painting (BLACK)</title>
		<link>http://blog.dreamshire.com/2013/02/19/spoj-problem-1680-black-and-white-painting-black/</link>
		<comments>http://blog.dreamshire.com/2013/02/19/spoj-problem-1680-black-and-white-painting-black/#comments</comments>
		<pubDate>Tue, 19 Feb 2013 09:29:14 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Tutorial Problems]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[SPOJ]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2539</guid>
		<description><![CDATA[How many 8 × 8 chess boards are embedded in a painting which consists solely of black and white squares, arranged in a checkered pattern]]></description>
				<content:encoded><![CDATA[<p><strong>Description</strong><br />
<a href="http://www.spoj.com/problems/BLACK/" title="Problem Code: BLACK" target="_blank">1680. Black and white painting</a><br />
You are visiting the Centre Pompidou which contains a lot of modern paintings. In particular you notice one painting which consists solely of black and white squares, arranged in rows and columns like in a chess board (no two adjacent squares have the same colour).</p>
<p>Since you are bored, you wonder how many 8 × 8 chess boards are embedded within this painting. The bottom right corner of a chess board must always be white.</p>
<p><strong>Input Specification</strong><br />
The input contains several test cases. Each test case consists of one line with three integers n, m and c. (8 ≤ n, m ≤ 40000), where n is the number of rows of the painting, and m is the number of columns of the painting. c is always 0 or 1, where 0 indicates that the bottom right corner of the painting is black, and 1 indicates that this corner is white.</p>
<p>The last test case is followed by a line containing three zeros.</p>
<p><strong>Output Specification</strong><br />
For each test case, print the number of chess boards embedded within the given painting.</p>
<p><strong>Ancillary Files</strong><br />
Input file: <a href="/data/SPOJ/BLACK/black.in" title="BLACK input file: black.in" target="_blank">black.in</a><br />
Output file: <a href="/data/SPOJ/BLACK/black.out" title="BLACK output file: black.out" target="_blank">black.out</a></p>
<p><strong>Solution</strong> (File: <a href="/data/SPOJ/BLACK/1680.py" title="Python source for 1680. BLACK" target="_blank">1680.py</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  n<span style="color: #66cc66;">,</span> m<span style="color: #66cc66;">,</span> c <span style="color: #66cc66;">=</span> <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: #66cc66;">,</span><span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>n+m+c <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>*<span style="color: black;">&#40;</span>m-<span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span> + c<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span></pre></td></tr></table></div>

<p><strong>Comments</strong><br />
Run from cmd prompt as: python 1680.py < black.in > my_black.out<br />
and compare my_black.out to black.out.  Submit the program to the SPOJ server at the link provided under the description.<br />
The test cases are qualified to match the input specification.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/02/19/spoj-problem-1680-black-and-white-painting-black/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Impossible Test Christmas Answers for iPhone/iPad</title>
		<link>http://blog.dreamshire.com/2013/01/16/impossible-test-christmas-answers-for-iphoneipad/</link>
		<comments>http://blog.dreamshire.com/2013/01/16/impossible-test-christmas-answers-for-iphoneipad/#comments</comments>
		<pubDate>Thu, 17 Jan 2013 02:02:04 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Game walkthroughs and solutions]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2520</guid>
		<description><![CDATA[Answers and techniques for solving the Impossible Test Christmas.  Revised for newest version 1/17/2013]]></description>
				<content:encoded><![CDATA[<p><img src="http://blog.dreamshire.com/wp-content/uploads/2013/01/itc.jpg" alt="itc" width="100" height="100" class="alignnone size-full wp-image-2521" /><br />
<a href="http://youtu.be/_UT2XOrzeSk" title="YouTube Link" target="_blank">YouTube video walkthrough.</a></p>
<h2>The Impossible Test Christmas Answers</h2>
<p>1. <b>Make it snow:</b> <em>Shake your device.</em></p>
<p>2. <b>Turn off the lights:</b> <em>Drag the word &#8220;off&#8221; onto the Christmas tree.</em></p>
<p>3. <b>Make the present burst open:</b> <em> Tap the snowballs on the left to shoot the moving present.</em></p>
<p>4. <b>Cool present! Maybe you should tap it?:</b> <em> Tap the laser gun and note the price $42.13.</em></p>
<p>5. <b>Tap Christmas day:</b> <em>The date directly down 3 rows is the number shown plus 21. Move left or right the appropriate number of dates to the 25th.</em></p>
<p>6. <b>Launch the sleigh:</b> <em> Drag the sleigh to the far left and release.</em></p>
<p>7. <b>Stuff the stocking without coal:</b> <em>Drag the word &#8220;Stuff&#8221; onto the stocking.</em></p>
<p>8. <b>Make a snow angel:</b> <em> Drag the word &#8220;snow&#8221; on top of the word &#8220;angel.&#8221; Make a circular motion in the middle of the screen to reveal a snow angel until the red button appears on the right.  Tap the button.</em></p>
<p>9. <b>Stop the stars from flashing:</b> <em>Drag the stars away to reveal the red off button and tap it.</em></p>
<p>10. <b>Find the North Pole:</b> <em> Turn the device upside down.</em></p>
<p>11. <b>Scratch the gift card:</b> <em>Use your finger to scratch off the code.  Tap the card when instructed.</em></p>
<p>12. <b>Catch all the icicles when they fall:</b> <em>Tap the 5 icicles as they fall before they hit the ground.</em></p>
<p>13. <b>Hang the wreath:</b> <em>Find the nail on the left side of the screen and drag it onto the door. Hang the wreath upon it.</em></p>
<p>14. <b>Touch purple, green, red, then yellow:</b> <em>It&#8217;s easier to pick one window and tap the elves&#8217; hat for purple, green, red as they pop in.  Then tap the word &#8220;yellow&#8221;</em></p>
<p>15. <b>Put the primary color presents in the chimney:</b> <em>drag the red, blue and yellow presents onto the word &#8220;chimney&#8221;</em></p>
<p>16. <b>Warm up the hot chocolate:</b> <em>Drag the cup to the left and tap the Heat button.</em></p>
<p>17. <b>How many points are on the poinsettia?</b> <em>25.</em></p>
<p>18. <b>Tap all the left/right mittens:</b> <em>Left mittens have the thumb pointing to the right unless upside down.  Right mittens have the thumb pointing to the left unless upside down.</em></p>
<p>19. <b>Make a cookie:</b> <em>Spell &#8220;GINGERBREAD&#8221;</em></p>
<p>20. <b>Bake the cookie at 425 degree Fahrenheit:</b> <em>Tap the word &#8220;Bake&#8221;, set the temp. to 425 and press the Bake button.</em></p>
<p>21. <b>Which order did the lights appear?</b> <em>Orange, Green, Red, Blue (From level 2).</em></p>
<p>22. <b>Make a snowman from right to left:</b> <em>Press the word &#8220;SNOWMAN&#8221; twice, then put the smaller snowball onto the bigger one. Tap the word &#8220;SNOWMAN&#8221; again and put the smallest snowball on top. Tap the screen on the middle snowball toward the top-right to place the right arm and mitten followed by the face, the hat and lastly the left arm and mitten.</em></p>
<p>23. <b>Help Santa go down the chimney:</b> Tap the chimney five times so that it enlarges and tap the word &#8220;down&#8221;</em></p>
<p>24. <b>Make a snowstorm in 15 seconds:</b> <em> Tap the 11 snowflakes as they move about the screen.</em></p>
<p>25. <b>Ring the bells in the correct order:</b> <em> Tap the bells in order as displayed on the price tag from level 4.  4, 2, 1, 3.</em></p>
<p>26. <b>How many candy canes are there?</b> <em>36. (Optional: turn the screen upside down to reveal the candy canes).</em></p>
<p>27. <b>Put the reindeers in alphabetical order:</b> <em>Tap two reindeer to have them exchange places.  Sort the reindeer&#8217;s names as Blitzen, Comet, Cupid, Dancer, Dasher, Donner, Prancer, Rudolph, Vixen.</em></p>
<p>28. <b>Tie string to each ornament:</b> <em>Stop the ornaments from turning by tapping on it when their loop is facing upwards. Drag the word &#8220;string&#8221; to each ornament&#8217;s loop and shake the device.</em></p>
<p>29. <b>Quickly tap the 4th star:</b> <em>Tap the fourth star from the left.</em></p>
<p>30. <b>What change from the previous question?</b> <em>Tap the sixth star from the left.</em></p>
<p>31. <b>Tap the same 2 hollies:</b> <em> Tap the third holly on the top row and the second on the bottom row.</em></p>
<p>32. <b>Put the candy cane on the Christmas tree:</b> <em>Put seeds in the pot to make a Christmas tree appear. Drag the word &#8220;Christmas&#8221; onto the tree.  Drag the candy cane onto tree.</em></p>
<p>33. <b>Tap the cracker 9 + 7 &#8211; 6 (<em>expression changes</em>) times:</b>  <em>Perform the calculation (10 in this case) and tap the cracker that number of times.  There is a delay before proceeding to the next level.</em></p>
<p>34. <b>How many snowflakes did you tap?</b> <em>11.</em></p>
<p>35. <b>Catch the elf:</b> <em>Watch the bottom right window and tap the elf when it appears.  May take practice.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2013/01/16/impossible-test-christmas-answers-for-iphoneipad/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 205 Solution</title>
		<link>http://blog.dreamshire.com/2012/12/26/project-euler-problem-205-solution/</link>
		<comments>http://blog.dreamshire.com/2012/12/26/project-euler-problem-205-solution/#comments</comments>
		<pubDate>Wed, 26 Dec 2012 20:46:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 200-250]]></category>
		<category><![CDATA[Probability]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2515</guid>
		<description><![CDATA[Dice Game]]></description>
				<content:encoded><![CDATA[<h4><span style="text-decoration: underline;">Problem Description</span></h4>
<p>Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4.<br />
Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.</p>
<p>Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal.</p>
<p>What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg</p>
<h4><span style="text-decoration: underline;">Analysis</span></h4>
<table style="text-align: right; table-layout: fixed; border-collapse: collapse;" border="1">
<caption>Ways to achieve sums on dice</caption>
<tbody>
<tr>
<th>Type</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
<th>16</th>
<th>17</th>
<th>18</th>
<th>19</th>
<th>20</th>
<th>21</th>
<th>22</th>
</tr>
<tr>
<td>9 four-sided dice</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>9</td>
<td>45</td>
<td>165</td>
<td>486</td>
<td>1206</td>
<td>2598</td>
<td>4950</td>
<td>8451</td>
<td>13051</td>
<td>18351</td>
<td>23607</td>
<td>27876</td>
<td>30276</td>
</tr>
<tr>
<td>6 six-sided dice</td>
<td>1</td>
<td>6</td>
<td>21</td>
<td>56</td>
<td>126</td>
<td>252</td>
<td>456</td>
<td>756</td>
<td>1161</td>
<td>1666</td>
<td>2247</td>
<td>2856</td>
<td>3431</td>
<td>3906</td>
<td>4221</td>
<td>4332</td>
<td>4221</td>
</tr>
</tbody>
</table>
<table style="text-align: right; table-layout: fixed; border-collapse: collapse;" border="1">
<caption>Ways to achieve sums on dice</caption>
<tbody>
<tr>
<th>Type</th>
<th>23</th>
<th>24</th>
<th>25</th>
<th>26</th>
<th>27</th>
<th>28</th>
<th>29</th>
<th>30</th>
<th>31</th>
<th>32</th>
<th>33</th>
<th>34</th>
<th>35</th>
<th>36</th>
</tr>
<tr>
<td>9 four-sided dice</td>
<td>30276</td>
<td>27876</td>
<td>23607</td>
<td>18351</td>
<td>13051</td>
<td>8451</td>
<td>4950</td>
<td>2598</td>
<td>1206</td>
<td>486</td>
<td>165</td>
<td>45</td>
<td>9</td>
<td>1</td>
</tr>
<tr>
<td>6 six-sided dice</td>
<td>3906</td>
<td>3431</td>
<td>2856</td>
<td>2247</td>
<td>1666</td>
<td>1161</td>
<td>756</td>
<td>456</td>
<td>252</td>
<td>126</td>
<td>56</td>
<td>21</td>
<td>6</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>After calculating the table above all that remains is finding the ratio of winning combinations to the total possible combinations. So the denominator will be (4<sup>9</sup>)(6<sup>6</sup>) and the numerator the total number of ways Pete out sums Colin (Pete&#8217;s index one more than Colin&#8217;s). Draws are inherently ignored, which was fortuitous.</p>
<h4><span style="text-decoration: underline;">Solution</span></h4>
<p>Runs &lt; 1 second in Python.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#number of ways of rolling a total of n using 9 four-sided dice (pyramidal)</span>
p <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>  <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>  <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span>   <span style="color: #ff4500;">9</span><span style="color: #66cc66;">,</span>  <span style="color: #ff4500;">45</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">165</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">486</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1206</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2598</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4950</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8451</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13051</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18351</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">23607</span><span style="color: #66cc66;">,</span>\
<span style="color: #ff4500;">27876</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">30276</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">30276</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">27876</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">23607</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18351</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13051</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">8451</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4950</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2598</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1206</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">486</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">165</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">45</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">9</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #808080; font-style: italic;">#number of ways of rolling a total of n using 6 six-sided dice (cubic)</span>
c <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">21</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">56</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">126</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">252</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">456</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">756</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1161</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1666</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2247</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2856</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3431</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3906</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4221</span><span style="color: #66cc66;">,</span>\
<span style="color: #ff4500;">4332</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4221</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3906</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3431</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2856</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2247</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1666</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1161</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">756</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">456</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">252</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">126</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">56</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">21</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
w <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span>
t <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span>**<span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span> * <span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span>**<span style="color: #ff4500;">6</span>.<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> C <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">31</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> P <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span> <span style="color: black;">&#40;</span>C+<span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">31</span><span style="color: black;">&#41;</span>:
        w +<span style="color: #66cc66;">=</span> p<span style="color: black;">&#91;</span>P<span style="color: black;">&#93;</span>*c<span style="color: black;">&#91;</span>C<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE205 =&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">round</span><span style="color: black;">&#40;</span>w/t<span style="color: #66cc66;">,</span><span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h4><span style="text-decoration: underline;">Comments</span></h4>
<p>See also, <a title="OEIS A108907" href="http://oeis.org/A108907" target="_blank">OEIS A108907: Number of times a point sum n is attained in all 6^6 permutations of throwing 6 dice.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2012/12/26/project-euler-problem-205-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 219 Solution</title>
		<link>http://blog.dreamshire.com/2012/12/20/project-euler-problem-219-solution/</link>
		<comments>http://blog.dreamshire.com/2012/12/20/project-euler-problem-219-solution/#comments</comments>
		<pubDate>Fri, 21 Dec 2012 04:09:01 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 200-250]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=2507</guid>
		<description><![CDATA[Skew-cost coding]]></description>
				<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Let <b>A</b> and <b>B</b> be bit strings (sequences of 0&#8242;s and 1&#8242;s).<br />
If <b>A</b> is equal to the <u>left</u>most length(<b>A</b>) bits of <b>B</b>, then <b>A</b> is said to be a <em>prefix</em> of <b>B</b>.<br />
For example, 00110 is a prefix of <u>00110</u>1001, but not of 00111 or 100110.</p>
<p>A <em>prefix-free code of size</em> <var>n</var> is a collection of <var>n</var> distinct bit strings such that no string is a prefix of any other.  For example, this is a prefix-free code of size 6:</p>
<p style="text-align:center;">0000, 0001, 001, 01, 10, 11</p>
<p>Now suppose that it costs one penny to transmit a &#8217;0&#8242; bit, but four pence to transmit a &#8217;1&#8242;.<br />
Then the total cost of the prefix-free code shown above is 35 pence, which happens to be the cheapest possible for the skewed pricing scheme in question.<br />
In short, we write Cost(6) = 35.</p>
<p>What is Cost(10<sup>9</sup>) ?</p>
<h4><u>Analysis</u></h4>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">L <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">10</span>**<span style="color: #ff4500;">9</span>
S <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> L-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
a <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span> 
<span style="color: #ff7700;font-weight:bold;">while</span> S<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:  
  S <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>S<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+S<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> S<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> S<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> S<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> S<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>-S<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
  a +<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span>
&nbsp;
cost <span style="color: #66cc66;">=</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span> <span style="color: black;">&#40;</span>a+i<span style="color: black;">&#41;</span>*S<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE219 =&quot;</span><span style="color: #66cc66;">,</span> cost</pre></td></tr></table></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2012/12/20/project-euler-problem-219-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

 Served from: blog.dreamshire.com @ 2013-05-20 15:13:44 by W3 Total Cache -->