Groovy web console

subscribe to the feed Subscribe
to this
site

Teacher helper

Published 1 month ago by cuong@dotochy.com
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def noQuestions = 8       
def bigResults = []
        int round = 1
        while (true) {
            def results = []
            while (results.size() < noQuestions) {
                def item = [
                        A: new Random().nextInt(17) + 1,
                        B: new Random().nextInt(15) + 1,
                        C: new Random().nextInt(13) + 1,
                        D: new Random().nextInt(13) + 1
                ]
                boolean duplicated = false
                int no = 0;
                for (int i = 0; i < results.size() && !duplicated; i++) {
                    def it = results[i]
                    if (no < 1 && (it.A == item.A || it.B == item.B || it.C == item.C || it.D == item.D)) {
                        no++
                    }
                    if (no >= 1) {
                        duplicated = true
                    }
                    if (!duplicated && ((it.A == item.A && it.B == item.B) ||
                            (it.A == item.A && it.C == item.C) ||
                            (it.A == item.A && it.D == item.D) ||
                            (it.B == item.B && it.C == item.C) ||
                            (it.B == item.B && it.D == item.D) ||
                            (it.C == item.C && it.D == item.D))

                    ) {
                        duplicated = true
                    }
                }
                if (!duplicated) {
                    results << item
                }
            }
if (round == 1) {
                bigResults << results
            } else {
                def firstResults = bigResults[0]
                boolean foundItem = false
                for (int i = 0; i < noQuestions && !foundItem; i++) {
                    def newItem = results[i]
                    for (int j = 0; j < noQuestions && !foundItem; j++) {
                        def item = firstResults[j]
                        if ((newItem.A == item.A && newItem.B == item.B) ||
                                (newItem.A == item.A && newItem.C == item.C) ||
                                (newItem.A == item.A && newItem.D == item.D) ||
                                (newItem.B == item.B && newItem.C == item.C) ||
                                (newItem.B == item.B && newItem.D == item.D) ||
                                (newItem.C == item.C && newItem.D == item.D)
                        ) {
                            foundItem = true
                        }
                    }
                }
                if (!foundItem) {
                    bigResults << results
                    break
                }
            }
            round++
        }
        def realResults = []
        realResults.addAll(bigResults[0])
        realResults.addAll(bigResults[1])
        realResults.each {
            println "A$it.A  B$it.B  C$it.C D$it.D"
        }