Skip to content

Commit

Permalink
Added Guillaume Petit submission, and result.txt file. Ran with Ruby …
Browse files Browse the repository at this point in the history
…1.9.1
  • Loading branch information
ashbb committed Feb 11, 2010
1 parent b68afc0 commit b492bc2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 10_GuillaumePetit/fair_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Author : Guillaume Petit

class Array

def sum
inject(0.0) { |memo, value| memo + value }
end

end

class FairDistribution

attr_reader :distribution

def initialize(jobs, nb_of_presses)
@jobs = jobs.sort { |a, b| b <=> a }
@nb_of_presses = nb_of_presses

@ideal_balance = @jobs.sum / @nb_of_presses

balance # the real deal
end

def time_required
@distribution.map { |distrib| distrib.sum }.max
end

private

def balance
@distribution = (1..@nb_of_presses).map { [] }

@jobs.each do |job|
@distribution[find_place_for(job)] << job
end
end

# try to find a place (distribution) by first looking for an "ideal" place
# and if not found, use the smallest simulated time
def find_place_for(job)
simulated_times = @distribution.map { |distrib| distrib.sum + job }
simulated_times.index(@ideal_balance) || simulated_times.each_with_index.min.last
end

end
7 changes: 7 additions & 0 deletions 10_GuillaumePetit/result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
***** FULL TEST REQUESTED *****
Loaded suite test_solution_acceptance
Started
.....
Finished in 0.000000 seconds.

5 tests, 8 assertions, 0 failures, 0 errors, 0 skips

0 comments on commit b492bc2

Please sign in to comment.