Skip to content

Instantly share code, notes, and snippets.

@masak
Created September 14, 2011 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/1216010 to your computer and use it in GitHub Desktop.
Save masak/1216010 to your computer and use it in GitHub Desktop.
Brute force de Bruijn
use 5.010;
use warnings;
use strict;
my $N = 10;
my $L = 4;
my $num_possible_combs = $N ** $L;
my $string = join '', map { int($N * rand) } 1..$L-1;
my %combs;
DIGIT:
while (keys(%combs) < $num_possible_combs) {
my @all_combs = ('');
for my $add_length (1..$L) {
sub pick_star {
map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, rand()] } @_
}
@all_combs = map { my $s = $_; map { $s.$_ } 0..$N-1 } @all_combs;
my @numbers_in_some_order = pick_star(@all_combs);
for my $addition (@numbers_in_some_order) {
my $resulting_comb = substr($string, -$L+$add_length) . $addition;
if (!exists $combs{$resulting_comb}) {
++$combs{$resulting_comb};
$string .= $addition;
next DIGIT;
}
}
}
}
say $string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment