Created
August 4, 2014 07:41
-
-
Save moritz/20ee1ec36322a41b173a to your computer and use it in GitHub Desktop.
Bisect string weirdness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
my $str = slurp 'gistfile1.txt'; | |
sub test(Str $str) { | |
return not try { $str.perl; True }; | |
} | |
my $left = 0; | |
my $right = $str.chars; | |
loop { | |
say "left: $left; right: $right; test: ", test($str.substr($left, $right - $left)); | |
my $middle = ($right + $left) div 2; | |
if test($str.substr($left, $middle - $left)) { | |
$right = $middle; | |
} | |
elsif test($str.substr($middle, $right - $middle)) { | |
$left = $middle; | |
} | |
elsif ($right == $left) { | |
say "no result"; | |
last; | |
} | |
else { | |
my $least = $str.substr($left, $right - $left); | |
say "Least remaining string: <<<$least>>>\n"; | |
say $least.ords; | |
last; | |
} | |
}; | |
# vim: ft=perl6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment