Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index: 1 2 3 4 5 6 7 8 9 10
- string: 1 0 0 0 1 -1 -1 0 1 -1 #this is input to function
- returns index of -1 and 1 so that -1 index is listed first
- [6,1] #from 1 to -1
- [6,9] #from -1 to 1
- [10,9] #from 1 to -1
- x<-c(1,0,0,0,1,-1,-1,0,1)
- matchVals(x)
- matchVals<-function(x){
- current.index<-getStart(x)
- #next VALUE to search for should be:
- next.val<-x[current.index]*-1
- next.index<-getNextVal(x[current.index:length(x)],next.val) + current.index #adding in offset
- return(current.index,next.index)
- }
- #gets index of first value that isnt a 0
- getStart<-function(x){
- lapply(1:length(x),function(i){if(x[i]!=0)return(i)})
- return(NA)
- }
- #gets FIRST index of the value specified (so dont feed it the entire string). Must add offset for truncated portion of string
- getNextVal<-function(x,v){
- lapply(1:length(x),function(i){if(x[i]==v)return(i)})
- return(NA)
- }
- x = c(1, 0, 0, 0, 1, -1, -1, 0, 1, -1)
- i1 = which(x == 1)
- i2 = which(x == -1)
- ff = function(x, y, acc = integer())
- {
- if(!length(x)) return(acc)
- if(!length(y)) return(c(acc, x[[1L]]))
- Recall(y[(findInterval(x[[1L]], y) + 1L):length(y)], x[-1L], c(acc, x[[1L]]))
- }
- ans = if(i1[[1]] < i2[[1]]) ff(i1, i2) else ff(i2, i1)
- ans
- #[1] 1 6 9 10
- tmp = embed(ans, 2)
- i = (seq_len(nrow(tmp)) %% 2) == (if(i1[[1]] < i2[[1]]) 0 else 1)
- tmp[i, ] = t(apply(tmp[i, , drop = FALSE], 1, rev))
- tmp
- # [,1] [,2]
- #[1,] 6 1
- #[2,] 6 9
- #[3,] 10 9
- X = c(0, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, 0, 0, -1, 0, 0, 1, -1,
- -1, -1, 0, 1, 0, 1, 1, 1, -1, 0, 0, 1, 0, 1, 0, -1, 1, 1, 1,
- -1, 0, 0, 1, 0, 1, 0, -1, 1, 1, 1)
- i1 = which(X == 1)
- i2 = which(X == -1)
- if(i1[[1]] < i2[[1]]) ff(i1, i2) else ff(i2, i1)
- # [1] 3 7 10 17 18 22 27 30 34 35 38 41 45 46
- #..and proceed as necessary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement