Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/ABC-problem/R/abc-problem-1.r
Normal file
43
Task/ABC-problem/R/abc-problem-1.r
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
blocks <- rbind(c("B","O"),
|
||||
c("X","K"),
|
||||
c("D","Q"),
|
||||
c("C","P"),
|
||||
c("N","A"),
|
||||
c("G","T"),
|
||||
c("R","E"),
|
||||
c("T","G"),
|
||||
c("Q","D"),
|
||||
c("F","S"),
|
||||
c("J","W"),
|
||||
c("H","U"),
|
||||
c("V","I"),
|
||||
c("A","N"),
|
||||
c("O","B"),
|
||||
c("E","R"),
|
||||
c("F","S"),
|
||||
c("L","Y"),
|
||||
c("P","C"),
|
||||
c("Z","M"))
|
||||
|
||||
canMake <- function(x) {
|
||||
x <- toupper(x)
|
||||
used <- rep(FALSE, dim(blocks)[1L])
|
||||
charList <- strsplit(x, character(0))
|
||||
tryChars <- function(chars, pos, used, inUse=NA) {
|
||||
if (pos > length(chars)) {
|
||||
TRUE
|
||||
} else {
|
||||
used[inUse] <- TRUE
|
||||
possible <- which(blocks == chars[pos] & !used, arr.ind=TRUE)[, 1L]
|
||||
any(vapply(possible, function(possBlock) tryChars(chars, pos + 1, used, possBlock), logical(1)))
|
||||
}
|
||||
}
|
||||
setNames(vapply(charList, tryChars, logical(1), 1L, used), x)
|
||||
}
|
||||
canMake(c("A",
|
||||
"BARK",
|
||||
"BOOK",
|
||||
"TREAT",
|
||||
"COMMON",
|
||||
"SQUAD",
|
||||
"CONFUSE"))
|
||||
21
Task/ABC-problem/R/abc-problem-2.r
Normal file
21
Task/ABC-problem/R/abc-problem-2.r
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
canMakeNoRecursion <- function(x) {
|
||||
x <- toupper(x)
|
||||
charList <- strsplit(x, character(0))
|
||||
getCombos <- function(chars) {
|
||||
charBlocks <- data.matrix(expand.grid(lapply(chars, function(char) which(blocks == char, arr.ind=TRUE)[, 1L])))
|
||||
charBlocks <- charBlocks[!apply(charBlocks, 1, function(row) any(duplicated(row))), , drop=FALSE]
|
||||
if (dim(charBlocks)[1L] > 0L) {
|
||||
t(apply(charBlocks, 1, function(row) apply(blocks[row, , drop=FALSE], 1, paste, collapse="")))
|
||||
} else {
|
||||
character(0)
|
||||
}
|
||||
}
|
||||
setNames(lapply(charList, getCombos), x)
|
||||
}
|
||||
canMakeNoRecursion(c("A",
|
||||
"BARK",
|
||||
"BOOK",
|
||||
"TREAT",
|
||||
"COMMON",
|
||||
"SQUAD",
|
||||
"CONFUSE"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue