#README #Run by typing "ruby (filename)". Drive code is already included. #This script adds a method "find_pivot" to the array class which finds the pivot point of any array #and returns the index of that point if it exists or -1 otherwise. class Array def find_pivot raise NoMethodError, 'Wrong type of input!' if self.class != Array low = 0 high = self.length - 1 mid = (low + high) / 2 searches = 0 until searches == (self.length / 2) searches += 1 left_sum = self[low..mid - 1].inject {|sum, i| sum + i} right_sum = self[mid + 1..high].inject{|sum, i| sum + i} if left_sum == right_sum return mid elsif right_sum > left_sum mid += 1 elsif left_sum > right_sum mid -= 1 end end return -1 end end p "[1,4,6,3,2].find_pivot yields " + "#{[1,4,6,3,2].find_pivot}." p "[1,4,6,7,3,2].find_pivot yields " + "#{[1,4,6,7,3,2].find_pivot}."