Groovy web console

subscribe to the feed Subscribe
to this
site

Divisible by 3

Published 1 month ago by Anonymous
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def x = [3,5,5,]
// Sort to do
println x.sum()
def s = x.sum()
def r = s % 3
def d = []
if (s == 0) {
  println x
  return
} else {
  for (def i = 0; i < x.size(); i++) {
    println x[i]%3
    if (x[i] % 3 == r) {
      x.remove(i)
      println x
      return
    } else if (x[i] % 3 == (3 - r)) {
      d.add(i)
      println "Remove two $d"
    }
  }
  // If we reached this point then we couldn't fix the set by removing one plate
  if (d.size() < 2) {
    println []
    return
  }
  x.remove(d[1])
  x.remove(d[0])
  println x
}