Topswops: Difference between revisions

Content added Content deleted
m (→‎{{header|C++}}: Remove vanity tags)
(Add R language solution)
Line 1,764: Line 1,764:
12: 65</pre>
12: 65</pre>


=={{header|Racket}}==
=={{header|R}}==
Using iterpc package for optimization
<lang R>
topswops <- function(x){
i <- 0
while(x[1] != 1){
first <- x[1]
if(first == length(x)){
x <- rev(x)
} else{
x <- c(x[first:1], x[(first+1):length(x)])
}
i <- i + 1
}
return(i)
}


library(iterpc)

resultad <- NULL

for(i in 1:10){
I <- iterpc(i, labels = 1:i, ordered = T)
A <- getall(I)
A <- data.frame(A)
A$flips <- apply(A, 1, topswops)
resultad <- rbind(resultad, c(i, max(A$flips)))
}
</lang>

Output:
<pre>
[,1] [,2]
[1,] 1 0
[2,] 2 1
[3,] 3 2
[4,] 4 4
[5,] 5 7
[6,] 6 10
[7,] 7 16
[8,] 8 22
[9,] 9 30
[10,] 10 38
</pre>

=={{header|Racket}}==
Simple search, only "optimization" is to consider only all-misplaced permutations (as in the alternative Haskell solution), which shaves off around 2 seconds (from ~5).
Simple search, only "optimization" is to consider only all-misplaced permutations (as in the alternative Haskell solution), which shaves off around 2 seconds (from ~5).