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 | Loops (for, while, do..while, until) Looping is a fundamental concept in programming that allows for the repeated execution of a block of code based on a condition. Ruby, being a flexible and dynamic language, provides various types of loops that can be used to handle condition-based iterations. These loops simplify tasks that require
5 min read
Ruby Programming Language Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. Ruby is a pure Object-Oriented language developed by Yukihiro Matsumoto. Everything in Ruby is an object except the blocks but there are replacements too for it i.e procs and lambda. The objective of Rubyâs develop
2 min read
Ruby on Rails Introduction Ruby on Rails or also known as rails is a server-side web application development framework that is written in the Ruby programming language, and it is developed by David Heinemeier Hansson under the MIT License. It supports MVC(model-view-controller) architecture that provides a default structure f
6 min read
Ruby on Rails Interview Questions And Answers Ruby on Rails, often shortened to Rails, is a server-side web application framework written in Ruby under the MIT License. Rails is known for its ease of use and ability to build complex web applications quickly. It was created by David Heinemeier Hansson and was first released in 2004. Now, Over 3.
15+ min read
Ruby - String split() Method with Examples split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified. Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches. Syntax: arr = s
2 min read
Ruby Tutorial Ruby is a object-oriented, reflective, general-purpose, dynamic programming language. Ruby was developed to make it act as a sensible buffer between human programmers and the underlying computing machinery. It is an interpreted scripting language which means most of its implementations execute instr
6 min read
Ruby | Data Types Data types in Ruby represents different types of data like text, string, numbers, etc. All data types are based on classes because it is a pure Object-Oriented language. There are different data types in Ruby as follows: NumbersBooleanStringsHashesArraysSymbols Numbers: Generally a number is defined
3 min read
Ruby | Case Statement The case statement is a multiway branch statement just like a switch statement in other languages. It provides an easy way to forward execution to different parts of code based on the value of the expression. There are 3 important keywords which are used in the case statement: case: It is similar to
3 min read
Hello World in Ruby Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. Hello World the program is the most basic and first program when we start a new programming language. This simply prints Hello World on the screen. Below is the program to write hello world". How to run a Ruby Prog
2 min read
Ruby For Beginners Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. This article will cover its basic syntax and some basic programs. This article is divided into various sections for various topi
3 min read