brewersfan1976

absolute_values_sum_minimization.rb

Jun 17th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.48 KB | None | 0 0
  1. def absoluteValuesSumMinimization(a)
  2.     x = 0
  3.     y = 0
  4.     sum = 0
  5.     smallest = 0
  6.    
  7.     while (x < a.length)
  8.           while (y < a.length)
  9.                 sum = sum + (a[y] - a[x]).abs
  10.                 y = y + 1
  11.           end
  12.        
  13.           if sum < smallest or x == 0 then
  14.              smallest = sum
  15.              lowest_x = a[x]
  16.           end
  17.              
  18.           x = x + 1
  19.           y = 0
  20.           sum = 0
  21.     end
  22.        
  23.     return lowest_x
  24. end
Advertisement
Add Comment
Please, Sign In to add comment