perlmeditation
PhilHibbs
Display a sequence of digits that contains every 2-digit pair once only. A sample program is provided.<p>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'.<p>A two stroke bonus will be awarded for a sequence that is not just a <tt>(00..99)</tt>.<p><strong>Update:</strong> 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.
<code>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';
</code>
Output:
<br><font size=-1><tt>00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990</tt></font>
<p>I can get within a dozen characters of a hard-coded print statement (138 strokes):
<table bgcolor=black><tr><td><font size=-1 color=black><tt>for('00'..'89'){if($s!~/$_/){($a,$b)=/(.)(.)/;if(($s.$a)!~/$_/){if(($s.$b)!~/$_/){$s.=$_}else{$s.=$b}}else{$s.=$a}}}print$s.'90'</tt></font></td></tr><tr><td><font size=-1><tt>123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 12345678</tt></font></td></tr></table>Can you do better?<p><strong>Update:</strong> Fixed dumb mistake - missing '' around ..<p><strong>Update:</strong> I believe that [BrowserUK] is [id://294421|in the lead] so far, but I have just discovered that my solution was flawed, as it duplicated '09'.<p><strong>Update:</strong> [Dragonchild] has also posted [id://294422|a correct solution], and a checker.
<p><strong>Update:</strong> [Anonymous Monk|Someone] has [id://294531|taken the lead] with an astonishing 28 characters! Who is that masked monk?
<p>Here is some code that you can use to test your output:
<code>$s = (<>);
for('00'..'99'){print $_." missing\n" if $s!~/$_/}
for('00'..'99'){print $_." duplicated\n" if index($s,$_) >= 0 and index($s,$_,index($s,$_)+1) >= 0}
</code>