mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* array.c (rb_ary_permutation): implementation contributed from
David Flanagan. [ruby-core:12344] * array.c (rb_ary_combination): RDoc update to clarify. a patch from David Flanagan. [ruby-core:12344] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
88f570d9ae
commit
04bc87e582
3 changed files with 111 additions and 36 deletions
|
@ -1177,14 +1177,6 @@ class TestArray < Test::Unit::TestCase
|
|||
assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
|
||||
end
|
||||
|
||||
# def test_permutation
|
||||
# assert_equal(@cls[[]], @cls[1,2,3].permutation(0).to_a)
|
||||
# assert_equal(@cls[[1],[2],[3]], @cls[1,2,3].permutation(1).to_a)
|
||||
# assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], @cls[1,2,3].permutation(2).to_a)
|
||||
# assert_equal(@cls[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], @cls[1,2,3].permutation(3).to_a)
|
||||
# assert_equal(@cls[], @cls[1,2,3].permutation(4).to_a)
|
||||
# end
|
||||
|
||||
def test_combination
|
||||
assert_equal(@cls[[]], @cls[1,2,3,4].combination(0).to_a)
|
||||
assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a)
|
||||
|
@ -1199,4 +1191,20 @@ class TestArray < Test::Unit::TestCase
|
|||
@cls[1,2,3].product([4,5]))
|
||||
assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @cls[1,2].product([1,2]))
|
||||
end
|
||||
|
||||
def test_permutation
|
||||
a = @cls[1,2,3]
|
||||
assert_equal(@cls[[]], a.permutation(0).to_a)
|
||||
assert_equal(@cls[[1],[2],[3]], a.permutation(1).to_a.sort)
|
||||
assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
|
||||
a.permutation(2).to_a.sort)
|
||||
assert_equal(@cls[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
|
||||
a.permutation(3).sort.to_a)
|
||||
assert_equal(@cls[], a.permutation(4).to_a)
|
||||
assert_equal(@cls[], a.permutation(-1).to_a)
|
||||
assert_equal("abcde".each_char.to_a.permutation(5).sort,
|
||||
"edcba".each_char.to_a.permutation(5).sort)
|
||||
assert_equal(@cls[].permutation(0).to_a, @cls[[]])
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue