[QUIZ] Perl 'Easy' Quiz of the Week #2005-2
Dan Sanderson <[email protected]> Wed, 9 Feb 2005 09:30:52 -0800 (PST)
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
IMPORTANT: Please do not post solutions, hints, or other spoilers
until at least 60 hours after the date of this message.
Thanks.
Write a subroutine called print_list that takes a number indicating
the width of the display as a number of characters, and a list of
strings, and prints the strings in a sorted columnar display, using as
many columns that will fit in the display for the given list and as few
rows as possible.
The longest string in one column should appear exactly two spaces away
from the beginning of the next column. (The columns are not
necessarily the same width.)
Each column should include consecutive elements in the sorted list,
such that the item following the last item in column 1 appears at the
top of column 2, and similarly for the remaining columns.
For example, given:
@strings = qw(Jonathan Sam Abby Daniel Julia Terrence Constance Al);
print_list(30, @strings) prints:
Abby Daniel Sam
Al Jonathan Terrence
Constance Julia
print_list(40, @strings) prints:
Abby Constance Jonathan Sam
Al Daniel Julia Terrence
print_list(80, @strings) prints:
Abby Al Constance Daniel Jonathan Julia Sam Terrence
And print_list(1, @strings) prints:
Abby
Al
Constance
Daniel
Jonathan
Julia
Sam
Terrence
For a real world example of a similar display, see the Unix/Linux command,
'ls'. For an example of how to determine the actual width of a terminal
window in characters (not required for this quiz, but good to know), see
the Perl Cookbook, 1st or 2nd edition, recipe 15.4.
-- Dan