Generate an incrementally increasing sequence like 112123123412345

后端 未结 4 929
日久生厌
日久生厌 2020-11-28 16:11

Basically I want to generate a sequence, say:

n is 2, the sequence will be 112
n is 3, sequence is 112123
n is 5, sequence is 112123123412345

I did

相关标签:
4条回答
  • 2020-11-28 16:56

    I misread the question as "how to generate that annoying puzzler sequence," which goes 1,11,21,1112,3112,... :-). So I figured I might as well write a solution to that.

    puzseq<-function(seqlen) {
    theseq<- list(1)
    for( j in 2:seqlen) {
    
    thetab<-table(theseq[[j-1]])
    theseq[[j]]<-unlist( sapply( 1:length(thetab), function(k) c(thetab[k], as.numeric(names(thetab)[k])) ))
    }
    return(theseq)
    }
    
    0 讨论(0)
  • 2020-11-28 17:00

    It'd do something like:

    do.call('c', sapply(1:5, seq, from = 1))
    # [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
    
    0 讨论(0)
  • 2020-11-28 17:03

    Use sequence:

    > sequence(1:5)
     [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
    
    0 讨论(0)
  • 2020-11-28 17:07

    Here is one possibility:

    n<-5
    unlist(lapply(1:n,function(x) 1:x))
    ## [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
    
    0 讨论(0)
提交回复
热议问题