Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Display a sequence of digits that contains every 2-digit pair once only. A sample program is provided.

To clarify, the output must contain all 100 combinations of two digits. It must contain '01' and '10', for example, but '010' would count for both of those pairs. '0110' would count as containing '01', '11', and '10'.

A two stroke bonus will be awarded for a sequence that is not just a (00..99).

Update: I fixed (well, bodged) my solution! Also, the two-stroke bonus is rather pointless, as other solutions are shorter anyway. My lack of imagination there, I'm afraid. I should have said "for code that does not itself contain any 2-digit pairs". Is it unfair to change the rules now? I don't think it changes the running order ATM.

for ('00'..'89'){ if (index($str,$_)<0) { if (index($str.substr($_,0,1),$_)<0) { if (index($str.substr($_,1,1),$_)<0) { $str.=$_; } else { $str.=substr($_,1,1); } } else { $str.=substr($_,0,1); } } } print $str.'90';
Output:
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990

I can get within a dozen characters of a hard-coded print statement (138 strokes):
for('00'..'89'){if($s!~/$_/){($a,$b)=/(.)(.)/;if(($s.$a)!~/$_/){if(($s.$b)!~/$_/){$s.=$_}else{$s.=$b}}else{$s.=$a}}}print$s.'90'
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 12345678
Can you do better?

Update: Fixed dumb mistake - missing '' around ..

Update: I believe that BrowserUK is in the lead so far, but I have just discovered that my solution was flawed, as it duplicated '09'.

Update: Dragonchild has also posted a correct solution, and a checker.

Update: Someone has taken the lead with an astonishing 28 characters! Who is that masked monk?

Here is some code that you can use to test your output:

$s = (<>); for('00'..'99'){print $_." missing\n" if $s!~/$_/} for('00'..'99'){print $_." duplicated\n" if index($s,$_) >= 0 and inde +x($s,$_,index($s,$_)+1) >= 0}

In reply to Friday Golf: All 2-digit combinations by PhilHibbs

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found