给出3元组的二次多项式

时间:2015-09-27 00:17:02

标签: perl

此代码的目的是定义一个子apply_2nd_deg_polys,它取一个匿名的第二学位列表 多项式和数字列表,并将每个多项式应用于列表中的每个数字。 任何帮助表示赞赏! (:

my @coeffs = ([1,2,3], [4,5,6]);
my @polys = gen_2nd_deg_polys(@coeffs);
my @numbers = (1..5);
my @poly_maps = apply_2nd_deg_polys2(\@polys, \@numbers);

输出应为:

[('1x^2 + 2x + 3 at x = 1 is ', 6), ('4x^2 + 5x + 6 at x = 1 is ', 15), ('1x^2 + 2x + 3 at x = 2 is ', 11), ('4x^2 + 5x + 6 at
x = 2 is ', 32), ('1x^2 + 2x + 3 at x = 3 is ', 18), ('4x^2 + 5x + 6 at x = 3 is ', 57), ('1x^2 + 2x + 3 at x = 4 is ', 27),
('4x^2 + 5x + 6 at x = 4 is ', 90), ('1x^2 + 2x + 3 at x = 5 is ', 38), ('4x^2 + 5x + 6 at x = 5 is ', 131)]

这是我到目前为止的代码......

sub apply_2nd_deg_polys{
    my @list = @_;
    my @polys = @{%_[0]}; my @numbers = @{@_[1]};
    push @list, $polys[0][i];
    push @list, $polys[i][0];
    return @list;

}

这是我的工作Python变体:

def apply_2nd_deg_polys(polys,numbers):
    newlist = []
    for number in numbers:
        newlist.append(polys[0](number))
        newlist.append(polys[1](number))
    return newlist

1 个答案:

答案 0 :(得分:1)

忽略你问题中的几乎所有内容,将Python代码翻译成Perl(或许过于文字)是:

sub apply_2nd_deg_polys {
    my ($polys, $numbers) = @_;
    my $newlist = [];
    for my $number (@$numbers) {
        push @$newlist, $polys->[0]->($number);
        push @$newlist, $polys->[1]->($number);
    }
    return $newlist;
}