Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Permutations/R/permutations-1.r
Normal file
35
Task/Permutations/R/permutations-1.r
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
next.perm <- function(a) {
|
||||
n <- length(a)
|
||||
i <- n
|
||||
while (i > 1 && a[i - 1] >= a[i]) i <- i - 1
|
||||
if (i == 1) {
|
||||
NULL
|
||||
} else {
|
||||
j <- i
|
||||
k <- n
|
||||
while (j < k) {
|
||||
s <- a[j]
|
||||
a[j] <- a[k]
|
||||
a[k] <- s
|
||||
j <- j + 1
|
||||
k <- k - 1
|
||||
}
|
||||
s <- a[i - 1]
|
||||
j <- i
|
||||
while (a[j] <= s) j <- j + 1
|
||||
a[i - 1] <- a[j]
|
||||
a[j] <- s
|
||||
a
|
||||
}
|
||||
}
|
||||
|
||||
perm <- function(n) {
|
||||
e <- NULL
|
||||
a <- 1:n
|
||||
repeat {
|
||||
e <- cbind(e, a)
|
||||
a <- next.perm(a)
|
||||
if (is.null(a)) break
|
||||
}
|
||||
unname(e)
|
||||
}
|
||||
5
Task/Permutations/R/permutations-2.r
Normal file
5
Task/Permutations/R/permutations-2.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
> perm(3)
|
||||
[,1] [,2] [,3] [,4] [,5] [,6]
|
||||
[1,] 1 1 2 2 3 3
|
||||
[2,] 2 3 1 3 1 2
|
||||
[3,] 3 2 3 1 2 1
|
||||
11
Task/Permutations/R/permutations-3.r
Normal file
11
Task/Permutations/R/permutations-3.r
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# list of the vectors by inserting x in s at position 0...end.
|
||||
linsert <- function(x,s) lapply(0:length(s), function(k) append(s,x,k))
|
||||
|
||||
# list of all permutations of 1:n
|
||||
perm <- function(n){
|
||||
if (n == 1) list(1)
|
||||
else unlist(lapply(perm(n-1), function(s) linsert(n,s)),
|
||||
recursive = F)}
|
||||
|
||||
# permutations of a vector s
|
||||
permutation <- function(s) lapply(perm(length(s)), function(i) s[i])
|
||||
18
Task/Permutations/R/permutations-4.r
Normal file
18
Task/Permutations/R/permutations-4.r
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
> permutation(letters[1:3])
|
||||
[[1]]
|
||||
[1] "c" "b" "a"
|
||||
|
||||
[[2]]
|
||||
[1] "b" "c" "a"
|
||||
|
||||
[[3]]
|
||||
[1] "b" "a" "c"
|
||||
|
||||
[[4]]
|
||||
[1] "c" "a" "b"
|
||||
|
||||
[[5]]
|
||||
[1] "a" "c" "b"
|
||||
|
||||
[[6]]
|
||||
[1] "a" "b" "c"
|
||||
Loading…
Add table
Add a link
Reference in a new issue