[perl-python]泛型等价分区 [英] [perl-python] generic equivalence partition

查看:52
本文介绍了[perl-python]泛型等价分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带列表的另一个功能练习。


这里是perl文档。我会在48小时内发布perl和翻译的

python版本。


= pod


parti(aList,equalFunc)


给出一个列表aList的n个元素,我们想要返回一个列表,它是一个从1到n的数字范围的数字,按谓词函数分区

等价等于。 (谓词函数是一个函数,

接受两个参数,并返回True或False。)


注:数学方面:有一定的数学约束

检查等价的函数。也就是说,如果a == b,

则b == a。如果a == b且b == c,那么a == c。而且,a == a。如果一个等价的

函数不满足这些,那就不一致了,基本上给了

无意义的结果。


例子:

parti([[''x'',''x'',''x'',''1''],

[''x'', ''x'',''x'',''2'',

[''x'',''x'',''x'',''2' '],

[''x'',''x'',''x'',''2''],

[''x' ',''x'',''x'',''3'',

[''x'',''x'',''x'','' 4''],

[''x'',''x'',''x'',''5''],

['' x'',''x'',''x'',''5''],sub {$ _ [0] - > [3] == $ _ [1] - > [3] })


返回

[[1],[''''',''3'',''4''],[' ''''',['''''',[''7'',''8'']];


= cut

在给出的示例中,输入列表'' lements是4

元素的列表,等价函数是如果

最后一项是相同的,则返回True。


请注意,这是一个通用函数。输入可以是

元素属于任何类型的列表。什么是parti确实是返回一个分区的

范围的数字,告诉我们哪个输入元素相当于

,根据给出的谓词。例如,在给定的

示例中,它告诉我们第2,第3,第4个元素是等价的。

它们等效于给定的谓词函数,

基本上测试他们的最后一项是否是相同的整数。 (注意,如果

我们想要将结果视为索引,那么它是基于1的索引。即

计数从1开始。)


PS如果你还没有意识到,perl中的嵌套列表/词典是一个完全痛苦的屁股。


PS注意代码sub {$ _ [0] - > [3] == $ _ [1] - > [3]}"在Perl中,lambda形式叫做




Xah
xa*@xahlee.org
http:/ /xahlee.org/PageTwo_dir/more.html

another functional exercise with lists.

Here''s the perl documentation. I''ll post a perl and the translated
python version in 48 hours.

=pod

parti(aList, equalFunc)

given a list aList of n elements, we want to return a list that is a
range of numbers from 1 to n, partition by the predicate function of
equivalence equalFunc. (a predicate function is a function that
takes two arguments, and returns either True or False.)

Note: a mathematical aspect: there are certain mathematical constraints
on the a function that checks equivalence. That is to say, if a==b,
then b==a. If a==b and b==c, then a==c. And, a==a. If a equivalence
function does not satisfy these, it is inconsistent and basically give
meaningless result.

example:
parti([[''x'',''x'',''x'',''1''],
[''x'',''x'',''x'',''2''],
[''x'',''x'',''x'',''2''],
[''x'',''x'',''x'',''2''],
[''x'',''x'',''x'',''3''],
[''x'',''x'',''x'',''4''],
[''x'',''x'',''x'',''5''],
[''x'',''x'',''x'',''5'']], sub {$_[0]->[3] == $_[1]->[3]} )

returns
[[1],[''2'',''3'',''4''],[''5''],[''6''],[''7'',''8'']];

=cut

In the example given, the input list''s elements are lists of 4
elements, and the equivalence function is one that returns True if the
last item are the same.

Note that this is a generic function. The input can be a list whose
elements are of any type. What "parti" does is to return a partitioned
range of numbers, that tells us which input element are equivalent to
which, according to the predicate given. For example, in the given
example, it tells us that the 2nd, 3rd, 4th elements are equivalent.
And they are equivalent measured by the predicate function given, which
basically tests if their last item are the same integer. (note that if
we want to view the result as indexes, then it is 1-based index. i.e.
counting starts at 1.)

PS if you didn''t realize yet, nested lists/dictionaries in perl is a
complete pain in the ass.

PS note that the code "sub {$_[0]->[3] == $_[1]->[3]}" is what''s called
the lambda form, in Perl.

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

推荐答案

_ [0] - > [3] = =
_[0]->[3] ==


_ [1] - > [3]})


返回

[[1], [ '' 2 '', '' 3 '', '' 4 ''],[ '' 5 ''],[ '' 6 ''],[ '' 7 '', '' 8 '']] ;


=削减


在给出的示例中,输入列表的元素是4
$ b $的列表b元素,等价函数是如果

最后一项是相同的,则返回True。


请注意,这是一个通用函数。输入可以是

元素属于任何类型的列表。什么是parti确实是返回一个分区的

范围的数字,告诉我们哪个输入元素相当于

,根据给出的谓词。例如,在给定的

示例中,它告诉我们第2,第3,第4个元素是等价的。

它们等效于给定的谓词函数,

基本上测试他们的最后一项是否是相同的整数。 (注意,如果

我们想要将结果视为索引,那么它是基于1的索引。即

计数从1开始。)


PS如果你还没有意识到,perl中的嵌套列表/词典是一个完全痛苦的屁股。


PS注意代码sub {
_[1]->[3]} )

returns
[[1],[''2'',''3'',''4''],[''5''],[''6''],[''7'',''8'']];

=cut

In the example given, the input list''s elements are lists of 4
elements, and the equivalence function is one that returns True if the
last item are the same.

Note that this is a generic function. The input can be a list whose
elements are of any type. What "parti" does is to return a partitioned
range of numbers, that tells us which input element are equivalent to
which, according to the predicate given. For example, in the given
example, it tells us that the 2nd, 3rd, 4th elements are equivalent.
And they are equivalent measured by the predicate function given, which
basically tests if their last item are the same integer. (note that if
we want to view the result as indexes, then it is 1-based index. i.e.
counting starts at 1.)

PS if you didn''t realize yet, nested lists/dictionaries in perl is a
complete pain in the ass.

PS note that the code "sub {


_ [0] - > [3] ==
_[0]->[3] ==


这篇关于[perl-python]泛型等价分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆