#!/usr/local/bin/perl -w # binarize.p , from DS.p - read in integer list # and write out a binary vector that describes the set sub bynumber { $a <=> $b; } $v=0; $ds=0; # whether to check the provided integers are indeed a D.S. $S = 0 ; # the set size from which points are drawn 0..S-1 eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if ($S==0) { print STDERR "usage:\n binarize.p S=100 < file\n" ; print STDERR "usage:\n binarize.p S=100 ds=100 v=1 < file\n" ; print STDERR "usage:\n echo \"1 2 9 10\" | binarize.p S=10\n" ; exit(0) ; } while ( <> ) { next if (/^\#*\s*\n/) ; s/^\s+// ; s/[^\s\d]/ /g ; # remove non-numbers @ind=split ; # solely for $ds checking later on # add zero to the list, and S+1. $SS = $S+1 ; $_ = "0 ".$_." $SS" ; @s = split ; $I = $#s ; printf STDERR " I = %d\n" ,$I-1; $ans="" ; for ( $i = 1; $i <= $I ; $i ++ ) { print "i=$i s[i] = $s[$i]\n" if($v); $delta = $s[$i] - $s[$i-1] -1 ; # this is the number of 0s to print $ans .= "0 "x$delta ; if ( $i < $I ) { $ans .= "1 " ; } } print $ans."\n" ; } if($ds) { print STDERR "# check for difference property\n" ; $clashes = 0 ; undef(@taken) ; $difflist="" ; for ( $i = 0 ; $i <= $#ind ; $i ++ ) { $ni = $ind[$i] ; for ( $j = $i+1 ; $j <= $#ind ; $j ++ ) { $nj = $ind[$j] ; print STDERR "finding diff $i $j\n" if($v>2) ; $diff = $nj - $ni ; if ( $taken{$diff} ) { $clashes ++ ; print STDERR "$i.$j: $ni.$nj +($diff) clashes with $taken{$diff}\n"; } else { $taken{$diff}="$ni.$nj"; } $difflist .= "$diff " ; $diff2 = $S - $diff ; if ( $diff2 != $diff ) { $diff=$diff2 ; if ( $taken{$diff} ) { $clashes ++ ; print STDERR "$i.$j: $nj.$ni -($diff) clashes with $taken{$diff}\n"; } else { $taken{$diff}="$nj.$ni"; } $difflist .= "$diff " ; } } } print STDERR "$clashes clashes\n"; if($v) { print join (" ", sort bynumber keys(%taken)) ; print "\n" ;} }