Rep-string: Difference between revisions

→‎{{header|Perl 6}}: use double-struck digits for more compact notation
(→‎version 1: remove incorrect incorrect marking :))
(→‎{{header|Perl 6}}: use double-struck digits for more compact notation)
Line 134:
=={{header|Perl 6}}==
<lang perl6>for <1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1> {
if /^ (.+) $0+: (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
.say;
my $rep = $0.chars;
if /^ (.+) $0+ (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
say .substr(0,$rep), .substr($rep,$rep).trans('01' => '𝟘𝟙'), .substr($rep*2);
say ' ' x $0.chars, "$0\n";
}
else {
say "$_ (no repeat)\n";
}
}</lang>
{{out}}
<pre>10011𝟙𝟘𝟘𝟙𝟙
<pre>1001110011
1110𝟙𝟙𝟙𝟘11
10011
001𝟘𝟘𝟙0010
 
1010𝟙𝟘𝟙𝟘10
1110111011
11111𝟙𝟙𝟙𝟙𝟙
1110
0100101101 (no repeat)
 
010𝟘𝟙𝟘0
0010010010
101 (no repeat)
001
1𝟙
 
0𝟘
1010101010
1 (no repeat)</pre>
1010
 
1111111111
11111
 
0100101101
(no repeat)
 
0100100
010
 
101
(no repeat)
 
11
1
 
00
0
 
1
(no repeat)</pre>
Here's a technique that relies on the fact that XORing the shifted binary number should set all the lower bits to 0 if there are repeats. (The cool thing is that shift will automatically throw away the bits on the right that you want thrown away.) This produces the same output as above.
<lang perl6>sub repstr(Str $s) {
my $bits = :2($s);
for reverse 1 .. $s.chars div 2 -> $left {
my $right = $s.chars - $left;
return $left if $bits +^ ($bits +> $left) == $bits +> $right +< $right;
}
}
Line 187 ⟶ 166:
for '1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1'.words {
if repstr $_ -> $rep {
say .substr(0,$rep), .substr($rep,$rep).trans('01' => '𝟘𝟙'), .substr($rep*2);
say $_;
say ' ' x $rep, .substr($rep,$rep), "\n";
}
else {
say "$_\n (no repeat)\n";
}
}</lang>
Anonymous user