Skip to content

Commit

Permalink
Added the first participant's solution and unit test result. Unit tes…
Browse files Browse the repository at this point in the history
…t was done with Ruby 1.9.1.

ps. also added 99_ashbb just as a sample. :)
  • Loading branch information
ashbb committed Jan 26, 2010
1 parent f52ba5a commit 717168f
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 3 deletions.
65 changes: 65 additions & 0 deletions 01_RajeshTripathi/fair_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## This is an approximation algorithm
## Sort the jobs, then start processing on the processor which has the least amount of jobs scheduled
## Provably the max time is (1/processors * (sum of processes) + max process time) if we do the smallest job first
## and if we do Longest time first it is 3/2 approximation
## I am doing Longest here first as this will pass most of tests, of course not all

class FairDistribution
def initialize(jobs, number_of_processes)
@jobs = jobs.sort
@number_of_processes = number_of_processes
@max_sum = @jobs.inject(0) {|sum, i| sum + i}
end

def time_required
solve.first
end

def distribution
solve.last
end

def solve
jobs = Array.new(@jobs)
distribution = []
@number_of_processes.times do
distribution << [jobs.shift]
end

while !jobs.empty?
index = find_least_end_time_index(distribution)
distribution[index] += [jobs.shift]
end
return find_max_end_time(distribution), distribution
end

def get_sum_array(distribution)
sum = []
distribution.each do |processor|
sum += [processor.inject(0) {|sum, i| sum + i}]
end
sum
end

def find_max_end_time(distribution)
sum = get_sum_array(distribution)
sum.sort.last
end

def find_least_end_time_index(distribution)
sum_arr = get_sum_array(distribution)
min_sum = @max_sum
index = 0
min_index = -1

sum_arr.each do |sum|
if sum < min_sum
min_index = index
min_sum = sum
end
index+=1
end

min_index
end
end
31 changes: 31 additions & 0 deletions 01_RajeshTripathi/result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Loaded suite test_solution_acceptance
Started
FFFFF
Finished in 0.000000 seconds.

1) Failure:
test_basic1(FairQueueTest) [test_solution_acceptance.rb:15]:
<110> expected but was
<135>.

2) Failure:
test_basic2(FairQueueTest) [test_solution_acceptance.rb:35]:
<6.33> expected but was
<8.63>.

3) Failure:
test_basic3(FairQueueTest) [test_solution_acceptance.rb:46]:
<3.73> expected but was
<4.97>.

4) Failure:
test_basic4(FairQueueTest) [test_solution_acceptance.rb:66]:
<9> expected but was
<12>.

5) Failure:
test_basic5(FairQueueTest) [test_solution_acceptance.rb:72]:
<3.2> expected but was
<3.67>.

5 tests, 5 assertions, 5 failures, 0 errors, 0 skips
41 changes: 41 additions & 0 deletions 99_ashbb/fair_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ashbb's solution for RPCFN6
class FairDistribution
attr_reader :distribution

def initialize jobs, n
@jobs, @n= jobs, n
max = jobs.inject(:+)
@solution = [max, max]
end

def time_required
100000.times do |i|
random_candidate
time = @candidate.collect{|press| press.inject(:+)}.max
score = @candidate.collect{|press| time - press.inject(:+)}.max
if time <= @solution[0] and score < @solution[1]
@solution = []
@solution << time << score << @candidate
end
end
@distribution = @solution[2].collect{|e| e - [0]}
p @solution[0], @distribution # debug
@solution[0]
end

def random_candidate
@candidate = Array.new(@n){[0]}
@jobs.each do |job|
@candidate[rand @n] << job
end
end
end

=begin
jobs = [0.23, 0.47, 0.73]
number_of_presses = 4
fd = FairDistribution.new(jobs, number_of_presses)
p fd.time_required
p fd.distribution
=end
16 changes: 16 additions & 0 deletions 99_ashbb/result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Loaded suite test_solution_acceptance
Started
110
[[10, 24, 30, 45], [15, 20, 75]]
.6.33
[[5.8], [1.1, 4.4], [2.83, 3.5], [1.0, 4.75]]
.3.73
[[0.23, 2.3, 1.09], [1.5, 1.75, 0.11, 0.27], [0.73, 3.0], [0.47, 3.2]]
.9
[[3, 3, 3], [5, 4], [5, 4]]
.3.2
[[0.23, 0.47, 0.73], [1.5], [3.0], [3.2]]
.
Finished in 18.187500 seconds.

5 tests, 8 assertions, 0 failures, 0 errors, 0 skips
13 changes: 13 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ clusion of the individual challenges.

This README should be updated/changed upon doing so, to reflect the challenge,
sponsor, winner, and other relevant news.


Unit test
---------

C:\RPCFN6> ruby test_solution_acceptance.rb 99_ashbb full > 99_ashbb/result.txt

C:\RPCFN6> cd 99_ashbb

C:\RPCFN6\99_ashbb> ls -l
total 1
-rw-r--r-- 1 asa Administ 986 Jan 26 22:12 fair_distribution.rb
-rw-r--r-- 1 asa Administ 393 Jan 26 22:15 result.txt
6 changes: 3 additions & 3 deletions test_solution_acceptance.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'test/unit'
require 'fair_distribution'
require "#{ARGV[0]}/fair_distribution" # edited by ashbb

#FairDistribution = LowMemoryFairDistribution

Expand Down Expand Up @@ -50,7 +50,7 @@ def test_basic3
# [3.0, 0.73],
# [2.3, 1.09, 0.23],
# [1.75, 1.5, 0.27, 0.11]
end if ARGV[0] == "full" # only use this TC if 'full' is added as an argument.
end if ARGV[1] == "full" # only use this TC if 'full' is added as an argument.

def test_basic4
jobs = [5,5,4,4,3,3,3]
Expand Down Expand Up @@ -115,4 +115,4 @@ def distributions_are_equivalent?(dist1, dist2)
def arrays_have_same_elements?(arr1, arr2)
arr1.size == arr2.size && (arr1 - arr2).empty?
end
end
end

0 comments on commit 717168f

Please sign in to comment.