Groovy web console

subscribe to the feed Subscribe
to this
site
Sequences (via #groovywebconsole)
tweet this snippet Tweet
this
script

Sequences

Published 3 weeks ago by Nerijus
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts


def getSequences(List<Integer> ints){
    def main = [];
    def sub = [];
    for(int i=0; i< ints.size(); i++){
        if(sub.isEmpty() || sub.get(sub.size()-1)==ints.get(i)-1){
           sub.add(ints.get(i));
        }else{  
            main.addAll(getSequences(ints.subList(i,ints.size())));
            break;
        }
    }
    main.add(sub);
    return main
}


println getSequences([1,2,3,5,6,7,11,101,102]).reverse()