我有一个字符串“ ECET”,我想创建所有可能的字符串,在其中用“ X”替换一个或多个字母(除第一个字母外)。
所以在这种情况下,我的结果将是:
> result
[1] "EXET" "ECXT" "ECEX" "EXXT" "EXEX" "ECXX" "EXXX"
关于如何解决该问题的任何想法?
这不仅可以创建“ X”的可能组合/排列,而且还可以将它们与现有字符串组合。
答案 0 :(得分:14)
使用function p<T>() { return null as any as T; }
const examplePropsSpec = {
specialProp: p<string>(),
};
export type ExampleProps = {
[K in keyof typeof examplePropsSpec]: (typeof examplePropsSpec)[K];
};
let k: ExampleProps = {
specialProp: "k"
};
的{{1}}参数:
FUN
combn
a <- "ECET"
fun <- function(n, string) {
combn(nchar(string), n, function(x) {
s <- strsplit(string, '')[[1]]
s[x] <- 'X'
paste(s, collapse = '')
} )
}
lapply(seq_len(nchar(a)), fun, string = a)
获得单个向量。可能会有更快的解决方案。
要保持第一个字符不变:
[[1]]
[1] "XCET" "EXET" "ECXT" "ECEX"
[[2]]
[1] "XXET" "XCXT" "XCEX" "EXXT" "EXEX" "ECXX"
[[3]]
[1] "XXXT" "XXEX" "XCXX" "EXXX"
[[4]]
[1] "XXXX"
unlist
答案 1 :(得分:7)
这是一个递归解决方案:
f <- function(x,pos=2){
if(pos <= nchar(x))
c(f(x,pos+1), f(`substr<-`(x, pos, pos, "X"),pos+1))
else x
}
f(x)[-1]
# [1] "ECEX" "ECXT" "ECXX" "EXET" "EXEX" "EXXT" "EXXX"
或使用expand.grid
:
do.call(paste0, expand.grid(c(substr(x,1,1),lapply(strsplit(x,"")[[1]][-1], c, "X"))))[-1]
# [1] "EXET" "ECXT" "EXXT" "ECEX" "EXEX" "ECXX" "EXXX"
或使用combn
/ Reduce
/ substr<-
:
combs <- unlist(lapply(seq(nchar(x)-1),combn, x =seq(nchar(x))[-1],simplify = F),F)
sapply(combs, Reduce, f= function(x,y) `substr<-`(x,y,y,"X"), init = x)
# [1] "EXET" "ECXT" "ECEX" "EXXT" "EXEX" "ECXX" "EXXX"
第二种解决方法
pairs0 <- lapply(strsplit(x,"")[[1]][-1], c, "X") # pairs of original letter + "X"
pairs1 <- c(substr(x,1,1), pairs0) # including 1st letter (without "X")
do.call(paste0, expand.grid(pairs1))[-1] # expand into data.frame and paste
答案 2 :(得分:6)
为使用二进制逻辑添加另一个选项而设置的种类:
假设您的字符串始终为4个字符长:
input<-"ECET"
invec <- strsplit(input,'')[[1]]
sapply(1:7, function(x) {
z <- invec
z[rev(as.logical(intToBits(x))[1:4])] <- "X"
paste0(z,collapse = '')
})
[1] "ECEX" "ECXT" "ECXX" "EXET" "EXEX" "EXXT" "EXXX"
如果字符串必须更长,则可以使用2的幂来计算值,如下所示:
input<-"ECETC"
pow <- nchar(input)
invec <- strsplit(input,'')[[1]]
sapply(1:(2^(pow-1) - 1), function(x) {
z <- invec
z[rev(as.logical(intToBits(x))[1:(pow)])] <- "X"
paste0(z,collapse = '')
})
[1] "ECETX" "ECEXC" "ECEXX" "ECXTC" "ECXTX" "ECXXC" "ECXXX" "EXETC" "EXETX" "EXEXC" "EXEXX" "EXXTC" "EXXTX" "EXXXC"
[15] "EXXXX"
这个想法是要知道可能的变更数量,它是3个位置的二进制数,所以2 ^ 3减1,因为我们不想保留无替换字符串:7
intToBits返回整数的二进制值,为5:
> intToBits(5)
[1] 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
R默认情况下使用32位,但是我们只需要一个与字符串长度相对应的逻辑向量,因此我们只保留原始字符串的nchar。 然后,我们将转换为逻辑并反转这4个布尔值,因为我们永远不会触发最后一位(4个字符为8),它将永远不会为真:
> intToBits(5)
[1] 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> tmp<-as.logical(intToBits(5)[1:4])
> tmp
[1] TRUE FALSE TRUE FALSE
> rev(tmp)
[1] FALSE TRUE FALSE TRUE
为避免覆盖原始矢量,我们将其复制到z中,然后仅使用此逻辑矢量替换z中的位置。
为获得良好的输出,我们返回带有折叠的paste0,因为没有什么可以重新创建单个字符串并检索字符向量。
答案 3 :(得分:3)
使用purrr的另一个带有combn的版本:
s <- "ECET"
f <- function(x,y) {substr(x,y,y) <- "X"; x}
g <- function(x) purrr::reduce(x,f,.init=s)
unlist(purrr::map(1:(nchar(s)-1), function(x) combn(2:nchar(s),x,g)))
#[1] "EXET" "ECXT" "ECEX" "EXXT" "EXEX" "ECXX" "EXXX"
或没有purrr:
s <- "ECET"
f <- function(x,y) {substr(x,y,y) <- "X"; x}
g <- function(x) Reduce(f,x,s)
unlist(lapply(1:(nchar(s)-1),function(x) combn(2:nchar(s),x,g)))
答案 4 :(得分:2)
这是基本的R解决方案,但我发现它很复杂,有3个嵌套循环。
replaceChar <- function(x, char = "X"){
n <- nchar(x)
res <- NULL
for(i in seq_len(n)){
cmb <- combn(n, i)
r <- apply(cmb, 2, function(cc){
y <- x
for(k in cc)
substr(y, k, k) <- char
y
})
res <- c(res, r)
}
res
}
x <- "ECET"
replaceChar(x)
replaceChar(x, "Y")
replaceChar(paste0(x, x))
答案 5 :(得分:1)
具有布尔索引的矢量化方法:
permX <- function(text, replChar='X') {
library(gtools)
library(stringr)
# get TRUE/FALSE permutations for nchar(text)
idx <- permutations(2, nchar(text),c(T,F), repeats.allowed = T)
# we don't want the first character to be replaced
idx <- idx[1:(nrow(idx)/2),]
# split string into single chars
chars <- str_split(text,'')
# build data.frame with nrows(df) == nrows(idx)
df = t(data.frame(rep(chars, nrow(idx))))
# do replacing
df[idx] <- replChar
row.names(df) <- c()
return(df)
}
permX('ECET')
[,1] [,2] [,3] [,4]
[1,] "E" "C" "E" "T"
[2,] "E" "C" "E" "X"
[3,] "E" "C" "X" "T"
[4,] "E" "C" "X" "X"
[5,] "E" "X" "E" "T"
[6,] "E" "X" "E" "X"
[7,] "E" "X" "X" "T"
[8,] "E" "X" "X" "X"
答案 6 :(得分:1)
另一个简单的解决方案
# expand.grid to get all combinations of the input vectors, result in a matrix
m <- expand.grid( c('E'),
c('C','X'),
c('E','X'),
c('T','X') )
# then, optionally, apply to paste the columns together
apply(m, 1, paste0, collapse='')[-1]
[1] "EXET" "ECXT" "EXXT" "ECEX" "EXEX" "ECXX" "EXXX"