How to iterate over the product of several ranges or iterators? - iterator

Is there a natural way in Rust to iterate over the "product" of several ranges or iterators?
This comes up when you're iterating over a multidimensional array, or perhaps some state space. For instance, I want to consider all possible values of a boolean tuple with 5 elements. Nesting 5 for loops is a bit unwieldy.

Here is a macro that does the job:
macro_rules! product {
($first:ident, $($next:ident),*) => (
$first.iter() $(
.flat_map(|e| std::iter::repeat(e)
.zip($next.iter()))
)*
);
}
fn main() {
let a = ['A', 'B', 'C'];
let b = [1, 4];
let c = [true, false];
let d = ['x', 'y'];
for (((a, b), c), d) in product![a, b, c, d] {
println!("{} {} {} {}", a, b, c, d);
}
}
Output:
A 1 true x
A 1 true y
A 1 false x
A 1 false y
A 4 true x
A 4 true y
etc...
Playpen example
The macro expands to the following
a.iter()
.flat_map(|e| std::iter::repeat(e).zip(b.iter()))
.flat_map(|e| std::iter::repeat(e).zip(c.iter()))
.flat_map(|e| std::iter::repeat(e).zip(d.iter()))
flat_map(|e| ... ) combines a sequence of iterators into an iterator. The e is an element yielded by an iterator.
std::iter::repeat(e) creates an iterator that repeats e.
.zip( ... ) iterates over two iterators simultaneously, yielding the elements of both as a pair.
Macros are a bit longer to explain, so it's best to read the macro chapter in the book

The itertools crate has a very ergonomic macro (iproduct!) for iterating over the product of iterators. Here is an example:
pub fn main() {
let a = ['A', 'B', 'C'];
let b = [1, 4];
let c = [true, false];
let d = ['x', 'y'];
for (a, b, c, d) in itertools::iproduct!(&a, &b, &c, &d) {
println!("{} {} {} {}", a, b, c, d);
}
}

Related

Replacing an element in Kotlin 2D list

I'm trying to replace an element in 2D list
hand instead of one element at one specified index all element are changing
this is the list
`private val printS: MutableList<MutableList<Char>> = mutableListOf(mutableListOf())`
This is how I literate them
// adding S to the list
for (i in 1..seat) printS[0].add('S')
// now we have list of S char in the printS list
for (i in 1..row) {
printS.add(printS[0])
}
now try to change list 5 element 5
printS[5][5] = 'B'
this is the result
1 2 3 4 5 6 7
1 S S S S S B S
2 S S S S S B S
3 S S S S S B S
4 S S S S S B S
5 S S S S S B S
6 S S S S S B S
7 S S S S S B S
all the lists have changed not just one
I need to just change one Char so the result should be
1 2 3 4 5 6 7
1 S S S S S S S
2 S S S S S S S
3 S S S S S S S
4 S S S S S S S
5 S S S S S S S
6 S S S S S B S
7 S S S S S S S
Edit (based on OP's clarification in the first comment below):
val rows = 7
val cols = 7
val result = MutableList(rows) { MutableList(cols) { 'S' } }
result[5][5] = 'B'
Still valid:
Your code does not work because the inner loop is not nested due to you not having put parentheses on the outer loop:
for (i in 1..seat) printS[0].add('S')
for (i in 1..row) {
printS.add(printS[0])
}
This means – properly formatted - nothing else than:
for (i in 1..seat) {
printS[0].add('S')
}
for (i in 1..row) {
printS.add(printS[0])
}
Obsolete:
This should work:
val rows = 7
val cols = 7
val result: MutableList<MutableList<Char>> = mutableListOf()
for (row in 0 until rows) {
result.add(mutableListOf())
for (col in 0 until rows) {
result[row].add(if (row = 5 && col == 5) 'B' else 'S')
}
}
result.forEach(::println)
Output:
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, B, S]
[S, S, S, S, S, S, S]
But a shorter way to create this matrix would be:
val result = List(rows) { row ->
List(cols) { col ->
if (row == 5 && col == 5) 'B' else 'S'
}
}

Is there any operation to multiply each element in the first array to each element in the second array in Kotlin?

Is there any function (like fold, map, filter), which gets 2 arrays and lambda-function (for example multiplication) as parameters and returns third array?
I've used cycle for, but is there more beautiful method?
Yes, there is zip (nice example at the bottom of the page), see this (different) example:
fun main() {
val a = arrayOf( 1, 2, 3, 4 )
val b = arrayOf( 1, 2, 3, 4 )
val c = a.zip(b) { i, j -> i * j }
println(c)
}
which outputs
[1, 4, 9, 16]
There isn't a built in specifically but you can do this:
array1.zip(array2).map { (x,y) -> x*y }

How to shuffle elements of Mutable list in Kotlin?

I wanted to create a MutableList of alphabets and then shuffle them and store it in another MutableList.
I used shuffle() function but it resulted in the original list being shuffled as well which I didn't wanted to happen as I will be using the original list to map it with new shuffled one.
fun main(){
val alphabets = ('A'..'Z').toMutableList()
var shuffAlp = alphabets
shuffAlp.shuffle()
println(alphabets)
println(shuffAlp)
}
So I had to create two mutable list and then shuffle one of them
val alphabets = ('A'..'Z').toMutableList()
var shuffAlp = ('A'..'Z').toMutableList()
shuffAlp.shuffle()
This might be a trivial question but is there any other way where I do not have to create two same list?
shuffle does shuffle into original list, shuffled do and return new list.
And same behavior is for sort & sorted, sortBy & sortedBy, reverse & asReversed:
fun main(){
val alphabets = ('A'..'Z').toMutableList()
val shuffAlp = alphabets.shuffled()
println(alphabets)
println(shuffAlp)
}
Result:
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
[U, B, A, N, H, R, O, K, X, C, W, E, Q, P, J, Z, L, Y, S, M, I, D, V, F, G, T]

zip with next value with kotlin Flow

how to zip a Flow with the next value as zipWithNext operator for collections?
zipWithNext behaves like:
val letters = ('a'..'f').toList()
val pairs = letters.zipWithNext()
println(letters) // [a, b, c, d, e, f]
println(pairs) // [(a, b), (b, c), (c, d), (d, e), (e, f)]
but in my case letters would be:
val letters = flowOf('a'..'f')
bonus points:
i tried flowOf(1, 2, 3).scan(emptyList<Int>()) { acc, value -> acc + value }.toList() on https://play.kotlinlang.org/ but doesn't find flowOf what import or else i'm missing there?
One possible solution is:
val letters = ('a'..'f').toList().toTypedArray()
val lettersFlow = flowOf(*letters)
val result = lettersFlow.scan(Pair<Char, Char>('a','a')) { acc, value -> Pair(acc.second, value) }.drop(2).toList()
println(result)

How to obtain a minimal key from functional dependencies?

I need some help and guidelines.
I have the following relation: R = {A, B, C, D, E, F} and the set of functional dependencies
F = {
{AB -> C};
{A -> D};
{D -> AE};
{E -> F};
}
What is the primary key for R ?
If i apply inference rules i get these additional Function dependencies:
D -> A
D -> E
D -> F
D -> AEF
A -> E
A -> F
A -> DEF
How do I continue?
There is a well known algorithm to do this. I don't remember it, but the excercise seems to be simple enough not to use it.
I think this is all about transitivity:
CurrentKey = {A, B, C, D, E, F}
You know D determines E and E determines F. Hence, D determines F by transitivity. As F doesn't determine anything, we can remove it and as E can be obtained from D we can remove it as well:
CurrentKey = {A, B, C, D}
As AB determines C and C doesn't determine anything we know it can't be part of the key, so we remove it:
CurrentKey = {A, B, D}
Finally we know A determines D so we can remove the latter from the key:
CurrentKey = {A, B}
If once you have this possible key, you can recreate all functional dependencies it is a possible key.
PS: If you happen to have the algorithm handy, please post it as I'd be glad to re-learn that :)
Algorithm: Key computation (call with x = βˆ…)
procedure key(x;A;F)
foreach ! B 2 F do
if x and B 2 x and B ΜΈ2 then
return; /* x not minimal */
fi
od
if x+ = A then
print x; /* found a minimal key x */
else
X any element of A τ€€€ x+;
key(x [ fXg;A;F);
foreach ! X 2 F do
key(x [ ;A;F);
od
fi