Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

23
Task/Amb/R/amb-1.r Normal file
View file

@ -0,0 +1,23 @@
checkSentence <- function(sentence){
# Input: character vector
# Output: whether the sentence formed by the elements of the vector is valid
for (index in 1:(length(sentence)-1)){
first.word <- sentence[index]
second.word <- sentence[index+1]
last.letter <- substr(first.word, nchar(first.word), nchar(first.word))
first.letter <- substr(second.word, 1, 1)
if (last.letter != first.letter){ return(FALSE) }
}
return(TRUE)
}
amb <- function(sets){
# Input: list of character vectors containing all sets to consider
# Output: list of character vectors that are valid
all.paths <- apply(expand.grid(sets), 2, as.character)
all.paths.list <- split(all.paths, 1:nrow(all.paths))
winners <- all.paths.list[sapply(all.paths.list, checkSentence)]
return(winners)
}

14
Task/Amb/R/amb-2.r Normal file
View file

@ -0,0 +1,14 @@
sentence1 <- c("that", "thing", "grows", "slowly")
sentence2 <- c("rosetta", "code", "is", "cool")
sentence <- list(sentence1, sentence2)
sapply(sentence, checkSentence)
[1] TRUE FALSE
set1 <- c("the", "that", "a")
set2 <- c("frog", "elephant", "thing")
set3 <- c("walked", "treaded", "grows")
set4 <- c("slowly", "quickly")
sets <- list(set1, set2, set3, set4)
amb(sets)
$`26`
[1] "that" "thing" "grows" "slowly"