The "perfect shuffles" puzzle (solved in Haskell)

Posted by Tom Moertel Thu, 23 Mar 2006 21:51:00 GMT

I ran across a fun programming puzzle (via Raganwald):

Given a deck of n unique cards, cut the deck i cards from top and perform a perfect shuffle. A perfect shuffle begins by putting down the bottom card from the top portion of the deck followed by the bottom card from the bottom portion of the deck followed by the next card from the top portion, etc., alternating cards until one portion is used up. The remaining cards go on top. The problem is to find the number of perfect shuffles required to return the deck to its original order. Your function should be declared as:

static long shuffles(int nCards,int iCut);

Please send the result of shuffles(1002,101) along with your program and your resume to ‘resume’ at nextag.com.

It’s a fun problem, so give it a try before reading on.

Warning: small spoilers ahead

Read more...

Posted in , ,
Tags , ,
3 comments
no trackbacks
Reddit Delicious

Talk: Embedded domain-specific languages for Perl

Posted by Tom Moertel Tue, 14 Mar 2006 22:39:00 GMT

Last week I gave a brief talk for the Pittsburgh Perl Mongers about embedding domain-specific languages into Perl. The slides from the talk are now available: Embedding an XHTML template language into Perl.

Title slide from my talk on embedding domain-specific languages into Perl

Posted in , ,
Tags , , ,
no comments
no trackbacks
Reddit Delicious

Good stuff: Tab Mix Plus for Firefox

Posted by Tom Moertel Fri, 03 Mar 2006 15:13:00 GMT

It’s a real forehead-slapper that I only now started using Tab Mix Plus, a Firefox Extension that lets you completely customize Firefox’s tab behavior. It has about a billion settings, but the defaults are carefully chosen so that it does good stuff without needing adjustment. Unless you’re a goofball, you will notice an immediate improvement in browsing.

Among the more useful features are:

  • Session management – If you quit Firefox (or it crashes), Tab Mix Plus can restore your session when you restart.
  • Sensible forking – If you “fork” a tab, the new tab appears next to its parent, not at the end of the tab bar. As a result, related tabs are clustered, not scattered, and the ordering of tabs corresponds with your brain’s picture of the session. (Also, you can drag tabs, should you want to impose another ordering.)
  • I’m-new highlights – Tabs you fork into the background are highlighted to let you know they contain new, unseen content. This is great for power sessions when you otherwise couldn’t remember which tabs you’ve already seen.
  • Locking/Protecting – Lock a tab, and the browser won’t change it. If you click a link or do anything else that would replace the contents of the tab, it will fork a new tab instead. Protect a tab, and you can’t accidentally close it; you must unprotect it first.

If you use Firefox, you need to check out Tab Mix Plus. It’s good stuff.

Posted in
1 comment
no trackbacks
Reddit Delicious

Finding duplicate words in writing: a handy Perl script

Posted by Tom Moertel Wed, 01 Mar 2006 20:17:00 GMT

The human mind subconsciously corrects mistakes while we read. This mental feature has a nasty drawback for writers: when proofreading, we often miss our own mistakes.

One of the most common writing mistakes that our brains stealthily correct is the the duplicate word problem. For example, the duplicate the words in the previous sentence. Did you catch them?

If so, don’t be too proud of your accomplishment. It is easier to see errors in others’ writing than in your own. Your brain is attuned to your natural writing patterns and much more likely to repair your mistakes without your knowing.

To overcome this problem, some writers recommend reading your work backward, but I think computers are a more practical solution.

Here’s the Perl script that I use to spot duplicate words:

#!/usr/bin/perl -n00
# dupwords.pl - find duplicate words in the input stream

print "$ARGV: para $.: ($1)\n" 
    while /(\b(\w+)\b\s+\b\2\b)/sg;

I use this script from Emacs via shell-command-on-region. I also use it from the command line to find duplicate-word errors in batch:

find . -name '*.txt' | xargs dupwords.pl

The duplicate-words problem is a favorite for programming cookbooks, so if you don’t like my recipe (or Perl), you have many other options.

Posted in ,
Tags , ,
no comments
no trackbacks
Reddit Delicious