Skip to content

Instantly share code, notes, and snippets.

@aquarla
Created September 22, 2014 09:03
Show Gist options
  • Save aquarla/d5020df8633929629c0b to your computer and use it in GitHub Desktop.
Save aquarla/d5020df8633929629c0b to your computer and use it in GitHub Desktop.
要素数が出来るだけ均等になるように、配列をn個に分割する
# -*- coding: utf-8 -*-
class Array
# 配列を要素数が出来るだけ均等になるようにn個に分割
def split_into(n)
sizes = (0...n).collect { |i|
(size % n >= (i+1)) ? (size / n + 1) : (size / n)
}
(0...n).collect { |i| slice(sizes[0,i].inject(0, :+), sizes[i]) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment