<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<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/"
	>

<channel>
	<title>Delphi Puzzles</title>
	<link>http://delphipuzzle.twodesk.com</link>
	<description>Delphi programming puzzles and games</description>
	<pubDate>Fri, 18 Apr 2008 07:00:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>Solution to &#8220;Searching for Bobby Fisher&#8221;</title>
		<link>http://delphipuzzle.twodesk.com/?p=29</link>
		<comments>http://delphipuzzle.twodesk.com/?p=29#comments</comments>
		<pubDate>Fri, 18 Apr 2008 07:00:31 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=29</guid>
		<description><![CDATA[In Roddy's own words:
"I was using the "Find" method on an unsorted TStringlist... which you're not supposed to do. Sometimes it works (my initial test case had the items in my list already sorted, just by coincidence), but usually it doesn't..."
So, Roddy had two options:






{...}


Names.LoadFromFile&#40;'names.txt'&#41;;


Names.Sorted := True;


{...} 








...or...






//Instead of using Names.find, do the following:


Result := [...]]]></description>
			<content:encoded><![CDATA[<p>In Roddy's own words:</p>
<p>"I was using the "Find" method on an unsorted TStringlist... which you're not supposed to do. Sometimes it works (my initial test case had the items in my list already sorted, just by coincidence), but usually it doesn't..."</p>
<p>So, Roddy had two options:</p>
<pre>
<div class="syntax_hilite">
<div id="delphi-3">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{...}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Names.<span style="color: #006600;">LoadFromFile</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'names.txt'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Names.<span style="color: #006600;">Sorted</span> := <span style="color: #000000; font-weight: bold;">True</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{...}</span> </div>
</li>
</ol>
</div>
</div>
</div>


</pre>
<p>...or...</p>
<pre>
<div class="syntax_hilite">
<div id="delphi-4">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//Instead of using Names.find, do the following:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Result := Names.<span style="color: #006600;">IndexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Bobby Fisher'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>


</pre>
<p>Thanks again to Roddy for suggesting this puzzle.  If you'd like to submit an idea for a puzzle, email me at jacob(at)twodesk.com.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=29</wfw:commentRss>
		</item>
		<item>
		<title>Searching for Bobby Fisher</title>
		<link>http://delphipuzzle.twodesk.com/?p=28</link>
		<comments>http://delphipuzzle.twodesk.com/?p=28#comments</comments>
		<pubDate>Mon, 14 Apr 2008 07:00:13 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=28</guid>
		<description><![CDATA[Special thanks to puzzle regular Roddy Pratt for suggesting the idea for this week's puzzle.
Roddy (*Names may have been changed to protect the innocent) had code similar to the following:






function FindBobbyFisher: Integer;


var


&#160; Names: TStringList;


&#160; I: Integer;


begin


&#160; Names := TStringList.Create;


&#160; Names.LoadFromFile&#40;'names.txt'&#41;;


&#160; if not Names.Find&#40;'Bobby Fisher', I&#41; then


&#160; &#160; Result := -1


&#160; else


&#160; &#160; Result := I;


&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Special thanks to puzzle regular <em>Roddy Pratt</em> for suggesting the idea for this week's puzzle.</p>
<p>Roddy (*Names may have been changed to protect the innocent) had code similar to the following:</p>
<pre>
<div class="syntax_hilite">
<div id="delphi-6">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> FindBobbyFisher: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Names: TStringList;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; I: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Names := TStringList.<span style="color: #006600;">Create</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Names.<span style="color: #006600;">LoadFromFile</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'names.txt'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">not</span> Names.<span style="color: #006600;">Find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Bobby Fisher'</span>, I<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Result := -<span style="color: #cc66cc;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Result := I;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Names.<span style="color: #006600;">Free</span>;&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>; </div>
</li>
</ol>
</div>
</div>
</div>


</pre>
<p>Roddy's code worked at first, but soon Bobby Fisher could not be found.  What is Roddy missing?</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=28</wfw:commentRss>
		</item>
		<item>
		<title>Answer to &#8220;Include&#8221;</title>
		<link>http://delphipuzzle.twodesk.com/?p=27</link>
		<comments>http://delphipuzzle.twodesk.com/?p=27#comments</comments>
		<pubDate>Fri, 11 Apr 2008 07:00:33 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=27</guid>
		<description><![CDATA[The problem here was with the {$INCLUDE} directives.  The following line compiles in the IDE, but not from the command line (or from a build tool that uses the command line compiler):
{$INCLUDE ../includes/dotnet.inc}
The fix?  Use backslashes in the file path, not forward slashes:
{$INCLUDE ..\includes\dotnet.inc}
]]></description>
			<content:encoded><![CDATA[<p>The problem here was with the {$INCLUDE} directives.  The following line compiles in the IDE, but not from the command line (or from a build tool that uses the command line compiler):</p>
<p>{$INCLUDE ../includes/dotnet.inc}</p>
<p>The fix?  Use backslashes in the file path, not forward slashes:</p>
<p>{$INCLUDE ..\includes\dotnet.inc}</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=27</wfw:commentRss>
		</item>
		<item>
		<title>Include</title>
		<link>http://delphipuzzle.twodesk.com/?p=26</link>
		<comments>http://delphipuzzle.twodesk.com/?p=26#comments</comments>
		<pubDate>Mon, 07 Apr 2008 07:00:16 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=26</guid>
		<description><![CDATA[Kyle put the following code in his file:





{$IFDEF CLR}


&#160; {$INCLUDE ../includes/dotnet.inc}


{$ELSE}


&#160; {$INCLUDE ../includes/win32.inc}


{$ENDIF} 






During testing, everything worked as expected, but after checking in all the files, he got an email from the build machine telling him he broke the build.
What did he do wrong?
]]></description>
			<content:encoded><![CDATA[<p>Kyle put the following code in his file:</p>
<div class="syntax_hilite">
<div id="delphi-8">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{$IFDEF CLR}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">{$INCLUDE ../includes/dotnet.inc}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{$ELSE}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">{$INCLUDE ../includes/win32.inc}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{$ENDIF}</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>During testing, everything worked as expected, but after checking in all the files, he got an email from the build machine telling him he broke the build.</p>
<p>What did he do wrong?</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=26</wfw:commentRss>
		</item>
		<item>
		<title>Answer to &#8220;Sing a Song&#8221;</title>
		<link>http://delphipuzzle.twodesk.com/?p=25</link>
		<comments>http://delphipuzzle.twodesk.com/?p=25#comments</comments>
		<pubDate>Fri, 22 Feb 2008 07:00:26 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=25</guid>
		<description><![CDATA[This puzzle came about a couple weeks ago when I was in a church.  I looked at the hymn board and discovered that there were so many "1"s on the board that they had run out, and someone had turned a "7" upside-down and covered the horizontal part with masking tape.  I thought, [...]]]></description>
			<content:encoded><![CDATA[<p>This puzzle came about a couple weeks ago when I was in a church.  I looked at the hymn board and discovered that there were so many "1"s on the board that they had run out, and someone had turned a "7" upside-down and covered the horizontal part with masking tape.  I thought, "there must be a simple way to know how many of each digit card to include."  I came up with one method, but thought it would be interesting to see if anyone else could come up with something better.</p>
<p>Here's the method I used:</p>
<ol>
<li>Create an array containing one string for each song in the songbook</li>
<li>Sort the array by the number of occurrences of the digit in question</li>
<li>Get the number of occurrences of the digit in the first X strings in the array, where X is the number of songs in the service.</li>
</ol>
<p>Here's my code:</p>
<pre>
<div class="syntax_hilite">
<div id="delphi-10">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> CountOccurrences<span style="color: #66cc66;">&#40;</span>S: <span style="color: #993333;">string</span>; AInt: <span style="color: #993333;">Integer</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; I: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result := <span style="color: #cc66cc;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">for</span> I := <span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #000000; font-weight: bold;">to</span> <span style="color: #000066;">Length</span><span style="color: #66cc66;">&#40;</span>S<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> S<span style="color: #66cc66;">&#91;</span>I<span style="color: #66cc66;">&#93;</span> = <span style="color: #000066;">Chr</span><span style="color: #66cc66;">&#40;</span>AInt + <span style="color: #cc66cc;color:#800000;">48</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000066;">Inc</span><span style="color: #66cc66;">&#40;</span>Result<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, Digit: <span style="color: #993333;">Integer</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; I, J: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Numbers: <span style="color: #000000; font-weight: bold;">array</span> <span style="color: #000000; font-weight: bold;">of</span> <span style="color: #993333;">string</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; TempStr: <span style="color: #993333;">string</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//Make sure the numbers are as expected</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066;">Assert</span><span style="color: #66cc66;">&#40;</span>SongsInService &lt;SongsInBook<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066;">Assert</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>Digit &lt;<span style="color: #cc66cc;color:#800000;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">and</span> <span style="color: #66cc66;">&#40;</span>Digit&gt; -<span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//First, fill an array with all the numbers in the book</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066;">SetLength</span><span style="color: #66cc66;">&#40;</span>Numbers, SongsInBook<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">for</span> I := <span style="color: #cc66cc;color:#800000;">0</span> <span style="color: #000000; font-weight: bold;">to</span> SongsInBook - <span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Numbers<span style="color: #66cc66;">&#91;</span>I<span style="color: #66cc66;">&#93;</span> := <span style="color: #000066;">IntToStr</span><span style="color: #66cc66;">&#40;</span>I + <span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//Sort the array by the number of occurences of the digit in question. We'll</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//just use a simple sort.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">for</span> I := <span style="color: #cc66cc;color:#800000;">0</span> <span style="color: #000000; font-weight: bold;">to</span> SongsInBook - <span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> J := I + <span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #000000; font-weight: bold;">to</span> SongsInBook - <span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> CountOccurrences<span style="color: #66cc66;">&#40;</span>Numbers<span style="color: #66cc66;">&#91;</span>J<span style="color: #66cc66;">&#93;</span>, Digit<span style="color: #66cc66;">&#41;</span>&gt;=</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CountOccurrences<span style="color: #66cc66;">&#40;</span>Numbers<span style="color: #66cc66;">&#91;</span>I<span style="color: #66cc66;">&#93;</span>, Digit<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; TempStr := Numbers<span style="color: #66cc66;">&#91;</span>I<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Numbers<span style="color: #66cc66;">&#91;</span>I<span style="color: #66cc66;">&#93;</span> := Numbers<span style="color: #66cc66;">&#91;</span>J<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Numbers<span style="color: #66cc66;">&#91;</span>J<span style="color: #66cc66;">&#93;</span> := TempStr;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//Finally, get the number of digits in the top SongsInService &quot;worst&quot; songs</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; TempStr := <span style="color: #ff0000;">''</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">for</span> I := <span style="color: #cc66cc;color:#800000;">0</span> <span style="color: #000000; font-weight: bold;">to</span> SongsInService - <span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; TempStr := TempStr + Numbers<span style="color: #66cc66;">&#91;</span>I<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result := CountOccurrences<span style="color: #66cc66;">&#40;</span>TempStr, Digit<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> GetDigitCounts<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService: <span style="color: #993333;">Integer</span><span style="color: #66cc66;">&#41;</span>: THymnBoard;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{var</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; I: Integer;}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N0</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N1</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N2</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N3</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N4</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">4</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N5</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">5</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N6</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">6</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N7</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">7</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N8</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">8</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Result.<span style="color: #006600;">N9</span> := GetCount<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService, <span style="color: #cc66cc;color:#800000;">9</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//The following also works, and is much shorter, but is less readable:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">{&nbsp; for I := 0 to 9 do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; Integer(Pointer(Integer(@Result) + I * sizeof(Integer))^) :=</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; GetCount(SongsInBook, SongsInService, I);}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>; </div>
</li>
</ol>
</div>
</div>
</div>

</pre>
<p>There are a couple of optimizations that could be done here that I didn't do for the sake of readability.  One is that the array doesn't necessarily need to be regenerated each time, but really only needs to be re-sorted for each digit.  The array could be generated only once.</p>
<p>Also, a better sorting algorithm than simple sort could obviously be used, but the sorting isn't the point here, so I decided to keep the code as simple as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=25</wfw:commentRss>
		</item>
		<item>
		<title>Sing a song</title>
		<link>http://delphipuzzle.twodesk.com/?p=23</link>
		<comments>http://delphipuzzle.twodesk.com/?p=23#comments</comments>
		<pubDate>Mon, 18 Feb 2008 16:55:24 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=23</guid>
		<description><![CDATA[You've been hired by a company that creates hymn boards - a plaque that goes on the wall of a church showing which songs are to be sung during a church service:
Your task is to create an algorithm to determine the minimum number of each digit card (the little cards with a single digit, 0-9, [...]]]></description>
			<content:encoded><![CDATA[<p>You've been hired by a company that creates hymn boards - a plaque that goes on the wall of a church showing which songs are to be sung during a church service:</p>
<p><img src="http://delphipuzzle.twodesk.com/wp-content/uploads/2008/02/hymnboard.jpg" alt="Hymn Board" align="left" style="margin-right: 12px;" />Your task is to create an algorithm to determine the minimum number of each digit card (the little cards with a single digit, 0-9, on them) to include with each hymn board.  You are given the number of songs in the songbook, and the maximum number of songs that can be sung during a service.  A song is never repeated during a service.</p>
<p>For example, if there are 10 songs in the book, and two songs can be sung during a service, then you need exactly two 1s, and one of everything else (two 1s in case the two songs are 1 and 10).  That one is easy enough to do by hand, but most books will have a few hundred songs, and at least three songs are sung at most services.  Your algorithm should be universal.</p>
<p>Here's your skeleton code:</p>
<div class="syntax_hilite">
<div id="delphi-12">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">type</span> THymnBoard = <span style="color: #000000; font-weight: bold;">record</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; N0, N1, N2, N3, N4, N5, N6, N7, N8, N9: <span style="color: #993333;">Integer</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> GetDigitCounts<span style="color: #66cc66;">&#40;</span>SongsInBook, SongsInService: <span style="color: #993333;">Integer</span><span style="color: #66cc66;">&#41;</span>: THymnBoard;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//You fill in here</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=23</wfw:commentRss>
		</item>
		<item>
		<title>Answer to &#8220;Through the eye of the needle&#8221;</title>
		<link>http://delphipuzzle.twodesk.com/?p=22</link>
		<comments>http://delphipuzzle.twodesk.com/?p=22#comments</comments>
		<pubDate>Fri, 15 Feb 2008 07:00:33 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=22</guid>
		<description><![CDATA[Steven pointed out that Ray's code did not gracefully handle the application closing.  If the application closed while there were still transactions processing, the application would simply appear to hang, with its user interface frozen, until either all the transactions were processed, or the user killed the process.
The solution is to check Application.Terminated in [...]]]></description>
			<content:encoded><![CDATA[<p>Steven pointed out that Ray's code did not gracefully handle the application closing.  If the application closed while there were still transactions processing, the application would simply appear to hang, with its user interface frozen, until either all the transactions were processed, or the user killed the process.</p>
<p>The solution is to check Application.Terminated in the thread loop:</p>
<pre>
<div class="syntax_hilite">
<div id="delphi-14">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">while</span> TransactionsPending <span style="color: #000000; font-weight: bold;">and</span> <span style="color: #000000; font-weight: bold;">not</span> Application.<span style="color: #006600;">Terminated</span> <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">//etc...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>; </div>
</li>
</ol>
</div>
</div>
</div>


</pre>
<p>With this check in, when the application is closed, the thread will finish processing the transaction it's processing, and then gracefully exit.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=22</wfw:commentRss>
		</item>
		<item>
		<title>Through the eye of the needle</title>
		<link>http://delphipuzzle.twodesk.com/?p=21</link>
		<comments>http://delphipuzzle.twodesk.com/?p=21#comments</comments>
		<pubDate>Mon, 11 Feb 2008 07:00:14 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=21</guid>
		<description><![CDATA[Ray was a junior developer at a large, nameless bank.  One of the applications his group maintained was one that settled certain transactions.  Ray was tasked with seeing if he could make the application run any faster.  He discovered that the major bottleneck was processing on a network server, and decided that [...]]]></description>
			<content:encoded><![CDATA[<p>Ray was a junior developer at a large, nameless bank.  One of the applications his group maintained was one that settled certain transactions.  Ray was tasked with seeing if he could make the application run any faster.  He discovered that the major bottleneck was processing on a network server, and decided that if he made his application multithreaded, it could settle multiple transactions at once, and it would be faster.  He created a new thread object and wrote the following Execute method:</p>
<pre>
<div class="syntax_hilite">
<div id="delphi-16">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">procedure</span> TProcessingThread.<span style="color: #006600;">Execute</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Trans: TAccountTransaction;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">while</span> TransactionsPending <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Trans := TAccountTransaction.<span style="color: #006600;">Create</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; GetNextPendingTransaction<span style="color: #66cc66;">&#40;</span>Trans<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Trans.<span style="color: #006600;">Settle</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">except</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">on</span> E: Exception <span style="color: #000000; font-weight: bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogError<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">Format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Transaction %d failed: %s'</span>, <span style="color: #66cc66;">&#91;</span>Trans.<span style="color: #006600;">ID</span>, E.<span style="color: #006600;">Message</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">finally</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Trans.<span style="color: #006600;">Free</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">end</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">end</span>; </div>
</li>
</ol>
</div>
</div>
</div>


</pre>
<p>When Steven, a senior developer looked at the code, he pulled Ray aside and gently explained something very important that Ray had missed.  What did Steven tell Ray?</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=21</wfw:commentRss>
		</item>
		<item>
		<title>The puzzle has the week off</title>
		<link>http://delphipuzzle.twodesk.com/?p=20</link>
		<comments>http://delphipuzzle.twodesk.com/?p=20#comments</comments>
		<pubDate>Mon, 04 Feb 2008 07:00:41 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Info]]></category>

		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=20</guid>
		<description><![CDATA[Delphi Puzzles is taking the week off this week.
The site is being moved to a new server, and to make sure that we don't lose any comments in the process, there is no puzzle this week.  It will be back in full force, better-than-ever, next week.
--Jacob
]]></description>
			<content:encoded><![CDATA[<p><em>Delphi Puzzles</em> is taking the week off this week.</p>
<p>The site is being moved to a new server, and to make sure that we don't lose any comments in the process, there is no puzzle this week.  It will be back in full force, better-than-ever, next week.</p>
<p>--Jacob</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=20</wfw:commentRss>
		</item>
		<item>
		<title>Solution to &#8220;Bluff the Programmer&#8221;</title>
		<link>http://delphipuzzle.twodesk.com/?p=19</link>
		<comments>http://delphipuzzle.twodesk.com/?p=19#comments</comments>
		<pubDate>Fri, 01 Feb 2008 07:00:25 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
		
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://delphipuzzle.twodesk.com/?p=19</guid>
		<description><![CDATA[The valid code is option #1:





Memo1.Lines.Add&#40;Format&#40;'1: %s' + ^M + '2: %s', &#91;Edit1.Text, Edit2.Text&#93;&#41;; 






^M is a Delphi code construct that compiles, but I haven't found it documented anywhere.  The ^M is character - analogue to #13#10 (or \n in c parlance).  The + operators aren't even necessary.  You can do 'string1'^M'string2' [...]]]></description>
			<content:encoded><![CDATA[<p>The valid code is option #1:</p>
<div class="syntax_hilite">
<div id="delphi-18">
<div class="delphi">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Memo1.<span style="color: #006600;">Lines</span>.<span style="color: #006600;">Add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">Format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1: %s'</span> + ^M + <span style="color: #ff0000;">'2: %s'</span>, <span style="color: #66cc66;">&#91;</span>Edit1.<span style="color: #006600;">Text</span>, Edit2.<span style="color: #006600;">Text</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>^M is a Delphi code construct that compiles, but I haven't found it documented anywhere.  The ^M is character - analogue to #13#10 (or \n in c parlance).  The + operators aren't even necessary.  You can do 'string1'^M'string2' and get the same result.</p>
<p>I wouldn't encourage using this in code, but it's good to know that it exists.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphipuzzle.twodesk.com/?feed=rss2&amp;p=19</wfw:commentRss>
		</item>
	</channel>
</rss>
