1
該代碼的目的是定義一個sub 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
我其實認爲這是有效的。不過,我是perl的新手,那麼如何將poly_maps(或從此返回的內容)打印到控制檯上?我試圖打印@poly_maps,我認爲它給了我一個內存位置。 –
@TerikBrunson該代碼返回對數組的引用。你可以嘗試'我的$ ref = apply_2nd_deg_polys(...);打印「@ $ ref \ n」;'但如果任何元素是引用,那也不會很有幫助。嘗試'使用Data :: Dumper;打印Dumper($ ref);'看看裏面有什麼。 – melpomene
是的,這工作。作爲一個方面說明,你知道我怎樣才能寫出同樣的東西,但是每個推入的元素用逗號分隔嗎?謝謝(: –