Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.25 KB | None | 0 0
  1. def index(list=[])
  2.     sum_left = 0
  3.     sum_right = list.inject(0,:+)
  4.     list.each_with_index do |value, index|
  5.         sum_right -= value
  6.         return index if sum_right == sum_left
  7.         sum_left += value
  8.     end
  9.     -1
  10. end
  11.  
  12. list = [1,2,3,4,3,2,1]
  13. print index(list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement