Re: Трава перл...фсем курить;-)

Jury Marchenko marchenko на zr.ru
Сб Сен 27 18:37:34 CDT 2003


Так, на скорую, вот вариант. Возмодна оптимизация. Длина задаётся $l.

#!/usr/bin/perl
use strict;
my $text = "A simple, easy to remember system for encoding integer amounts
can be very useful. For example, dealers at flea markets put the information
about an item on a card that they let potential buyers see. They find it
advantageous to encode the amount they originally paid for the item on the
card.\nA good system is to use a substitution code, in which each digit is
encoded by a letter. An easy to remember 10-letter word or phrase, the key,
is chosen. Every '1' in the value is replaced by the first letter of the
key, every '2' is replaced by the second letter of the key, and so on. Every
'0' is replaced by the last letter of the key. Letters that do not appear in
the key can be inserted anywhere without affecting the value represented by
the code.. This helps to make the resulting code much harder to break
(without knowing the key).\nCreate a class Substitute that contains the
method getValue that is given the Strings key and code as input and that
returns the decoded value.";
my @words = split(/ |\n/g, $text); my $l = 45;
for (my $i=0;$i<=$#words;$i++) {
    my $string = $words[$i];
    while (length($string)<$l) {
        $i++;
        if (!$words[$i]) { $string = &add_dot($string, $l); }
        if ((length($string) + length(" " . $words[$i]))<=$l) { $string.="
$words[$i]"; } else { $i--; $string = &add_dot($string, $l); last; }
    }
    print "$string\n";
}
sub add_dot { my ($string, $l) = @_; while (length ($string) < $l) { $string
= " $string"; } return $string; }



С уважением, Марченко Юрий Иванович,
редактор отдела оперативной информации,
ЗАО КЖИ "За Рулём"
URL: http://www.zr.ru ; http://www.za.ru ; E-mail: marchenko на zr.ru
Телефоны: +7 (095) 208-00-70 +7 (926) 203-52-90

----- Original Message -----
From: "LaVei" <lavei на mail.ru>
To: <samara-pm на mail.pm.org>
Sent: Thursday, September 25, 2003 15:30
Subject: Трава перл...фсем курить;-)


> Как решить в одну строку это, я себе пока не представляю...
> поэтому если участники не против то критерием будет Benchmark?;-)
>
> Задача :
> Написать программу на perl выводящую текст в виде
> прямоугольника с
> заданной шириной.
> До заданной ширины строку следует дополнять пробелами,
> начиная с левой её
> части. Параметр, определяющий ширину результирующего
> прямоугольника,
> гарантированно больше, чем самое длинное слово,
> встречающееся в тексте.
>
> Текст
> "A simple, easy to remember system for encoding integer
> amounts can be very
> useful. For example, dealers at flea markets put the
> information about an item
> on a card that they let potential buyers see. They find it
> advantageous to
> encode the amount they originally paid for the item on the
> card.\nA good system
> is to use a substitution code, in which each digit is
> encoded by a letter. An
> easy to remember 10-letter word or phrase, the key, is
> chosen. Every '1' in the
> value is replaced by the first letter of the key, every
> '2' is replaced by the
> second letter of the key, and so on. Every '0' is replaced
> by the last letter of
> the key. Letters that do not appear in the key can be
> inserted anywhere without
> affecting the value represented by the code.. This helps
> to make the resulting
> code much harder to break (without knowing the
> key).\nCreate a class Substitute
> that contains the method getValue that is given the
> Strings key and code as
> input and that returns the decoded value."
>
> Данил
>




Подробная информация о списке рассылки Samara-pm