Groovy web console

subscribe to the feed Subscribe
to this
site

trisectdemo

Published 1 month ago by Anonymous
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
@groovy.transform.TypeChecked
def List<Integer> trisectArray(int arraysize) {
    /** Cuts bigarray into 3 smaller arrays: Left/Middle/Right

        Returns 2 indexes: [int splitA, int splitB]:
        * splitA, index of the first element inside MIDDLE smallarray
        * splitB, index of the first element inside RIGHT smallarray
    */
    
    //println "DEBUG: Entering trisectArray(int arraysize=${n})"
    assert 0 <= arraysize;

    def int intdivthird = arraysize.intdiv(3);

    // Let's split into three smaller sets left, middle and right:
    def int lsize = -1;
    def int msize = -1;
    def int rsize = -1;

    // Each smaller set gets a (relatively) fair share.

    // Since we're dividing by 3, we might end up
    // with either 0, 1 or 2 elements left over.
    switch(arraysize % 3) {
        case 0:
            // Nothing left over: each smallset ends up with the same size.
            lsize = intdivthird;
            msize = intdivthird;
            rsize = intdivthird;
            break;
        case 1:
            // 1 element left over: it goes to LEFT smallset.
            lsize = intdivthird + 1;
            msize = intdivthird;
            rsize = intdivthird;
            break;
        case 2:
            // 2 elements left over: 1 to LEFT, 1 to MIDDLE.
            lsize = intdivthird + 1;
            msize = intdivthird + 1;
            rsize = intdivthird;
            break;
        default:
            throw new Exception("Internal error");
            break;
    }
    
    // Make sure we have assigned something to each smallset:
    assert lsize >= 0;
    assert msize >= 0;
    assert rsize >= 0;

    // Make sure we have not assigned too little or too much:
    assert arraysize == (lsize + msize + rsize);

    // Generate indexes of where to cut (offset+size):
    def int splitA =      0 + lsize;
    def int splitB = splitA + msize;
    
    // Make sure our indexes are legally inside bigarray:
    assert      0 <= splitA;
    assert splitA <= splitB
    assert splitB <= arraysize;

    return [splitA, splitB];
}

@groovy.transform.TypeChecked
def void trisectArrayAndPrint(Object[] array){
    //println "DEBUG: Entering trisectArrayAndPrint(Object[] array=${array.inspect()})"
    def List<Integer> splitList = trisectArray(array.size());
    def int splitA = splitList[0];
    def int splitB = splitList[1];

    def int lsize =       splitA -      0
    def int msize =       splitB - splitA
    def int rsize = array.size() - splitB

    println "" + 
        "${array.size()} In:  ${array.inspect()}\t(Single array of size ${array.size()}.)\n" + 
        "${array.size()} Out: " + 
        array[0..<splitA].inspect() +
        array[splitA..<splitB].inspect() +
        array[splitB..<array.size()].inspect() + 

        "\t" + 
        "(Three arrays of sizes ${lsize}/${msize}/${rsize}: " + 
        "with chops @${splitA}/@${splitB}.)";
    println "";
}

@groovy.transform.TypeChecked
def String[] generateArray(int size){
    // println "DEBUG: Entering generateArray(int size=${size})"
    def tempArray = [];
    for (int i=0; i<size; i++){
        def String element = 'e' + i;
        tempArray += element;
    }
    return (tempArray as String[]);
}

@groovy.transform.TypeChecked
def void splitDemo(int n){
    // println "DEBUG: Entering splitDemo(int n=${n.inspect()})"
    for (int i=0; i<n; i++){
        def String[] a = generateArray(i);
        trisectArrayAndPrint(a);
    }
}


println new Date();
splitDemo(10);