Groovy web console

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

mountain_view

Published 6 months ago by salman khattak with tags slope high+school+competition
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def l = [1,5,6,8,7,4,11,8,4,3]
println "INPUT $l"
l.eachWithIndex{ num, idx ->
  int count = 1
  int lastslope = 0
  for (int i=idx+1; i<l.size();i++) {
      int nnum = l[i]
      int currslope = nnum-num
      def cansee = count>1?currslope > lastslope:true
      if (cansee) {
        print "$num sees $nnum".padRight(10)
	count++ 
      } else {
        println "$num's view obstructed"
        break
       }
      lastslope=currslope
  }
  println "$num saw $count mountains!"
} 
println "@TODO find the highest count..."
println "@TODO check both directions ..."