Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested loops and debugging

Tags:

php

Objective: Elaborate a function, that returns an array of dimension 4x4. An example of the possible outcome of the function is:

All I have done:

matriz<-function(M){

M<-matrix(ncol=4,nrow=4);

M[1,]<-sample(LETTERS[1:4]);

M[2,]<-sample(LETTERS[1:4]);

for(i in 2:4){
    for(j in 1:4){
    j<-j

        if(j<=4)
        if(M[i,j]!= M[i-1,j]){
            j<-j+1
            }
        else{
            M[i,]<-sample(LETTERS[1:4])
            }

    }
    i<-i+1
    if(i<=4){
        M[i,]<-sample(LETTERS[1:4])
        j=1
        }
    else{print(M)}
 }
}

debug(matriz); matriz(M)
like image 672
Gerard Alemany Avatar asked May 13 '17 11:05

Gerard Alemany


People also ask

What are nested loops used for?

The inner loop is nested inside the outer loop. Nested loops are useful when for each pass through the outer loop, you need to repeat some action on the data in the outer loop. For example, you read a file line by line and for each line you must count how many times the word “the” is found.

What is the concept of nested loop?

A nested loop means a loop statement inside another loop statement. That is why nested loops are also called “loop inside loops“. We can define any number of loops inside another loop.

What is the main advantage of nested loops?

Nested loop join with sparse index has the following performance advantages: Access to the inner table is more efficient when the inner has no efficient index on the join columns. A sort of the composite table is avoided when composite table is relatively large.

Why are nested for loops bad?

Nested loops are frequently (but not always) bad practice, because they're frequently (but not always) overkill for what you're trying to do. In many cases, there's a much faster and less wasteful way to accomplish the goal you're trying to achieve.


1 Answers

This seems to work. It generates a list of all possible combinations, then deletes any that match previously selected rows, leaving just one at the end.

matriz <- function(n){
  combs <- as.matrix(expand.grid(rep(list(LETTERS[1:n]),n)))
  combs <- combs[apply(combs,1,function(r) all(LETTERS[1:n] %in% r)),]
  mat <- matrix(NA,nrow=n,ncol=n)
  for(i in 1:(n-1)){
    mat[i,] <- combs[sample(1:nrow(combs),1),]
    combs <- combs[!apply(combs,1,function(r) any(r == mat[i,])),]
  }
  mat[n,] <- combs
  return(mat)
  }

> matriz(5)
     [,1] [,2] [,3] [,4] [,5]
[1,] "B"  "D"  "A"  "E"  "C" 
[2,] "E"  "C"  "D"  "B"  "A" 
[3,] "D"  "A"  "B"  "C"  "E" 
[4,] "A"  "E"  "C"  "D"  "B" 
[5,] "C"  "B"  "E"  "A"  "D" 

> matriz(5)
     [,1] [,2] [,3] [,4] [,5]
[1,] "D"  "C"  "E"  "B"  "A" 
[2,] "E"  "A"  "C"  "D"  "B" 
[3,] "A"  "D"  "B"  "C"  "E" 
[4,] "B"  "E"  "D"  "A"  "C" 
[5,] "C"  "B"  "A"  "E"  "D" 

A slightly faster version using the combinat package would be

library(combinat)
matriz <- function(n){
  combs <- do.call(rbind,permn(LETTERS[1:n]))
  mat <- matrix(NA,nrow=n,ncol=n)
  #rest of function as above...

Both of these can be quite slow for n>10 or so. However, if you have generated one valid square m, then all others will be permutations m[sample(nrow(m)),sample(ncol(m))] so this might be a faster approach if you are doing lots of them.

like image 175
Andrew Gustar Avatar answered Sep 20 '22 12:09

Andrew Gustar