Ruby | Array repeated_permutation() function
Last Updated :
05 Dec, 2019
Array#repeated_permutation() : repeated_permutation() is a Array class method which returns all repeated permutations of length n of the elements of the array, then return the array itself.
Syntax: Array.repeated_permutation()
Parameter: Array
Return: all repeated permutations of length n of the elements of the array, then return the array itself.
Example #1 :
Ruby
# Ruby code for repeated_permutation() method
# declaring array
a = [18, 22, 33, nil, 5, 6]
# declaring array
b = [1, 4, 1, 1, 88, 9]
# declaring array
c = []
# repeated_permutation method example
puts "repeated_permutation() method form :
#{a.repeated_permutation(2).to_a}\n\n"
puts "repeated_permutation() method form :
#{b.repeated_permutation(1).to_a}\n\n"
puts "repeated_permutation() method form :
#{c.repeated_permutation(4).to_a}\n\n"
Output :
repeated_permutation() method form :
[[18, 18], [18, 22], [18, 33], [18, nil], [18, 5], [18, 6], [22, 18], [22, 22], [22, 33], [22, nil], [22, 5], [22, 6], [33, 18], [33, 22], [33, 33], [33, nil], [33, 5], [33, 6], [nil, 18], [nil, 22], [nil, 33], [nil, nil], [nil, 5], [nil, 6], [5, 18], [5, 22], [5, 33], [5, nil], [5, 5], [5, 6], [6, 18], [6, 22], [6, 33], [6, nil], [6, 5], [6, 6]]
repeated_permutation() method form :
[[1], [4], [1], [1], [88], [9]]
repeated_permutation() method form :
[]
Example #2 :
Ruby
# Ruby code for repeated_permutation() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
c = [nil]
# declaring array
b = ["cow", nil, "dog"]
# repeated_permutation method example
puts "repeated_permutation() method form :
#{a.repeated_permutation(2).to_a}\n\n"
puts "repeated_permutation() method form :
#{b.repeated_permutation(1).to_a}\n\n"
puts "repeated_permutation() method form :
#{c.repeated_permutation(4).to_a}\n\n"
Output :
repeated_permutation() method form :
[["abc", "abc"], ["abc", "nil"], ["abc", "dog"], ["nil", "abc"], ["nil", "nil"], ["nil", "dog"], ["dog", "abc"], ["dog", "nil"], ["dog", "dog"]]
repeated_permutation() method form :
[["cow"], [nil], ["dog"]]
repeated_permutation() method form :
[[nil, nil, nil, nil]]
Similar Reads
Ruby | Array permutation() function
Array#permutation() : permutation() is a Array class method which returns all permutations of length n of the elements of the array, then return the array itself. Syntax: Array.permutation() Parameter: Array Return: all permutations of length n of the elements of the array, then return the array its
2 min read
Ruby | Array repeated_combination() function
Array#repeated_combination() : repeated_combination() is a Array class method which returns all repeated combinations of length n of elements from the array and then returns the array itself. Syntax: Array.repeated_combination() Parameter: Array Return: all repeated combinations of length n of eleme
2 min read
Ruby | Array rotate!() function
Array#rotate!() : rotate!() is a Array class method which returns self rotated in place so that the element at count comes first, and returns self. Syntax: Array.rotate!() Parameter: Array Return: self rotated in place so that the element at count comes first, and returns self. Example #1 : Ruby # R
2 min read
Ruby | Array rotate() function
Array#rotate() : rotate() is a Array class method which returns a new array by rotating self so that the element at count is the first element of the new array. Syntax: Array.rotate() Parameter: Array Return: a new array by rotating self so that the element at count is the first element of the new a
2 min read
Ruby | Array replace() function
Array#replace() : replace() is a Array class method which returns an array of all combinations of elements from all arrays. Syntax: Array.replace() Parameter: Array Return: an array of all combinations of elements from all arrays. Example #1 : Ruby # Ruby code for replace() method # declaring array
2 min read
Ruby | Array rindex() function
Array#rindex() : rindex() is a Array class method which returns the index of the last object in the array. Syntax: Array.rindex() Parameter: Array Return: the index of the last object in the array. if not present then nil Example #1 : Ruby # Ruby code for rindex() method # declaring array a = [18, 2
1 min read
Ruby | Array pop() function
Array#pop() : pop() is a Array class method which checks removes the last element from the array and returns it. Syntax: Array.pop() Parameter: Array Return: removes the last element from the array and returns it. Example #1 : Ruby # Ruby code for pop() method # declaring array a = [18, 22, 33, nil,
1 min read
Ruby | Array one?() function
Array#one?() : one?() is a Array class method which checks whether the array is having only one array element. Syntax: Array.one?() Parameter: Array Return: true - if array has only one element otherwise return false. Example #1 :Â Ruby # Ruby code for one?() method # declaring array a = [18, 22, 33
1 min read
Ruby | Array map() function
Array#map() : map() is a Array class method which returns a new array containing the values returned by the block. Syntax: Array.map() Parameter: Array Return: a new array containing the values returned by the block. Example #1 : Ruby # Ruby code for map() method # declaring array a = [18, 22, 33, 3
2 min read
Ruby | Array product() function
Array#product() : product() is a Array class method which returns an array of all combinations of elements from all arrays. Syntax: Array.product() Parameter: Array Return: an array of all combinations of elements from all arrays. Example #1 : Ruby # Ruby code for product() method # declaring array
2 min read