Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
0c3ee0ba
Commit
0c3ee0ba
authored
Oct 20, 2017
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Array#{permutation,combination}.
parent
f2394859
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
125 additions
and
0 deletions
+125
-0
mrbgems/mruby-array-ext/mrbgem.rake
mrbgems/mruby-array-ext/mrbgem.rake
+1
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+96
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+28
-0
No files found.
mrbgems/mruby-array-ext/mrbgem.rake
View file @
0c3ee0ba
...
...
@@ -2,4 +2,5 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec|
spec
.
license
=
'MIT'
spec
.
author
=
'mruby developers'
spec
.
summary
=
'Array class extension'
spec
.
add_test_dependency
'mruby-enumerator'
,
core:
'mruby-enumerator'
end
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
0c3ee0ba
...
...
@@ -808,4 +808,100 @@ class Array
n
end
end
##
# call-seq:
# ary.permutation { |p| block } -> ary
# ary.permutation -> Enumerator
# ary.permutation(n) { |p| block } -> ary
# ary.permutation(n) -> Enumerator
#
# When invoked with a block, yield all permutations of length +n+ of the
# elements of the array, then return the array itself.
#
# If +n+ is not specified, yield all permutations of all elements.
#
# The implementation makes no guarantees about the order in which the
# permutations are yielded.
#
# If no block is given, an Enumerator is returned instead.
#
# Examples:
#
# 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
def
permutation
(
n
=
self
.
size
,
&
block
)
size
=
self
.
size
return
to_enum
(
:permutation
,
n
)
unless
block
return
if
n
>
size
if
n
==
0
yield
[]
else
i
=
0
while
i
<
size
result
=
[
self
[
i
]]
if
n
-
1
>
0
ary
=
self
[
0
...
i
]
+
self
[
i
+
1
..-
1
]
ary
.
permutation
(
n
-
1
)
do
|
c
|
yield
result
+
c
end
else
yield
result
end
i
+=
1
end
end
end
##
# call-seq:
# ary.combination(n) { |c| block } -> ary
# ary.combination(n) -> Enumerator
#
# When invoked with a block, yields all combinations of length +n+ of elements
# from the array and then returns the array itself.
#
# The implementation makes no guarantees about the order in which the
# combinations are yielded.
#
# If no block is given, an Enumerator is returned instead.
#
# Examples:
#
# 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
def
combination
(
n
,
&
block
)
size
=
self
.
size
return
to_enum
(
:combination
,
n
)
unless
block
return
if
n
>
size
if
n
==
0
yield
[]
elsif
n
==
1
i
=
0
while
i
<
size
yield
[
self
[
i
]]
i
+=
1
end
else
i
=
0
while
i
<
size
result
=
[
self
[
i
]]
self
[
i
+
1
..-
1
].
combination
(
n
-
1
)
do
|
c
|
yield
result
+
c
end
i
+=
1
end
end
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
0c3ee0ba
...
...
@@ -381,3 +381,31 @@ assert("Array#slice!") do
assert_equal
(
i
,
[
1
,
2
,
3
])
assert_equal
(
j
,
nil
)
end
assert
(
"Array#permutation"
)
do
a
=
[
1
,
2
,
3
]
assert_equal
([[
1
,
2
,
3
],[
1
,
3
,
2
],[
2
,
1
,
3
],[
2
,
3
,
1
],[
3
,
1
,
2
],[
3
,
2
,
1
]],
a
.
permutation
.
to_a
)
assert_equal
([[
1
],[
2
],[
3
]],
a
.
permutation
(
1
).
to_a
)
assert_equal
([[
1
,
2
],[
1
,
3
],[
2
,
1
],[
2
,
3
],[
3
,
1
],[
3
,
2
]],
a
.
permutation
(
2
).
to_a
)
assert_equal
([[
1
,
2
,
3
],[
1
,
3
,
2
],[
2
,
1
,
3
],[
2
,
3
,
1
],[
3
,
1
,
2
],[
3
,
2
,
1
]],
a
.
permutation
(
3
).
to_a
)
assert_equal
([[]],
a
.
permutation
(
0
).
to_a
)
assert_equal
([],
a
.
permutation
(
4
).
to_a
)
end
assert
(
"Array#combination"
)
do
a
=
[
1
,
2
,
3
,
4
]
assert_equal
([[
1
],[
2
],[
3
],[
4
]],
a
.
combination
(
1
).
to_a
)
assert_equal
([[
1
,
2
],[
1
,
3
],[
1
,
4
],[
2
,
3
],[
2
,
4
],[
3
,
4
]],
a
.
combination
(
2
).
to_a
)
assert_equal
([[
1
,
2
,
3
],[
1
,
2
,
4
],[
1
,
3
,
4
],[
2
,
3
,
4
]],
a
.
combination
(
3
).
to_a
)
assert_equal
([[
1
,
2
,
3
,
4
]],
a
.
combination
(
4
).
to_a
)
assert_equal
([[]],
a
.
combination
(
0
).
to_a
)
assert_equal
([],
a
.
combination
(
5
).
to_a
)
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment