in reply to Re^2: Spreading out the elements
in thread Spreading out the elements
I think this is what merlyn means:
perl -MAlgorithm::Line::Bresenham=line -le'($A,$B)=@ARGV;@p=map{$_->[0 +]}line($A,1,1,$B);$l=$p[1];for(1..$B){if($p[$_]==$l){push@a,"A"}else{ +push@a,"B";$l=$p[$_]}}print$_ for@a' 10 40
Sorry, made it on the command line, can't be bothered to reformat it. The idea is to use ($A, 1) for the start point, (1, $B) for the end point, then pluck out the x values (projection of line onto x-axis, if that makes sense) from the list returned by line. The x values will sometimes stay the same, sometimes increase; you're interested in where it increases, that's where the Bs get plopped in.
I don't think that you'll generally have the exact right number of each element, just that you'll get a roughly even distribution of elements, given a certain ratio of A and B. Quickly experimenting about, I found that it breaks a little when the 1st number approaches the 2nd one (try 990 and 1000, for example, there will be 989 Bs instead of 990)
Assumes: two integer args on command line, the first would be your number of Bs, the second the total number of A + B.
It doesn't do things you wanted like endpoints always are A. For that, just subtract 2 from 2nd arg, and print out A at the beginning and end.
|
|---|