Rep-string: Difference between revisions

Content added Content deleted
(→‎version 1: why incorrect??)
(→‎{{header|Perl 6}}: update to task, add bitwise numeric solution based on XOR)
Line 134: Line 134:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
<lang perl6>for <1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1> {
{{update|Perl 6|It is '00' not '10' for the test.}}
<lang perl6>for <1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 10 1> {
.say;
.say;
if /^ (.+) $0+ (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
if /^ (.+) $0+ (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
Line 172: Line 171:
1
1


00
10
0
(no repeat)


1
1
(no repeat)</pre>
(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;
}
}


for '1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1'.words {
if repstr $_ -> $rep {
say $_;
say ' ' x $rep, .substr($rep,$rep), "\n";
}
else {
say "$_\n (no repeat)\n";
}
}</lang>


=={{header|Python}}==
=={{header|Python}}==