Skip to content
loading...

re: Daily Challenge #126 - The Supermarket Line VIEW POST

FULL DISCUSSION
 

Ruby solution: It's not the prettiest, but it works. I'm a Ruby newb so this was some good practice and I won't say no to any pointers or suggestions if I am doing something that's not very "Rubyish" (What word do they use for poorly used Ruby? In Python they say "that's not very "pythonic").

  1
  2 def queueTime(line, till_count)
  3     empty = false
  4     in_progress = []
  5     time_spent = 0
  6     while !empty do
  7         # remove finished customers from in_progress
  8         in_progress = in_progress.reject{ |x| x <= 0 }
  9
 10         # add more customers to in_progress
 11         while in_progress.length < till_count and line.length > 0 do
 12             in_progress.push(line.shift)
 13         end
 14
 15         # check if done, otherwise, keep tracking progress
 16         if in_progress.length == 0 and line.length == 0
 17             empty = true
 18         else
 19             min = in_progress.min
 20             in_progress = in_progress.map{|x| x - min }
 21             time_spent += min
 22         end
 23     end
 24     time_spent
 25 end
code of conduct - report abuse