I'm having a hard time finding a solution for a spreadsheet on google sheets, javascript language. I was seeing some things like group by and discard. But I still haven't been able to assemble anything mentally that would come to any result.
I need to find items and groups of items (summed) within a dataset that fits the given defined value.
I have a data sheet with defined values, here is an example list
item
value
1
100.00
2
53.50
3
45.00
4
67.00
5
32.50
6
35.60
7
34.70
I need groups of items to be formed so that their sum is ideally 100.00, but it can be something close to that, even a little higher would be possible, but the ideal would be up to (<=).
Expected result logic:
Group 1 = item 1 value 100.00
Group 2 = item 4 value 67.00 and item 5 value 32.50 (since the sum is
99.50, being the closest to 100)
Group 3 = item 2 value 53.50 and item 3 value 45.00 (since the sum is 98.50, being the closest to 100)
Group 4 = item 6 value 35.60 + item 7 value 34.70 (since the sum is 70.30)
It doesn't matter the order of the items or the formation of the item groups. Only items cannot be repeated.
As an output you would only need the items from each group. Ex:
1
4 5
2 3
6 7
But if it had all the results it would be excellent (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'm thinking this way, but I still haven't found how to do it
Action
Rule
Result
Check the maximum value of an ITEM as the LIMITER cannot be lower
Maximum value
Show maximum value. Ex: 100
Set 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
Set in sequential order (+1 in each cycle) the group number for items with a value of 100 first
check if any sum of ITEM is equal to LIMITER 100
sum of items = value of the limiter, without repeating items already listed
Set in sequential order (+1 in each cycle) the number of the group of items summed with a value of 100
check if any ITEM is = -0.01 which previous search
without repeating items already listed
Set in sequential order (+1 in each cycle) the number of the group of items with values lower than the limiter
check if any sum of items is = -0.01 which previous search
sum of items = value of fetched, without repeating items already listed
Set in sequential order (+1 in each cycle) the group number for summed items with values lower than the limiter
continue searches until no values remain
without repeating items already listed
Set list to last available item
Every help is welcome
I noticed this question is very similar to the knapsack problem. I'm not very good at explaining it, but here are some resources that do a far better job of explaining it than I could.
https://learnersbucket.com/examples/algorithms/knapsack-problem-in-javascript/
Related
This question already has answers here:
How does '&' work in relation to odd and even? In JS
(3 answers)
Closed last month.
I'm working through a problem on CodeSignal and trying to understand some of the solutions that other people have submitted. One of the solutions was as follows, and I don't understand what the ampersand is doing.
(a) => a.reduce((p,v,i) => (p[i&1]+=v,p), [0,0])
The problem is:
Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.
You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.
Example
For a = [50, 60, 60, 45, 70], the output should be
solution(a) = [180, 105].
In this solution, the & operator is used to perform a bitwise AND operation. In JavaScript, the & operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
In the given solution, the & operator is used to determine whether the index i of the current element in the array is even or odd. If i is even, the result of i & 1 will be 0. If i is odd, the result of i & 1 will be 1.
I'm trying to solve a problem and I've been experimenting but can't get it to work.
Suppose I have two columns and I am trying to count the number of rows where the value in the first column are greater than the value in the second column.
So if the values in column A are 2,3,3,5 and the values in column B are 1,3,3,4 then the answer should be 2 because 2 is greater than 1 and 5 is greater than 4.
Any suggestions?
P.S. seems like I'm struggling to define a criterion to compare against that includes a value in a range.
=COUNTIF(ARRAYFORMULA(A1:A4>B1:B4), TRUE)
ARRAYFORMULA(COUNTIF(A1:A4,">"&B1:B4))
I have a master list with 8 items in, then a number of lists with the same items as the master list but where the items appear in a different order. How do I find a percentage similarity between each list with the master list?
For example, the master list might be:
[8,7,6,5,4,3,2,1];
One of the lists I want to compare it against could be:
[8,6,4,2,7,5,3,1];
I know I could just loop through the master list and check for matches, but is there an elegant way to work out how close each number is in the list to the same number in the master list?
For example:
position 0: '8' match in position 0; 0 positions difference (100%)
position 1: '7' match in position 4; 3 positions difference (57.1%)
position 2: '6' match in position 1; 2 positions difference (71.4%)
etc.
The end result would be a percentage similarity between the two lists.
You could use the Array map and reduce functions:
function getSimilaritry(a, b) {
return a.map(function(val, index) {
//calculate the position offset and divide by the length to get each
//values similarity score
var posOffset = Math.abs(b.indexOf(val) - index);
return posOffset/a.length
}).reduce(function(curr, prev) {
//divide the current value by the length and subtract from
//one to get the contribution to similarity
return (1 - curr/a.length) + prev;
});
}
If the lists aren't guaranteed to have the same values, you would need to add handling for that.
Also note that the order you pass the arguments a and b to the getSimilarity function will impact the result. Not clear if this is an issue for your application.
PS: I think your question was down-voted for not including the code you already had trying to solve this problem.
I have 2 tables with 2 sets of data
score - individual name - team name
score 1 name 1 team 1
score 2 name 2 team 1
score 3 name 3 team 1
score - individual name - team name
score 1 name 1 team 2
score 2 name 2 team 2
score 3 name 3 team 2
Each (other than team name, which is only one text input) is a text input where I will input a value.
Lets say the possible score is a value from 1 - 100 and I want to list the players in order from lowest score to highest score in a table on the click of a button. I also want to associate the players name and there team with their score in the list. I know how to set the score in an array using document.getElementsByClassName() and list from least to greatest. What I do not know how to make the players name and team stay with that particular score, even if the score changes and I hit the button again. I would appreciate javascript only, no Jquery unless it is the only way. Thanks for the help!
I have a working app that needs to store up to 4 matrixes of integer data per record. I'm not sure how to get there with Titanium and SQlite.
A record will contain at least 1 but up to 4 matrixes of integers:
The matrix size is variable, each matrix consists of:
1 - 20 rows with 3 columns per row
OR
1 - 20 rows with 6 columns per row
The matrix structure will be identical for each record, i.e. 3 3x20 matrixes in
a record or 4 6x10 matrixes in a record. At this point my app starts, allows the user to choose the matrix parameters then accepts the data entry to fill in the matrix values. The matrixes are actually an JS array of arrays. How can I store an array of arrays and read it back in when I need to?
Edit: Let me see if I can clarify...
The app I'm working on is a scorecard for archery tournaments, similar in concept to a scorecard in golf. In archery you shoot for a set number of ends with a set number of arrows shot per end. The app asks for the number of ends (up to 20) and the number of arrows shot per end (3 or 6). After each shot the archer enters the score (an integer value). so for argument's sake say we're scoring for three ends with three arrows per end. We might see something like this:
arrow scores
8 8 9 (end 1)
7 9 10 (end 2)
9 9 10 (end 3)
There's my matrix that I need to save for this individual record. However, the next tournament I need to score may have a different number of ends and arrows:
arrow scores
7 8 9 10 10 10 (end 1)
10 9 9 7 8 10 (end 2)
9 6 6 6 9 9 (end 3)
7 8 6 7 8 8 (end 4)
10 10 9 8 8 8 (end 5)
Let's simplify and say that I want to store the scorecard for one archer per record. I already have my data entry and score tabulation working. I just don't understand the best way to store matrices as illustrated above.
I would recommend against storing an array of arrays. Generally speaking, its not terribly efficient to write abstractions with code that are meaningful for reasoning about matrices when make array of arrays a firm concept. The only exception I've seen is matlab/octave.
I've always found I end up with simpler code when I flatten the data. With a flat array you'll have to manage the indexing yourself. A few helper functions make that simple to reason about.
I'm not given much info to go on, but I'd assume that putting the data into two different tables will make things simpler.
CREATE TABLE mat3x20 (i1j1, i1j2, i1j2 ....
CREATE TABLE mat6x10 (i1j1, i1j2, i1j2 ....
Otherwise you have some weird behavior flag in the data which will make it less obvious what the code is supposed to be doing somewhere in the call stack.