设为首页 加入收藏

TOP

Ruby数组(2):数组方法详细整理(六)
2019-08-15 00:10:23 】 浏览:565
Tags:Ruby 数组 方法 详细 整理
t;dog" ary = %w[albatross dog horse] ary.max(2) # ["horse", "dog"] ary.max(2){|a, b| a.length <=> b.length} # ["albatross","horse"]
ary = %w(albatross dog horse)
a = ary.max do |a, b|
  x=a.length
  y=b.length
  y <=> x
end


permutation()和combination()

permutation {|p| block} → ary
permutation → Enumerator
permutation(n) {|p| block} → ary
permutation(n) → Enumerator

combination(n) {|c| block} → ary
combination(n) → Enumerator

permutation()对数组的元素进行排列,返回排列后的各数组。

  • 当指定参数n时,则对所有n个元素作排列
  • 当不指定参数n时,则n默认为数组长度,即对所有元素作排列

combination()对数组作n个元素的组合。

注意,排列、组合的顺序不作任何保证。

关于排列和组合的区别:

  • 排列:从n个不同的元素中,取r个不重复的元素,按次序排列,称为从n个中取r个的无重复排列
  • 组合:从n个不同的元素中,取r个不重复的元素,组成一个子集,而不考虑其元素的顺序,称为从n个中取r个的无重组和

看下面的示例即可理解:

# permutation()作排列操作
a = [1, 2, 3]
a.permutation.to_a    # [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(1).to_a # [[1],[2],[3]]
a.permutation(2).to_a # [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.permutation(3).to_a # [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(0).to_a # [[]] # one permutation of length 0
a.permutation(4).to_a # []   # no permutations of length 4
# combination()作组合操作
a = [1, 2, 3, 4]
a.combination(1).to_a  # [[1],[2],[3],[4]]
a.combination(2).to_a  # [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
a.combination(3).to_a  # [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
a.combination(4).to_a  # [[1,2,3,4]]
a.combination(0).to_a  # [[]] # one combination of length 0
a.combination(5).to_a  # []   # no combinations of length 5

当使用了语句块时,每个排列后的子数组将传递给语句块的变量。

a = [1, 2, 3, 4]
a.combination(3) {|x| p x << "z" }
## 输出:
## [1, 2, 3, "z"]
## [1, 2, 4, "z"]
## [1, 3, 4, "z"]
## [2, 3, 4, "z"]


repeated_combination()和repeated_permutation()

repeated_combination(n) {|c| block} → ary
repeated_combination(n) → Enumerator

repeated_permutation(n) {|p| block} → ary
repeated_permutation(n) → Enumerator

重复n个数组自身,并对这n个数组进行排列操作、组合操作。看示例即可明白。

# repeated_combination()
a = [1, 2, 3]
a.repeated_combination(1).to_a  # [[1], [2], [3]]
a.repeated_combination(2).to_a  # [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
a.repeated_combination(3).to_a  # [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
                                #    [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
a.repeated_combination(4).to_a  # [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],
                                #    [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
                                #    [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]
a.repeated_combination(0).to_a  # [[]] # one combination of length 0
# repeated_permutation()
a = [1, 2]
a.repeated_permutation(1).to_a  # [[1], [2]]
a.repeated_permutation(2).to_a  # [[1,1],[1,2],[2,1],[2,2]]
a.repeated_permutation(3).to_a  # [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
                                #    [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
a.repeated_permutation(0).to_a  # [[]] # one permutation of length 0

product()

product(other_ary, ...) → new_ary
product(other_ary, ...) {|p| block} → ary

将数组和other_ary的各元素进行组合后返回。

如果使用了语句块,则每个组合后的子数组都传递给语句块,并返回数组自身(即a.product() {}时返回a)。

[1,2,3].product([4,5])     # [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
[1,2].product([1,2])       # [[1,1],[1,2],[2,1],[2,2]]
[1,2].product([3,4],[5,6]) # [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
                           #    [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
[1,2].product()            # [[1],[2]]
[1,
首页 上一页 3 4 5 6 7 8 9 下一页 尾页 6/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ruby中to_s和to_str、to_i和to_in.. 下一篇Python之路【第四篇】:Python基础..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目