http://qs1969.pair.com?node_id=181383


in reply to Re: •Re: Puzzle: need a more general algorithm
in thread Puzzle: need a more general algorithm

As we discussed in that meeting, here's the code snippet I was thinking about to generate the binary strings:
print map "$_\n", strings_for(6, 4); sub strings_for { my ($cats, $cols) = @_; $cats--; $cols--; my @ret; for (0..(1 << $cats) - 1) { my $bitstring = substr(unpack("B*", pack "N", $_), -$cats); next unless $bitstring =~ tr/1// == $cols; push @ret, $bitstring; } @ret; }

-- Randal L. Schwartz, Perl hacker