Skip to content

Instantly share code, notes, and snippets.

@chasestubblefield
Last active December 25, 2015 07:59
Show Gist options
  • Save chasestubblefield/6943641 to your computer and use it in GitHub Desktop.
Save chasestubblefield/6943641 to your computer and use it in GitHub Desktop.
push the next biggest file onto the smallest partition until no more files
partitions.min_by { |p| p.inject(0) { |combined_weight, file| combined_weight + file_weights[file] } }
.push(files_biggest_first.delete_at(0)) until files_biggest_first.empty?
#!/usr/bin/env ruby
# usage: partition.rb PATTERN N K [WEIGHTS]
#
# splits files matching PATTERN into N partitions, and outputs the Kth one
#
# WEIGHTS is an optional .yml file that contains weights for each file
# If present, we attempt to minimize the combined weight of the largest partition
require 'yaml'
pattern = ARGV[0]
n, k = ARGV[1, 2].map(&:to_i)
weights = ARGV[3]
files = Dir.glob(pattern)
if weights && File.exists?(weights)
file_weights = Hash.new(0).merge(YAML.load_file(weights)) # 0 is default value
files_biggest_first = files.sort { |f1, f2| file_weights[f1] <=> file_weights[f2] }.reverse!
partitions = Array.new(n) { [] }
partitions.min_by { |p| p.inject(0) { |combined_weight, file| combined_weight + file_weights[file] } }
.push(files_biggest_first.delete_at(0)) until files_biggest_first.empty?
puts partitions[k-1]
else
size = (files.length.to_f / n).ceil
puts files[(k - 1) * size, size]
end
@enthal
Copy link

enthal commented Oct 12, 2013

Slick.

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