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 = [6,3,5,5,3,3,].sort()

def s = x.sum()
if (s == 0) {
  println x
  return
}

def r = s % 3
def d = []
for (def i = 0; i < x.size(); i++) {
  if (x[i] % 3 == r) {
    x.remove(i)
    println x
    return
  } else if (x[i] % 3 == (3 - r)) {
    d.add(i)
  }
}
// If we reached this point then we couldn't fix the set by removing one plate
if (d.size() >= 2) {
  x.remove(d[1])
  x.remove(d[0])
  println x
  return
}
println []