Create sets of unique items that meet a specific value criteria

I'm struggling to find a solution for a spreadsheet on google sheets using JavaScript. I've come across terms like group by and discard, but I can't seem to put together a mental image that would lead to any results.

Within a dataset, I need to identify individual items and groups of items (summed) that match a specified value.

In my data sheet, I have a list with defined values:

item value
1 100.00
2 53.50
3 45.00
4 67.00
5 32.50
6 35.60
7 34.70

I am looking to create groups of items where their total sum ideally equals 100.00. However, a slightly higher sum could also be acceptable, but the ideal is up to (<=).

The expected result logic is as follows:

  • Group 1 = item 1 value 100.00

  • Group 2 = item 4 value 67.00 and item 5 value 32.50 (closest to 100 with a sum of 99.50)

  • Group 3 = item 2 value 53.50 and item 3 value 45.00 (closest to 100 with a sum of 98.50)

  • Group 4 = item 6 value 35.60 + item 7 value 34.70 (combined sum is 70.30)

The order of items or formation of item groups does not matter. Only unique items can be included in each group.

The output should display only the items from each group. For example:

1

4 5

2 3

6 7 

An ideal output would include all results (group, items, total value):

Group 1 item(s) 1 total value 100.00

Group 2 item(s) 4 5 total value 99.50

Group 3 item(s) 2 3 total value 98.50

Group 4 item(s) 6 7 total value 70.30

I have a concept in mind, but I am still figuring out how to execute it.

Action Rule Result
Determine the maximum value of an ITEM as the LIMITER must not be lower Maximum value Display the maximum value. Example: 100
Set the LIMITER as long as it is equal to or less than an ITEM's maximum value <= MaximumValue Example: LIMITER: 100
Check if any ITEM has a value equal to the LIMITER 100 value == LIMIT Assign a sequential group number to items with a value of 100 first
Check if the sum of any ITEMS equals the LIMITER 100 sum of items = value of the limiter, without repeating items already listed Assign a sequential group number to items summed to reach 100
Check if any ITEM is = -0.01 which was searched earlier without repeating items already listed Assign a sequential group number to items with values below the limiter
Check if the sum of any items is = -0.01 which was searched earlier sum of items = value of fetched, without repeating items already listed Assign a sequential group number to items summed with values below the limiter
Continue searches until no more values remain without repeating items already listed Finalize the list with the last available item

Any assistance would be greatly appreciated.

Answer №1

Upon further examination, it seems that this particular query is closely related to the knapsack problem. While I may struggle to articulate it effectively, there are numerous resources available that offer detailed explanations on the subject.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Picking out specific elements from a component that is rendered multiple times in a React application

One of the challenges I face involves a component called card, which is rendered multiple times with different data. Here's how it looks: { this.state.response.reminders.map((item,i=0) =>{ return <Card key={i++} reminder={item} deleteRem={th ...

Exploring the Uses of SystemJS with TypeScript Loader

Can someone help clarify something about this TypeScript plugin for SystemJS? https://github.com/frankwallis/plugin-typescript/ Here is what the plugin does: This SystemJS plugin allows you to import TypeScript files directly and have them compiled in ...

What is the best way to invert the positioning of the li elements to move upwards?

https://i.stack.imgur.com/mZaoS.png Seeking assistance on adjusting the height of bars to start from the bottom and go upwards instead of starting from the top position and going downwards. The JavaScript code below is used to generate the li elements and ...

Injecting CSS styles into dynamically inserted DOM elements

Utilizing Javascript, I am injecting several DOM elements into the page. Currently, I can successfully inject a single DOM element and apply CSS styling to it: var $e = $('<div id="header"></div>'); $('body').append($e); $ ...

Enter information into the TextArea and jQuery dialog

I require assistance in populating a textarea with data obtained from querying a database. I have a jQuery dialog that contains another dialog, inside of which is a textarea. Here is some pseudocode: <MODAL> <modalB> <TextArea>s ...

Using Node.js to send an OSC message via HTTP request

As a newcomer to Node.js, I find myself intrigued by the idea of creating a program that can automatically send an OSC message whenever someone accesses my server through an HTTP request. Currently, I have a basic program set up that sends an OSC message w ...

Embed JavaScript locally within an iframe HTML

When attempting to add HTML code within an iframe to showcase data, everything works as expected. However, the issue arises when trying to include any JavaScript within the iframe, as it doesn't seem to be recognized locally. Below is the example cod ...

What is the best way to attach functions to specific jQuery objects and exclude others?

Imagine having an unordered list <ul>: <ul class="products"> ... </ul> You want to use jQuery to select it and then add custom functions to that specific object. For instance, you wish to include an addProduct(productData) function ...

Displaying altered textContent

I am working with an SVG stored as a string and making some adjustments to it. The final result is being displayed as text within targetDiv. <html lang="en"> <head> <title>Output Modified</title> </head> <body> ...

Enhance TinyMCE functionality to permit text as a primary element within <table> or <tr> tags

I've been struggling for the past three hours, trying out different solutions and searching like crazy on Google. Unfortunately, I have not been able to find a resolution to this particular issue. Issue: TinyMCE is not allowing me to insert text dire ...

Implementing mixin functions in vue.router routes with Vue.js

Is there a way to dynamically change the title of the window based on each route? I have included a meta: { title: ... } object in each child object within the routes: []. Here is an example: routes: [ { path: 'profile/:id', name: 'Prof ...

Sending NodeJS Buffer as form data to Spring Boot in a correct way

I'm facing an issue with my NodeJS application where I am working with an image buffer called qrCode const qrCodeData = Buffer.from(body).toString('base64'); //body received, not sure if base64 is correct f ...

Error occurs when a handlebar helper is nested too deeply

I have set up two handlebar helpers with the names 'outer' and 'inner'. In my template, I am using them as shown below: {{#outer (inner data)}} {{/outer}} However, I am encountering an error in the console related to the inner helper, ...

Using Javascript to populate text input fields with selected values from a dropdown menu

I am in need of assistance with a JavaScript task as I am still relatively new to it. Currently, I have two input text fields that I would like to populate with text based on the selection made from a dropdown menu. For instance... The select field look ...

Having issues with default sorting and searching not functioning in Datatables with Angularjs

Utilizing a directive to facilitate database building once ng-repeat has completed: app.directive('repeatDone', function() { return function(scope, element, attrs) { if (scope.$last) { scope.$eval(attrs.repeatDone); ...

Apply an opacity setting of 0.5 to the specific segment representing 30% of the scrollable div

I have a scrollable container for displaying messages. I would like to apply an opacity of 0.5 to the content in the top 30% of the container, as shown in this image: https://i.stack.imgur.com/NHlBN.png. However, when I tried using a background div with a ...

Having trouble with Axios communicating with the Scryfall API

Trying to fetch data with axios from this specific endpoint, but encountering unexpected errors: Oddly enough, the request returns data successfully when tested using Postman or a browser (GET request), but it's unresponsive otherwise. Here's t ...

Executing a php function on input onchange event

Hey there! I have a input field named result. I am trying to trigger a function whenever something changes in this input. So, I attempted the following: <input onchange="maFonction" id="result" readonly="readonly" type="text" value="0" size = "10" /&g ...

Checkbox form is not updating its state when unchecked

I am encountering an issue with a form checkbox where the checked values are being returned as JSON. Even when I uncheck the box, the JSON still shows the checked value. Concerns about onChange function const updateCheckedValues = (e) => { setCheck ...

Using Jquery to load a css background image with a loader

I am seeking suggestions on how to display a loader between button clicks while waiting for a background image to fully load. Thank you <html > <head> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script type= ...