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 8122575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:52:41+00:00 2026-06-06T05:52:41+00:00

I spent a bit of time today tackling the equilibrium index problem (described here

  • 0

I spent a bit of time today tackling the equilibrium index problem (described here)

After writing my own solution (which performed badly with large numbers), I decided to find one that would be a perfect score. I found this (which Codility scores as 100/100):

  def equi(a)
    left, right = 0, a.inject(0, &:+)
    indices = []
    a.each_with_index do |val, i|
      right -= val

      indices << i if right == left

      left += val
    end
    indices
  end

What I don’t understand is the piece of parallel assignment and use of inject at the top of the method. Is anyone able to describe what this is doing?

Many thanks!
Stu

  • 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-06T05:52:44+00:00Added an answer on June 6, 2026 at 5:52 am

    It assigns 0 to left and the sum of a‘s elements to right. The 0 argument is there because otherwise an empty array would return nil. The shorthand used for summing is Symbol#to_proc and is unnecessary because inject directly takes a symbol as its argument – inject(0, :+).

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

Sidebar

Related Questions

I've spent a bit of time researching this on here and the mysql site
(except for proxy setup!) I spent some time writing a question here regarding a
I've spent a bit of time writing an application for practice and i've taken
I've spent quite a bit of time on this so here's where I'm stuck.
Spent some time troubleshooting a problem whereby a PHP/MySQL web application was having problems
I've spent a fair bit of time with PHP & Python frameworks and recently
I am totally new to jQuery. I spent quite a bit of time researching
Ok, I've searched all over and have spent quite a bit of my time
Having spent a bit of time learning about functional programming, it's becoming more and
I've spent quite a bit of time on this but seem to be going

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.