in reply to Even column spreading of data

Another solution
sub spread { return ($_[0] % $_[1])?join("-", ((int($_[0] / ($_[1] - 1))) x ($_ +[1]-1), ($_[0] % ($_[1] - 1)))):join("-", (int($_[0] / $_[1])) x $_[1 +]); } print spread(19,4); #test first branch of ?: print spread(25,5); #test second branch of ?:
Update: I read FamousLongAgo's reply, and thanks for testing my code.

However I don't think there is any problem, although in the 22/7 case, that 4 in the last column is greater than 3's in the rest columns. This really depends on how you define "evenly", and what "remainder goes to last column" means.

My understanding is that other than the last column, other columns should be exactly the same; Whatever left goes to last column. It is never said anywhere in his post, that the last column cannot be greater than the rest.

However the value of the last column must be less than $col - 1 (number of columns - 1), if it is greater than or equals to "number of columns - 1", then it is really not even, and you really should redistribute.

As for whether the code is reable, I don't think it is too difficult to understand, as obviously you understood, and tried to reformat it, but this is more perlier ;-)

Replies are listed 'Best First'.
Re: Re: Even column spreading of data
by FamousLongAgo (Friar) on Dec 22, 2002 at 02:12 UTC
    Same code ( what is this, Lisp? ), reformatted to be more readable and do a sanity check for the zero-columns case. Note that it doesn't seem to do exactly what the questioner asked - see the 22/7 case:
    #!/usr/bin/perl -w use strict; while (<DATA> ) { my ( $items, $cols ) = split; print "Spread for $items, $cols = ", spread( $items, $cols ), "\n"; } sub spread { my ($a, $cols) = @_; $a ||= 0; return unless $cols > 0; # avoid nasty surprises return $a if $cols == 1; #trivial case if ( $a % $cols) { return join '-', ( (int( $a / ($cols - 1) )) x ( $cols - 1) +, ( $a % ($cols - 1))); } else { return join '-', (int ( $a / $cols)) x $cols; } } __DATA__ 19 3 20 5 22 7 2 47 0 9 899 0
    Results:
    Spread for 19, 3 = 9-9-1 Spread for 20, 5 = 4-4-4-4-4 Spread for 22, 7 = 3-3-3-3-3-3-4 Spread for 2, 47 = 0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0 +-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-2 Spread for 0, 9 = 0-0-0-0-0-0-0-0-0 Spread for 899, 0 =
Re: Re: Even column spreading of data
by Kage (Scribe) on Dec 22, 2002 at 22:37 UTC
    I'm not sure of any way I can explain what I mean any further than I did. Basically, what I want is the first (Columns_Number - 1) columns to have the same number of digits in them, and the last column will have the remainder, which would be <= the total number to spread. The only real easy way I can think of to explain it would be an example of a total of 19, spread over 5 columns, which would turn out as 4-4-4-4-3.

    That is going to be as best I can possibly explain "evenly" and "remainder goes to last column" in my own words. Sorry if this leaves any gaping holes in my request. =/
    “A script is what you give the actors. A program is what you give the audience.” ~ Larry Wall