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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:51:58+00:00 2026-06-08T10:51:58+00:00

Consider the following situation: items = [ { id: 1 attributes: [ { key:

  • 0

Consider the following situation:

items = [
  {
    id: 1
    attributes: [
      { key: a, value: 2 }
      { key: b, value: 3 }
    ],
    requirements: null
  }
  {
    id: 2
    attributes: [
      { key: b, value: 2 }
    ],
    requirements: a > 2
  }
  {
    id: 3
    attributes: [
      { key: a, value: 1 }
      { key: c, value: 1 }
    ],
    requirements: a > 1 and b > 2
  }
  {
    id: 4
    attributes: [
      { key: a, value: 2 }
      { key: d, value: 7 }
    ],
    requirements: b > 5 and h < 10
  }
]

The expected result, adding together (sum) the various attributes is:

result = [
  { key: a, value: 3 }
  { key: b, value: 5 }
  { key: c, value: 1 }
]

As you can observe, there are dependencies (requirements) between objects in the list. In particular, the object with id: 4 (last one of the series) is discarded from the calculation since the condition b > 5 and h < 10 is never checked. On the contrary, the object with id: 2, initially discarded, then falls in the calculation as a result of the object with id: 3 (which, by adding 1 to the attribute a, makes true the condition a > 2).

What is the algorithm needed to obtain the required result having N objects?

Disclaimer: the proposed structure is only an example. You can suggest any changes you believe to achieve the result. I’m working in JavaScript (CoffeeScript) programming language, but any other will be okay.

  • 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-08T10:52:00+00:00Added an answer on June 8, 2026 at 10:52 am

    Let’s start by getting the data in a format we can use. We need to be able to test requirements at will, instead of only when the data object is instantiated:

      {
        id: 4
        attributes: [
          { key: a, value: 2 }
          { key: d, value: 7 }
        ],
        requirements: (sum) -> sum.b > 5 and sum.h < 10
      }
    

    While we’re at it, let’s get the attributes in a more useful state (note that this isn’t strictly necessary, but makes everything simpler):

      {
        id: 4
        attributes: {
          a: 2
          d: 7
        },
        requirements: (sum) -> sum.b > 5 and sum.h < 10
      }
    

    Now I’ll go through the naive algorithm, which is simplest and should fit your needs. Essentially, we’ll keep looping over the data set, testing each one that hasn’t been used yet and adding it to the sum if it passes.

    changed = true
    sum = {}
    init(sum, items)
    while changed
        changed = false
        for item in items
            if !item.used && item.requirements(sum)
                add(sum, item.attributes)
                changed = true
                item.used = true
    

    I’ll let you fill in the add and init functions. The add one should be simple; it adds each element in the second parameter to each element in the first. The init needs to set each element in sum that may be used (tested or added to) to 0.

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

Sidebar

Related Questions

Consider the following (simplistic) situation: CREATE TABLE PARENT ( PARENT_ID INT PRIMARY KEY );
Consider the following situation: N items in the kill ring. The desired item needed
Consider the following situation Code was added to the trunk at revision x A
Consider the following situation: WidgetCompany produced a .NET DLL in 2006 called Widget.dll, version
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
Consider the following situation: I have two android projects named P1 and P2 which
Consider the following situation: public class Storage { public T GetSingleByID<T>(long id) where T
Consider the following situation: I want to replace links in a string, specifically I
Consider the following situation: SomeType *sptr = someFunction(); // do sth with sptr I
Consider the following situation: class MyFoo { public: MyFoo(); ~MyFoo(); void doSomething(void); private: unsigned

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.