How to make permutations satisfying conditions using google apps script? - javascript

I'm trying to get permutations of a column from one sheet automatically updated in another sheet (of the same spreadsheet) - while satisfying some conditions.
Eg. This is a dummy table with dummy values for example to illustrate my problem/goal.
Sheet 1
a 2 3
b 4 8
c 3 5
d 11 7
e 7 15
I want to have combinations of column1 such that the sum of values of their column2 is say 20 +/- 5 and column3 is 15 +/- 3. In order to achieve this, the combinations can include multiples of one element too - like 1/2 a or 3 b.
So, for example, one set of combinations in Sheet 2 would be
column1 = a + d + 1/2 e
column2 = 16.5
column3 = 17.5
I've only ever done basic coding so far and I really have no idea how to go about this, any help would be much appreciated :(

Related

Auto-increment dates in two cells if date has passed

Overview:
I'm not a programmer but I managed to get some serious coding into a Gsheets to track my teams project, so we have multiple-variable dropdown menus and integration with google calendar to track projects development and all that.
Why I'm at stackoverflow:
I kind of lack the knowledge to start the code from the scratch, I usually find spare parts of code through forums on the internet and clue them together and it usually works surprisingly well, but this time I couldn't find much informtation.
What I need:
I have 5 cells, and we can put as below,
Date start - Date end - date code* - number** - Priority***
*script to add the date range to gcalendar
** & *** The number is an array that's based on the word written on the priority cell, for example: If priority is written Weekly them
the number colunm will show 7 on the cell to the left and them it
goes. (monthly = 30 and blablabla...)
So I'd like to know if someone could give a hand with a script that would work (at least in my head) as following:
If I set the priority to weekly, it will show 7 on the number colunm and them, every time the "Date end" has passed, it will automatically add 7 days to the "Date start" and "Date end" again.
That way I could keep the projects on a loop where I'll be able to track them constatly.
Thanks in advance for any insights provided,
ps: I've seen some posts about this on sql, but I have no idea also on how to take advantage of the proposals that were presented there.
Edit:
Spreadsheet picture
eDIT2:
Spreadsheet with a increment colunm
Pertinent to the data set and description, you probably do not need any VBA as the increment could be achieved by adding +1 to the reference pointing to previous cell. For example, assuming cell A1 is formatted as Date, enter in cell B1: =A1+1 , then in cell C1: =B1+1 and so on. The result should be as shown below
A B C
9/1/2017 9/2/2017 9/3/2017
It could be further extended with simple logic allowing do display incremented value only if the previous cell is not empty, like : =IF(A1,A1+1,"")
In your case, it could be cell F1 containing =IF(E1,E1+1,"").
FYI, the underlying value of Date is just an Integer value (Time is represented as decimal part), so the arithmetic operations could be applied.
More generic solution would be based on the Excel DATE() Worksheet formula as shown in sample shown below (adding 1 mo. to the date entered in cell A1):
=DATE(YEAR(A1), MONTH(A1)+1, DAY(A1))
In order to implement additional logic, you may consider using Excel Worksheet IF() statement like for example, cell B1 containing:
=A1+IF(C1="week",7,1)
A B C
9/1/2017 9/8/2017 week
so based on the IF() condition it will add either 7 days if C1 contains the word "week" or 1 day otherwise. It could be further extended with nested IF().
Hope this will help.

How to Highlight Specific Column Values based on a CSS Class and Binary Digit On/Off Positions

Using Oracle APEX v5.1.2.
Unsure how to tackle the following but I have a table called, flag_defs with the following example data:
ID NAME
------- ------
1 A
2 B
3 C
4 D
5 E
6 F
Based on the above table, I need to display all these names with a report region but not on separate lines but in the following fashion:
A B C D
E F
where I'm only showing 4 names across.
Now based on the following binary convention, say the following:
010010
where the first 0 lines up to ID = 1 and the last 0 in this sequence lines up to ID = 6
Based on this binary sequence, which will be stored within a database field in another table, I need to apply a class called "flag-red" that I will define as color:red;font-weight:bold; to the names that have the ID position set to "1".
So in the above example binary sequence, both "B" and "E" only would receive the class of "flag-red" and would be red/bold within the report region. The others would not.
The same goes with removing the class if the digit "1" is reset back to "0" for that ID.
I would need to iterate through each digit in this field to set the correct class in my report.
I'm assuming I would firstly create a report and assign a span class to each name but unsure if this is the correct approach.
Furthermore, would JavaScript be a better option or stick to SQL
Would appreciate some assistance on how to tackle the above.
I would suggest you to make a report in the following way:
Lets say you have a query which returns some rows with rownum column. Adding a pivot to this query we can turn it into the following:
select *
from (select r, mod(rownum, 6) group_no, floor((rownum - 1)/6) row_id
from (select rownum r
from dual connect by level <= 30) t)
pivot(max(r) for group_no in (1, 2, 3, 4, 5, 0))
order by 1
Now we can join this query with the table which stores binary mask to show. Lets say initial query has columns C1, C2, ... C6with data, and joined table with mask data has columns M1, M2, ... M6. (If it is one column with a mask, we can produce 6 columns using calculating expressions) You can calculate a name of a CSS class in these columns, something like
select ...
case when <expression1> then 'flag-red' else 'flad-green' end M1,
...
Next step is to go to report properties, choose column C1 in the column list, go to Column formatting section, put #M1# in the CSS Classes field:
Also, mark M1, M2, ... M6 columns as hidden.

Assigning numbers to mysql rows depending on their other variables

So I have 2 tables in MYSQL which look like this:
person(ID, name, division, subdivision, number)
label(ID, divsion, subdivision, ordernum)
person table
Persons are inputed via form, where name, division and subdivision are submitted. The ID column is autoincremented, and I'll get to the number column shortly.
label table
The labels are already inputed and can't be modified. There are different types of groups, and inside those divisions, 5 subgdivisions for each (to make it short, lets say car 1/2/3/4/5, plane 1/2/3/4/5, and so on).
Each of these rows have an order number assigned, starting from one.
My objective is to, once all persons are input, order them by the ordernum comparing their group and subgroup data (they can of course be repeated, just one beneath the other) and after this, assign them a number (which is where the number column comes in).
Once the persons are in order by ordernum, I want the numbers to be assigned like this:
The number of persons have to be a multiple of 5. Lets say for the sake of this example, there are 20 persons. I would want the numbers to be assigned like this:
1 2 3 4 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 4
Before I was doing this all with javascript, but I encountered too many problems and the number wasn't recorded in the DB, so I wanted to ask if something like this was possible via PHP.
Thanks in advance!

How to find the amount of each review based on the average and total number of reviews

So I have this problem that I need to work out for work, but I can't figure out a good algorithm. I am given the average for the reviews and the number of total reviews. From those two pieces of information, I need to randomly generate how many 5 star, 4 star, 3 star, 2 star, and 1 star reviews were made. Here are two formulas that might better explain what exactly the problem is.
Given Review Average (x)
Given Total Number of Reviews (y)
Find:
a = # of 5 star reviews
b = # of 4 star reviews
c = # of 3 star reviews
d = # of 2 star reviews
e = # of 1 star reviews
a + b + c + d + e = total # of reviews (KNOWN: y)
(5*a + 4*b + 3*c + 2*d + 1*e) = total number of stars = average(x) * total reviews(y)
This is more of a type of math problem than programming, but I need to be able to write some type of algorithm to get a,b,c,d, and e programatically in JavaScript. Does anyone know of any good algorithms for something like this? Thanks!
You can get the maximum possible points total by making everything a 5-star review. You can reduce this total by one by making one of the 5-star reviews a 4-star review. You could continue reducing this by one until everything is a 4-star review. Again, reduce by one by making a 4-star review a 3-star review...
So there is a way to get every possible points total from all 5-stars to all 1-stars, and you can get some sort of answer for every possible total.
Of course, you don't need to compute this point by point. You can start your search by comparing the points total with all 5-stars, all 4-stars.. all 1-stars and find the smallest total bigger than your target. Then change just enough of whatever star you have settled on to the next smallest value to reach your exact target.
(Of course this will look a bit artificial but you couldn't be creating fake review numbers to push products on a sight because this would be false advertising and sooner or later you would get caught).

Text Grouping Algorithm

Given an arbitrary string of text, the task is to group the text into separate sections of a template. Each section has different min length and max length parameters. A solution can be considered optimal for a section as long as it falls within those bounds. A greedy solution might result in some sections not meeting their minimums, which means the solution as a whole is not acceptable.
I'm having trouble efficiently constructing an algorithm to do this. It seems that a dynamic programming approach might help, but thus far, I haven't been able to couch it in dynamic programming terms. Does anyone have any leads on solving this problem?
function groupText(str, template)
Inputs:
str: a string of text
template: array of JavaScript objects.
One object per section that describes the min/max amount of text allowed
Output:
array: each element corresponds to one section.
The value of the element is the text that is in the section.
As an example, let's define a string str that is equal to "This is a test." We also have a template t. t consists of several sections. Each section s has a minimum and maximum amount of characters allowed. Let's say for this example there are only two sections: s1 and s2. s1 has a minimum of 1 character and a maximum of 100. s2 has a minimum of 10 characters and a maximum of 15. We pass our string str and our template t to a function groupText. groupText must return an array, with each element i corresponding to a section. For example, element 0 will correspond to s1. The value of the element will be the text that has been assigned to the section.
In this example, a solution might be.
s1text = "This "
s2text = "is a test."
If I understood the problem correctly there's no need of any search... just subtract from the total length the sum of the minimum lengths and what remains is the amount to be distributed. Then distribute this amount to each element up to its maximum until nothing is left... in code
var minsum = 0;
for (vsr i=0; i < sections.length; i++)
minsum += sections[i].min_size;
var extra = text.length - minsum;
if (extra < 0) return null; // no solution
var solution = [];
for (var i=0; i < sections.length; i++)
{
var x = sections[i].min_size + extra;
if (x > sections[i].max_size)
x = sections[i].max_size;
solution.push(x);
extra -= x - sections[i].min_size;
}
if (extra > 0) return null; // no solution
return solution;
OK, so here's an ad-hoc, untested algorithm. If it's no good, perhaps it's good enough to goad someone else into a better answer;
Let's have some trial data. Suppose your template comprises 6 sections, which have min,max limits as:
1 - 12
13 - 25
5 - 7
6 - 7
5 - 5
10 - 25
which means that you're going to need a string of at least 40 and at most 81 characters to satisfy your constraints. And therein lies the solution. First, compute a table like this:
40 - 81
39 - 69
26 - 34
21 - 37
15 - 30
10 - 25
in which each row gives the total length of string that can still be partitioned across the 'slots' in your template. Into slot 1 you put text so that you still have between 39 and 69 characters left for the rest of the slots. Into slot 2 you put text so that you still have between 26 and 34 characters. And so on.

Categories

Resources