De Bruijn sequences: Difference between revisions

→‎{{header|Perl 6}}: Randomize the sequence
(→‎{{header|Perl 6}}: Rewrote, rearranged a bit, DRY, Deviate slightly from task spec.)
(→‎{{header|Perl 6}}: Randomize the sequence)
Line 217:
=={{header|Perl 6}}==
{{works with|Rakudo|2019.07.1}}
Deviates very slightly from the task spec. (ReplacesGenerates a randomized de Bruijn sequence and replaces the 4444th characterdigit with a 5the digit plus 1 mod 10 rather than a '.'), mostly so it can demonstrate detection of extra PINs as well as missing ones.
 
<lang perl6># Generate the sequence
Line 231:
}
}
 
$seq ~= '000';
$seq = $seq.comb.list.rotate((^10000).pick).join;
 
$seq ~= $seq.substr(0,3);
 
sub check ($seq) {
my %chk;
for ^($seq.chars) { %chk{$seq.substr( $_, 4 )}++ }
put 'Missing: ', (^9999).grep( { not %chk{ .fmt: '%04d' } } ).fmt: '%04d';
put 'Extra: ', %chk.grep( *.value > 1 )».key.sort.fmt: '%04d';
}
 
Line 253 ⟶ 256:
check $seq.flip;
 
putmy "\nReplacing$digit the= 4444th digit, ({$seq.substr(4443,1)}) with 5";
put "\nReplacing the 4444th digit, ($digit) with { ($digit += 1) %= 10 }";
put "Incorrect 4 digit PINs in the revised sequence:";
$seq.substr-rw(4443,1) = 5$digit;
check $seq;</lang>
{{out|Sample output}}
</lang>
{{out}}
<pre>de Bruijn sequence length: 10003
 
First 130 characters:
4558455945654566456745684569457545764577457845794585458645874588458945954596459745984599464647464846494655465646574658465946654666
0000100020003000400050006000700080009001100120013001400150016001700180019002100220023002400250026002700280029003100320033003400350
 
Last 130 characters:
5445644574458445944654466446744684469447544764477447844794485448644874488448944954496449744984499454546454745484549455545564557455
6898689969697769786979698769886989699769986999777787779778877897798779978787978887889789878997979887989799879998888988998989999000
 
Incorrect 4 digit PINs in this sequence:
Line 275 ⟶ 278:
Extra:
 
Replacing the 4444th digit, (41) with 52
Incorrect 4 digit PINs in the revised sequence:
Missing: 14590961 45911096 58146109 81459610
Extra: 15590962 55912096 58156209 81559620</pre>
 
=={{header|REXX}}==
10,333

edits