infused (owner)

Revisions

gist: 116997 Download_button fork
public
Public Clone URL: git://gist.github.com/116997.git
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'date'
 
exclude_days = [6,7]
total_hours_worked = remaining_hours = 107
max_hours_per_day = 5
dates = (Date.new(2006,2,26)..Date.new(2006,7,26)).to_a
date_log = {}
 
puts "Randomly distributing #{total_hours_worked} hours over #{dates.size} days."
 
while remaining_hours > 0 do
  h = rand(max_hours_per_day+1)
  if rand(2) == 0 && h > 1
    h -= 0.5
  else
    h +- 0.5
  end
  h = remaining_hours if h > remaining_hours
  unless h == 0
    loop do
      day = rand(dates.size + 1) - 1
      unless date_log.has_key?(day) || exclude_days.include?(dates[day].cwday)
        date_log[day] = h
        break
      end
    end
  end
  remaining_hours -= h
end
 
0.upto(dates.size) do |n|
  if date_log.has_key?(n)
    puts "#{dates[n]} #{date_log[n]}"
  end
end