Mr.JoshuaGordon
BAN USERR
x <- scan()
4 3 8 9 -2 6 10 13 -1 2 3
s <- c(0, which(x < 0), length(x) + 1)
result <- rep(NA, length(x))
result[which(x < 0)] <- x[which(x < 0)]
for(i in 1:(length(s)-1)){
flip <- (s[i]+1):(s[i+1]-1)
result[flip] <- rev(x[flip])
}
print(result)
R
maxConcat <- function(s) {
s1 <- unique(as.matrix(expand.grid(s,s,s)))
s2 <- s1[(apply(s1, 1, FUN = function(x) length(unique(x))) == 3),]
s3 <- apply(s2, 1, FUN = function(x) paste0(x, collapse = ''))
return(s3[which.max(as.numeric(s3))])
}
x1 <- c('9', '918', '917')
maxConcat(s = x1)
x2 <- c('1','112','113')
maxConcat(s = x2)
R
s <- 'adobecodebanc'
t <- 'abc'
t <- strsplit(t,'')[[1]]
sz <- 2
test <- FALSE
while(test == FALSE){
for(i in 1:(nchar(s)-sz)){
s1 <- strsplit(substr(s,i,i+sz),'')[[1]]
if(sum(t %in% s1) == 3) {
test <- TRUE
cat(s1)
}
}
sz <- sz + 1
}
R
input <- "[1,2][5,6][1,5][7,8][1,6]"
j <- input
j <- gsub(pattern='\\[','',j)
j <- strsplit(j, ']')[[1]]
j <- sapply(j,FUN=strsplit,split=',')
pairs <- matrix(as.numeric(unlist(j)),byrow=T, ncol=2)
myRange <- function(data){
return(seq(from=data[1], data[2], by=1))
}
ls <- apply(pairs, 1,FUN= myRange)
getOverlappingSets <- function(ls){
inside <- c()
for(i in 1:(length(ls))){
count <- 0
for(j in 1:length(ls)){
if( i != j ){
if(any(ls[[i]] %in% ls[[j]])){
count <- count + 1
}
}
}
if(count > 0){
inside <- c(inside, i)
}
}
return(inside)
}
getOverlappingSets(ls)
# test case
x = floor(runif(8,0,9)*10)
y = x + floor(runif(8, 1, 10))*3
pairs <- data.frame(x,y)
ls <- apply(pairs, 1,FUN= myRange)
getOverlappingSets(ls)
I was not sure what to do in the case of (()()) Is it two or three?
- Mr.JoshuaGordon February 24, 2015countNested <- function(input){
s <- strsplit(input,"")[[1]]
if(length(which(c('(',')') %in% s == T)) == 0) {
noquote('Please try again')
} else{
open <- c()
count <- 0
top <- 0
for(i in 1:length(s)){
if(s[i]=="("){
open <- c(open, "(")
} # end nested opener
if(length(open)>0) {
if(s[i]==")"){
open <- open[-length(open)]
count <- count + 1
top <- max(top, count)
if(length(open)==0){
count <- 0
} # start over again and keep looking
} # end nested closer
} #end if length(open) > 0
} # end loop
return(top)
} #end string check
}
countNested("abc(123(xyz))m(((n)))o")
countNested("(as)(as)s(a()())")
R
a <- matrix(c(0,10,100,10,15,300,16,20,400,5,15,200), ncol=3, byrow=T)
> a
[,1] [,2] [,3]
[1,] 0 10 100
[2,] 10 15 300
[3,] 16 20 400
[4,] 5 15 200
alla <- data.frame()
for(i in 1:nrow(a)){
sa <- seq(a[i,1], a[i,2], by = 1)
ta <- data.frame(sa, rep(a[i,3], length(sa) ))
alla <- rbind(alla, ta)
}
names(alla) <- c('time', 'flow')
ag <- aggregate(alla$flow, by=list(alla$time), FUN=sum)
ag[which.max(ag[,2]),]
R
str <- "3+12*3-4/7-12*2/2"
op <- c('/','*','-','+')
str1 <- as.numeric(strsplit(gsub("[+-/*]",",",str),',')[[1]])
str2 <- strsplit(str,"")[[1]]
str3 <- op[match(str2,op)[!is.na(match(str2,op))]]
str4 <- rep(NA, length(str1) + length(str3))
str4[seq(1, length(str4), by = 2)] <- str1
str4[is.na(str4)] <- str3
str2a <- str4
# multiplcation and division
md <- function(str2a) {
i <-1
while(length(str2a)>1){
if(str2a[i] == '*'){
str2a[i] <- as.numeric(str2a[i-1]) * as.numeric(str2a[i+1])
str2a <- str2a[-c(i-1,i+1)]
} else if(str2a[i] == '/'){
str2a[i] <- as.numeric(str2a[i-1]) / as.numeric(str2a[i+1])
str2a <- str2a[-c(i-1,i+1)]
}
i <- i +1
if(i > length(str2a)) {i <- 1}
if(length(which(str2a %in% op[1:2])) == 0){break}
}
return(str2a)
}
str2b <- md(str2a)
as <- function(str2a){
i <-1
while(length(str2a)>1){
if(str2a[i] == '+'){
str2a[i] <- as.numeric(str2a[i-1]) + as.numeric(str2a[i+1])
str2a <- str2a[-c(i-1,i+1)]
} else if(str2a[i] == '-'){
str2a[i] <- as.numeric(str2a[i-1]) - as.numeric(str2a[i+1])
str2a <- str2a[-c(i-1,i+1)]
}
i <- which(str2a %in% op[3:4])[1]
if(length(which(str2a %in% op[3:4])) == 0){break}
}
return(as.numeric(str2a))
}
as(str2b)
Another R solution:
temp = scan()
0 10 20 10 30 40 50 40 30 20 10 20 30 40 50 60 50 60 70
plot(temp, type='b')
result <- c()
if(temp[1] < temp[2]) {
result[1] <- temp[1]
} else{
result <- c()
}
for(i in 2:(length(temp)-1)){
if(temp[i] < temp[i+1] & temp[i] < temp[i-1]){
result <- c(result, temp[i])
}
}
if(temp[length(temp)] < temp[length(temp)-1]) {
result = c(result, temp[length(temp)])
}
result
R:
mountain <- function(M = 10, N = 10) {
a <- matrix(floor(runif(n=M*N, min = 0, max = 100)), nrow = N, ncol = M)
moves <- expand.grid(-1:1, -1:1)
moves <- moves[-which(moves[,1] == 0 & moves[,2] == 0),]
result <- c()
for(i in 2:N-1){
for(j in 2:M-1){
pos <- c(i,j)
check <- pos + moves
if( length(which(a[i,j] > a[as.matrix(check)])==T) == 8){
result <- c(result, a[i,j])
}
}
}
return(list(result, a))
}
mountain(5,5)
Fixed for multiple 2's in one number
- Mr.JoshuaGordon November 28, 2015