Here is something for the monks to mull over on the weekend. It is not a perl specific thing but I fear that large amounts of perl ingenuity will be required to solve it.

Q: What is the shortest string that contains all the numbers from 0000 to 9999.

For example: '012345678' contains 0123, 1234, 2345, 3456, 4567, 5678.

Is it possible to create a string which contains all the numbers and no duplicates? To date my best achievement is:

Length is: '10427'. Missing count: '0' Duplicate count: '424'

Please feel free to use this function to test any strings produced:

# Will return an analysis of the string. # Usage: print analyse( $string ); sub analyse { # Make an array from the number and create hash. my $number = shift; my @string = split //, $number; my %count = (); # Work over the number adding values to the hash. while (1) { my $candidate = $string[0].$string[1].$string[2].$string[3]; $count{$candidate}++; # Get rid of first element of the array. shift @string; # Break out at the end of array. last if scalar @string < 4; } # Tally up the number of duplicates and missing numbers. my $duplicates = 0; my $missing = 0; for ( 0 .. 9999 ) { my $k = 0 x ( 4 - length $_ ) . $_; my $v = $count{$k}; unless ( defined $v ) { $missing++; next; } $duplicates += ( $v - 1 ) if $v > 1; } # Create the summary. my $text; $text .= " Length is: " . length($number) . "\n"; $text .= " Missing count: " . $missing . "\n"; $text .= "Duplicate count: " . $duplicates . "\n\n"; return $text; }

My thoughts on this puzzle are that you can go about it one of two ways: add unused numbers to the end of the string (technique used for results above) or remove duplicates. Probably the best is a combination of both.

Update: Made a change to the analysis code - pesky CVS caught me out.

--tidiness is the memory loss of environmental mnemonics


In reply to Shortest string containing all from 0000 to 9999 challenge. by EvdB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.