Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:50:27+00:00 2026-06-14T11:50:27+00:00

The code below passed tests on Ruby 1.8/1.9, but when I run these tests

  • 0

The code below passed tests on Ruby 1.8/1.9, but when I run these tests on Ruby 1.9.2, I’m getting no method errors for Array#sum. For example,

NoMethodError: undefined method `sum' for [3.2, 3.0, 1.5, 0.73, 0.47, 0.23]:Array

I came across inject(:+), but when I tried to substitute it in where sum was, it created other problems. There’s two methods where sum is used, time_required and balance_queues. In the second method, it was complicated to work it into the old code q1.sum - q2.sum. As much detail/explanation would be helpful.

class FairDistribution
  def initialize(jobs, num_of_queues)
    @queues = [ jobs.sort.reverse ]
    (num_of_queues - 1).times { @queues << [] }

    # Balance the queues until they are perfectly balanced
    while !balance_all_queues do; end
  end

  # Time required for all queues processing
  def time_required
    @queues.map { |q| q.sum }.max                      #SUM
  end

  # The actual distribution of jobs across the queues
  def distribution
    @queues
  end

  private

  # Runs through all queues and balances them against each other.
  # Makes one pass only and returns FALSE if there was nothing changed
  # during the pass.
  def balance_all_queues
    updated = false

    @queues.each_with_index do |q1, qi1|
      (qi1+1 ... @queues.size).each do |qi2|
        res = balance_queues(q1, @queues[qi2])
        updated ||= res
      end
    end

    return !updated
  end

  # Balances the two queues between themselves by finding the best possible
  # swap of jobs between them. If there's nothing to be improved, returns FALSE.
  def balance_queues(q1, q2)
    delta =  q1.sum - q2.sum                            #SUM
    return false if delta == 0

    best_swap       = nil
    best_swap_delta = delta.abs

    q1.each_combination do |c1|
      best_swap, best_swap_delta = choose_better_swap(c1, [], delta, best_swap, best_swap_delta)

      q2.each_combination do |c2|
        best_swap, best_swap_delta = choose_better_swap(c1, c2, delta, best_swap, best_swap_delta)
      end
    end

    best_swap.apply(q1, q2) unless best_swap.nil?

    return !best_swap.nil?
  end

  # Sees if the swap we have at hand is better than our current best
  # swap and replaces the latest if it is.
  def choose_better_swap(c1, c2, delta, best_swap, best_swap_delta)
    unless c1 == c2
      s = Swap.new(c1, c2, delta)
      best_swap, best_swap_delta = s, s.delta if s.delta < best_swap_delta 
    end

    return best_swap, best_swap_delta
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:50:28+00:00Added an answer on June 14, 2026 at 11:50 am

    Enumerable#sum is provided by ActiveSupport (part of Ruby on Rails). If you have the active_support gem installed, you can use [].sum by adding this to the top of your script:

    require 'active_support/core_ext/enumerable'
    

    Try using .inject(0, :+). This will result in ‘0’ for an empty array, and might have been the cause of your problems with inject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run some unit tests on the method below I am
I am using the below example code and have a problem where I can't
I have a question about using form_dropdown() . The code below works, but i
I have the below code to loop through and run an external file. I
I tried code below in .NET 3.5 but mi is null. How to call
The code pasted below was taken from Javadocs on HttpURLConnection . I get the
The code pasted below gets an exception on the line that says... InputStream in
The code pasted below is a simple JSF program, with an idea of having
I have a plot (sample code pasted below) that I am trying to add
Firstly sorry for the long piece of code pasted below. This is my first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.