How to resolve the algorithm Combinations with repetitions step by step in the Kotlin programming language
How to resolve the algorithm Combinations with repetitions step by step in the Kotlin programming language
Table of Contents
Problem Statement
The set of combinations with repetitions is computed from a set,
S
{\displaystyle S}
(of cardinality
n
{\displaystyle n}
), and a size of resulting selection,
k
{\displaystyle k}
, by reporting the sets of cardinality
k
{\displaystyle k}
where each member of those sets is chosen from
S
{\displaystyle S}
. In the real world, it is about choosing sets where there is a “large” supply of each type of element and where the order of choice does not matter. For example: Note that both the order of items within a pair, and the order of the pairs given in the answer is not significant; the pairs represent multisets. Also note that doughnut can also be spelled donut.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Combinations with repetitions step by step in the Kotlin programming language
Explanation of the Kotlin Code:
1. Class Definition:
- The code defines a Kotlin class named
CombsWithReps
. This class generates combinations with repetitions for a given number of elements (n
) takenm
at a time.
2. Properties:
m
: The number of elements to be taken at a time.n
: The total number of elements.items
: A list of items to generate combinations from.countOnly
: A boolean flag that specifies whether to only count the combinations or print them as well.
3. Initialization:
- The class constructor initializes the
combination
array with a size ofm
to represent the current combination. count
is initialized to 0 to keep track of the number of combinations generated.- The
generate
function is called with an initial value of 0 to start generating combinations. - If
countOnly
is false, a newline is printed to separate the output. - The total number of combinations is printed.
4. generate
Function:
- The
generate
function is a recursive function that generates combinations. - It takes a parameter
k
, which represents the current index in thecombination
array. - If
k
is greater than or equal tom
, it means the current combination is complete. - If
countOnly
is false, the current combination is printed by iterating through thecombination
array and printing the corresponding items from theitems
list. - The
count
is incremented to keep track of the combinations. - If
k
is less thanm
, a loop is executed to generate possible values for thek
-th element in the combination. - For each value
j
within the range of 0 ton
, thecombination
array is updated withj
at indexk
and thegenerate
function is called recursively to continue generating the next element in the combination.
5. Main Function:
- The
main
function is the entry point of the program. - It creates a list of doughnuts (["iced", "jam", "plain"]) and generates all combinations of 2 elements taken 3 at a time with repetitions.
- A list of generic characters ("0123456789") is also created and used to generate all combinations of 3 elements taken 10 at a time with repetitions.
Output:
For the doughnuts list:
iced jam
iced plain
jam iced
jam plain
plain iced
plain jam
There are 9 combinations of 3 things taken 2 at a time, with repetitions
For the generic characters list:
0 1 2
0 1 3
0 1 4
...
9 7 8
9 8 9
There are 1000 combinations of 10 things taken 3 at a time, with repetitions
Source code in the kotlin programming language
// version 1.0.6
class CombsWithReps<T>(val m: Int, val n: Int, val items: List<T>, val countOnly: Boolean = false) {
private val combination = IntArray(m)
private var count = 0
init {
generate(0)
if (!countOnly) println()
println("There are $count combinations of $n things taken $m at a time, with repetitions")
}
private fun generate(k: Int) {
if (k >= m) {
if (!countOnly) {
for (i in 0 until m) print("${items[combination[i]]}\t")
println()
}
count++
}
else {
for (j in 0 until n)
if (k == 0 || j >= combination[k - 1]) {
combination[k] = j
generate(k + 1)
}
}
}
}
fun main(args: Array<String>) {
val doughnuts = listOf("iced", "jam", "plain")
CombsWithReps(2, 3, doughnuts)
println()
val generic10 = "0123456789".chunked(1)
CombsWithReps(3, 10, generic10, true)
}
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Ring programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Octave programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Haskell programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the PicoLisp programming language