Home » Ruby programming

Array.repeated_permutation() Method with Example in Ruby

Ruby Array.repeated_permutation() Method: Here, we are going to learn about the Array.repeated_permutation() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 09, 2020

Array.repeated_permutation() Method

In this article, we will study about Array.repeated_permutation() method. You all must be thinking the method must be doing something which is related to creating permutations of certain elements. Let's figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it will take elements from the Array instance and make repeated permutations according to the number passed in the method and then it returns the Array instance itself. This method does not guarantee the order of the elements yielded. This method is invoked with a block or an Array and the result is being converted into the Array instance with the help of .to_a method.

If you do not provide any block then the enumerator is returned itself.

Syntax:

    array.repeated_permutation(n) { |c| block }

Argument(s) required:

This method only requires one argument. This argument decides the number of repeated permutations possible from the elements of Array instance.

Example 1:

=begin
  Ruby program to demonstrate repeated_permutation method
=end

# array declaration
a = [1, 2]

print a.repeated_permutation(1).to_a  
puts ""

print a.repeated_permutation(2).to_a
puts ""  

print a.repeated_permutation(3).to_a  

Output

[[1], [2]]
[[1, 1], [1, 2], [2, 1], [2, 2]]
[[1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2]]

Explanation:

In the above code, you can observe that this method is used to create repeated permutations of the array elements. Repeated permutations are being created on the basis of the argument passed inside the method. You can predict the number of permutations that can be generated by keeping the length of the Array as the base and the integer passed as the argument as power. In the above code, you can observe that when the integer 3 is passed, 8 permutations are created.

Example 2:

=begin
  Ruby program to demonstrate repeated_permutation method
=end

# array declaration
a = ["Kamiara","Arshi"]

print a.repeated_permutation(1).to_a  
puts ""

print a.repeated_permutation(2).to_a
puts ""  

print a.repeated_permutation(3).to_a 

Output

[["Kamiara"], ["Arshi"]]
[["Kamiara", "Kamiara"], ["Kamiara", "Arshi"], ["Arshi", "Kamiara"], ["Arshi", "Arshi"]]
[["Kamiara", "Kamiara", "Kamiara"], ["Kamiara", "Kamiara", "Arshi"], ["Kamiara", "Arshi", "Kamiara"], ["Kamiara", "Arshi", "Arshi"], ["Arshi", "Kamiara", "Kamiara"], ["Arshi", "Kamiara", "Arshi"], ["Arshi", "Arshi", "Kamiara"], ["Arshi", "Arshi", "Arshi"]]

Explanation:

In the above example you can observe that this method works upon String Array instances as well. This method is returning elements after making their repeated permutations. You can predict the number of permutations that can be generated by keep the length of the Array as the base and the integer passed as the argument as power. In the above code, you can observe that when the integer 3 is passed, 8 permutations are created.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.