Skip to content

Instantly share code, notes, and snippets.

@abacha
Last active December 23, 2015 13:59
Show Gist options
  • Select an option

  • Save abacha/6645699 to your computer and use it in GitHub Desktop.

Select an option

Save abacha/6645699 to your computer and use it in GitHub Desktop.
def reduce(arr, amount)
sample = []
ratio = (arr.count / amount).floor
while sample.count < amount
index = sample.count
inf = index * ratio
sup = (index + 1) * ratio - 1.0
sum = arr[inf..sup].reduce(:+)
sample << sum / sup - inf + 1.0
end
sample
end
@abacha
Copy link
Author

abacha commented Sep 21, 2013

arr = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
reduce(arr, 10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

arr = (1..20).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
reduce(arr, 5)
=> [2.5, 6.5, 10.5, 14.5, 18.5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment