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 ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.