<?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>stchur-BLOG &#187; Computing</title>
	<atom:link href="http://blog.stchur.com/category/computing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stchur.com</link>
	<description>web / programming / javascript / css / html</description>
	<lastBuildDate>Sat, 16 Jan 2010 01:09:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Palindromes</title>
		<link>http://blog.stchur.com/2008/06/14/palindromes/</link>
		<comments>http://blog.stchur.com/2008/06/14/palindromes/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 19:06:11 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript basics]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Useful Functions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[palindrome]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/06/14/palindromes/</guid>
		<description><![CDATA[	The other evening while I was watching TV, I happened to have my laptop out, so I started dabbling around in Firebug just for the heck of it.  I'll often just start writing functions for no particular reason, other than to refresh my skills or perhaps discover something about Javascript that I hadn't previously [...]]]></description>
			<content:encoded><![CDATA[	<p>The other evening while I was watching TV, I happened to have my laptop out, so I started dabbling around in Firebug just for the heck of it.  I'll often just start writing functions for no particular reason, other than to refresh my skills or perhaps discover something about Javascript that I hadn't previously known.</p>
	<p>So I was monkeying around, and I decided to write a function to determine if a string were a <a href = "http://en.wikipedia.org/wiki/Palindrome" title = "Wikipedia: Palindrome">palindrome</a>.  This is not a very difficult problem, but it might make for a descent introductory interview question (one of those questions you'd ask to rule out the truly inept).</p>
	<p>With problems like this, I'm mostly interested in two things:</p>
	<ol>
	<li>Writing the function in a clever way, so as to achieve the least amount of code possible</li>
	<li>Writing the function in the most efficient way possible</li>
	</ol>
	<p>Now, if it were Ruby, writing the clever solution (which might also happen to be the most efficient solution in that language -- not sure) would be ridiculously easy:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
&nbsp; &nbsp;s == s.<span class="me1">reverse</span><br />
<span class="kw1">end</span></div>
	<p>That's so trivial, it's probably not even worth writing.</p>
	<p>But Javascript doesn't have a .reverse() method on strings, so you have to take an extra step (still pretty easy though):</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> s === s.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span>.<span class="me1">reverse</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
	<p>Split the string into an array, reverse the array, and then join it back together.  Since this is all native Javascript stuff, it turns out to be reasonably fast, though one might expect you could do better.</p>
	<p>For grins and giggles, I decided to see how a manual solution, where I compare characters starting at each end of the string and work towards the middle, would compare:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> i, j,<br />
&nbsp; &nbsp;len = s.<span class="me1">length</span>,<br />
&nbsp; &nbsp;mid = Math.<span class="me1">floor</span><span class="br0">&#40;</span>len / <span class="nu0">2</span><span class="br0">&#41;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span>i = <span class="nu0">0</span>, j = len - <span class="nu0">1</span>; i &lt; mid; i++, j--<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>s.<span class="me1">charAt</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span> !== s.<span class="me1">charAt</span><span class="br0">&#40;</span>j<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span></div>
	<p>This function will probably fail quickly, so that's good, but it also turns out to be generally faster than the split/reverse/join version.  It makes sense, since you're going through the string only one time and twice as fast as normal since you're traversing from both ends at the same time.</p>
	<p>Can you do even better though?  Maybe.  At least in Firefox you can, and quick tests seemed to confirm across other browsers too (but I didn't test extensively).</p>
	<p>What if we split the string in half, and only reverse half of it.  Then we can compare the first half with the original half:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> len = s.<span class="me1">length</span>,<br />
&nbsp; &nbsp;x = len / <span class="nu0">2</span>,<br />
&nbsp; &nbsp;y = x === Math.<span class="me1">floor</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span> ? x : <span class="br0">&#40;</span>x = Math.<span class="me1">floor</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span><span class="br0">&#41;</span> + <span class="nu0">1</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> s.<span class="me1">substr</span><span class="br0">&#40;</span><span class="nu0">0</span>, x<span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span>.<span class="me1">reverse</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span> === s.<span class="me1">substr</span><span class="br0">&#40;</span>y,len<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
	<p>The idea here is just split the string in half, reverse one of the two pieces and compare them.  For example:</p>
	<p>TOOT: splits into "TO" and "OT."  Reverse one of them, say the second part ("OT") and compare.</p>
	<p>If you've got a odd number of characters, you can ignore the middle most character:</p>
	<p>RACECAR: splits into "RAC" and "CAR" (ignore the "E" in the middle).  Reverse the second part ("CAR") and compare.</p>
	<p>As it turned out, this seemed to be faster than the other two methods.  Not by orders of magnitude, but not insignificantly either.  Here are the number from Firefox, running each function 10,000 time on the string "gohangasalamiimalasagnahog" (go hang a salami, i'm a lasagna hog).</p>
	<ul>
	<li>(Traverse from both ends): 797ms</li>
	<li>(Split/Reverse/Join): 969ms</li>
	<li>(Compare halves): 640ms</li>
	</ul>
	<p>Of course, numbers varied slightly during each run, but overall the they were consistent relative to each other.</p>
	<p>So there ya go.  Nothing in particular I wanted to point out here; just found it interesting and thought you might too.</p>
	<p>Think my code sucks?  Got a better solution?  Say so in the comments (you won't hurt my feelings).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/06/14/palindromes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Another Puzzle</title>
		<link>http://blog.stchur.com/2008/06/09/another-puzzle/</link>
		<comments>http://blog.stchur.com/2008/06/09/another-puzzle/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 17:25:38 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[npr]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/06/09/another-puzzle/</guid>
		<description><![CDATA[	Well after some significant thought, I have decided that I was either not given enough information to solve the puzzle in my last post, or I'm going to need a hint from the person who gave me the puzzle.
	Either way though, I don't have an answer to post as I promised I would, but not [...]]]></description>
			<content:encoded><![CDATA[	<p>Well after some significant thought, I have decided that I was either not given enough information to solve the puzzle in my <a href = "http://blog.stchur.com/2008/06/05/a-puzzle/" Title = "Incoherent Babble | A Puzzle">last post</a>, or I'm going to need a hint from the person who gave me the puzzle.</p>
	<p>Either way though, I don't have an answer to post as I promised I would, but not to worry... I have something better: another puzzle!</p>
	<p>This one I heard on NPR's Puzzle Master, so I know it is legit:</p>
	<div>
On a calculator display there are 5 digits.  The first 4 digits are: 8 7 3 5.  What is the fifth digit?
</div>
	<p>It's not a trick question, and there is a simple and elegant solution to the puzzle.</p>
	<p><button onclick="document.getElementById('theAnswer').style.display='block';">Display the answer</button></p>
	<div id = "theAnswer" style = "display: none; border: 2px solid #ccc; margin: 1em; padding: 1em; background: #333;">
	<p>The fifth digit is 5 (so that the calclator display reads: 8 7 3 5 5).  Why?  Because it fits the pattern.  Digits displayed on a calculator are usually displayed with bars. For example a 3 would be displayed with 5 bars, like so:</p>
	<p style = "margin-left: 3em;">
_<br />
_|<br />
_|
</p>
	<p>So if you start with an 8, you can easily figure out the pattern.  An 8 is made up of 7 bars, so 7 is the next digit.  A 7 is made up of 3 bars so 3 is the next digit.  A 3 is made up of 5 bars so 5 is the next digit.  And finally, a 5 is made up of 5 bars, so 5 is again the next digit.</p>
	</div>
	<p>In case you're wondering, I heard this in the car listening to the radio, and I'd estimate that I drove about 5 miles or so before the answers came to me, so I'll guess that that was about 5 minutes.  How long did it take you to solve?</p>
	<p>Comments welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/06/09/another-puzzle/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Puzzle</title>
		<link>http://blog.stchur.com/2008/06/05/a-puzzle/</link>
		<comments>http://blog.stchur.com/2008/06/05/a-puzzle/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 22:45:12 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/06/05/a-puzzle/</guid>
		<description><![CDATA[	So today at lunch, my co-worker gave me this puzzle to solve:
	
	In some prison, there are 10 prisoners and a prison guard.  The prison guard plans to randomly choose prisoners and take them one by one into a special room that has two switches.  Either switch can be on or off and no [...]]]></description>
			<content:encoded><![CDATA[	<p>So today at lunch, my co-worker gave me this puzzle to solve:</p>
	<div style="background: #333; padding: 1em; margin: 1em; border: 2px solid #ccc">
	<p>In some prison, there are 10 prisoners and a prison guard.  The prison guard plans to randomly choose prisoners and take them one by one into a special room that has two switches.  Either switch can be on or off and no one knows the starting state of each switch ahead of time.  Upon entering the room, the prisoner must throw one (and exactly one switch).  He cannot simply do nothing and then leave the room.</p>
	<p>Once all 10 prisoners have entered the special room, the prison guard will let them go free, but there is a catch.  One of the prisoners needs to declare when all prisoners have entered the special room at least one time.  If his declaration is correct, they all go free.  If not, they all die.  Remember that the guard is bringing prisoners into the special room completely at random, and there is nothing to stop him from bring the same person into the room multiple times.</p>
	<p>Now, before the big event, the prison guard is going to allow the prisoners to meet together and try to come up with a strategy to figure out how to determine how they'll know when all 10 prisoners have entered the special room at least once.</p>
	</div>
	<p>So what's the strategy?</p>
	<p>Now I just heard this problem, and I've only put minimal thought into it so far.  I will post back with the "official" answer in a few days, but before I do, I thought it would be amusing to entertain some of my quick responses, all of which (based on the problem as it was given to me) are completely valid:</p>
	<h3>My first solution:</h3>
	<p>Each prisoner who enters the room takes off one shoe and leaves it behind (while also throwing one switch as per the rules).  When a prisoner enters the room and sees 9 shoes, he'll know he is the 10th prisoner.</p>
	<p>My co-worker then replied that the prisoners don't have any shoes, or any clothes of any kind; they're completely naked.</p>
	<h3>My second solution:</h3>
	<p>Each prisoner who enters the room spits a giant <a href = "http://www.urbandictionary.com/define.php?term=loogie" title = "Definition of loogie">loogie</a> onto the ground (while also throwing one switch as per the rules).  When a prisoner enters the room and sees 9 loogies on the ground, he'll know he is the 10th prisoner.</p>
	<p>My co-worker then replied that the loogies will dry up and you won't be able to see them because the guard will wait a very long time between bringing each prisoner into the room.</p>
	<h3>My third solution:</h3>
	<p>Each prisoner who enters the room will leave a giant poo on the ground (while also throwing one switch as per the rules), and when a prisoner enters the room and sees 9 piles of poo, he'll know he is the 10th visitor.</p>
	<p>At this point, we were all laughing too hard to have any further intelligent discussion on the matter.</p>
	<p>But I do plan to think about this and report back with a "real" solution.  I have a few ideas already, but I don't have sufficient time to get them into words.</p>
	<p>If you'd like to post an answer, you're welcome to do so.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/06/05/a-puzzle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Earth is cool?</title>
		<link>http://blog.stchur.com/2008/05/13/google-earth-is-cool/</link>
		<comments>http://blog.stchur.com/2008/05/13/google-earth-is-cool/#comments</comments>
		<pubDate>Tue, 13 May 2008 16:51:24 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[MiniPosts]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Sky]]></category>
		<category><![CDATA[Telescope]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/05/13/google-earth-is-cool/</guid>
		<description><![CDATA[I - don't - think - so!

Compared to this, Google Earth/Sky looks like a Fisher Price toy for little kids.

Oh, and by the way all you "Microsoft only copies" people out there, this product was in the works long before Google Earth/Sky ever existed.]]></description>
			<content:encoded><![CDATA[<p>I - don't - think - so!</p>

<p>Compared to <a href = "http://www.worldwidetelescope.org/" title = "WorldWide Telescope">this</a>, Google Earth/Sky looks like a Fisher Price toy for little kids.</p>

<p>Oh, and by the way all you "Microsoft only copies" people out there, <a href = "http://www.worldwidetelescope.org/" title = "WorldWide Telescope">this product</a> was in the works <em>long</em> before Google Earth/Sky ever existed.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/05/13/google-earth-is-cool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copy Cats</title>
		<link>http://blog.stchur.com/2008/01/23/copy-cats/</link>
		<comments>http://blog.stchur.com/2008/01/23/copy-cats/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 17:49:31 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[MiniPosts]]></category>
		<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/01/23/copy-cats/</guid>
		<description><![CDATA[With Valentine's Day coming up, Apple has announced a new, pink iPod nano to its line-up.

The new 8GB iPod nano "costs $199 and includes free engraving on the back..." (emphasis mine)

Free engraving?  Gee, I wonder where they got that idea.]]></description>
			<content:encoded><![CDATA[<p>With Valentine's Day coming up, Apple has announced a new, pink iPod nano to its line-up.</p>

<p>The new 8GB iPod nano "costs $199 and includes <strong>free engraving</strong> on the back..." (emphasis mine)</p>

<p>Free engraving?  Gee, I wonder where they <a href = "http://www.zuneoriginals.net" title = "Zune Originals">got that idea</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/01/23/copy-cats/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>IE7 Auto Update</title>
		<link>http://blog.stchur.com/2008/01/21/ie7-auto-update/</link>
		<comments>http://blog.stchur.com/2008/01/21/ie7-auto-update/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 06:38:25 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[MiniPosts]]></category>
		<category><![CDATA[Web-related]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/01/21/ie7-auto-update/</guid>
		<description><![CDATA[To further validate my previous post, InfoWorld has a new article out which explains how Microsoft is warning businesses of an impending auto update to IE7.

Buh-bye IE6!  Nice knowing ya.]]></description>
			<content:encoded><![CDATA[<p>To further validate my <a href = "http://blog.stchur.com/2008/01/15/ie5-is-dead-and-ie6-is-on-death-row/" title = "IE5 is Dead (and IE6 is on Death Row)">previous post</a>, <a href = "http://www.infoworld.com/" title = "InfoWorld">InfoWorld</a> has <a href = "http://www.infoworld.com/archives/emailPrint.jsp?R=printThis&#038;A=/article/08/01/17/Microsoft-warns-businesses-of-autoupdate-to-IE7_1.html" title = "Microsoft warns of impending auto update to IE7">a new article</a> out which explains how Microsoft is warning businesses of an impending auto update to IE7.<p>

<p>Buh-bye IE6!  Nice knowing ya.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/01/21/ie7-auto-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>2nd Impression of Windows Vista</title>
		<link>http://blog.stchur.com/2008/01/11/2nd-impression-of-windows-vista/</link>
		<comments>http://blog.stchur.com/2008/01/11/2nd-impression-of-windows-vista/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 05:15:47 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/01/11/2nd-impression-of-windows-vista/</guid>
		<description><![CDATA[	So I finally got the stupid box open and got the thing installed (which was quite painless I must admit).  I did a clean install, not an upgrade, and I'd recommend you do the same if you are considering upgrading to Vista.
	It automatically found drivers for pretty much all of my hardware, save for [...]]]></description>
			<content:encoded><![CDATA[	<p>So I finally got the <a href = "http://blog.stchur.com/2008/01/11/windows-vista/" title = "Windows Vista packaging is horrible">stupid box open</a> and got the thing installed (which was quite painless I must admit).  I did a clean install, not an upgrade, and I'd recommend you do the same if you are considering upgrading to Vista.</p>
	<p>It automatically found drivers for pretty much all of my hardware, save for my sound card, which was easily solved by quickly downloading the Vista drivers from the Toshiba support web site.</p>
	<p>My laptop came with a TV tuner, so I took advantage of the fact that <a href = "http://www.microsoft.com/windows/products/windowsvista/editions/ultimate/default.mspx" title = "Windows Vista, Ultimate Edition">Vista Ultimate</a> has <a href = "http://www.microsoft.com/windows/products/windowsvista/features/details/mediacenter.mspx" title = "Windows Media Center">Media Center</a> built in.  I connected it to my cable and recorded an episode of <a href = "http://www.thesimpsons.com/index.html" title = "The Simpsons">The Simpsons</a>, which I then put on my <a href = "http://www.zune.net" title = "Zune, better than the iPod!">Zune</a> -- the whole process was super easy (and very cool!).</p>
	<p>So far I like it.  It's performing as well, if not better, than XP did on my laptop, so I'm not sure what all the nay-sayers are whining  about.  Seems to me that some people just desperately <em>want</em> to hate Microsoft products, even when there is no real reason to do so.</p>
	<p>If you've got the hardware for it (which, IMO, is a minimum of 2 Gigs of Ram and at least a <a href = "http://www.intel.com/products/processor/core2duo/index.htm" title = "Intel Core 2 Duo Processor">Core 2 Duo</a> processor), then I say take the plunge and install <a href = "http://www.microsoft.com/windows/products/windowsvista/default.mspx" title = "Windows Vista">Vista</a>.</p>
	<p>It's worth the upgrade.</p>
	<p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/01/11/2nd-impression-of-windows-vista/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
