Last active
December 25, 2015 07:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slick.