how to To extract a two-by-two table for a specific event - javascript-objects

function tableFor(event, journal) {
let table = [0, 0, 0, 0];
for (let i = 0; i < journal.length; i++) {
let entry = journal[i], index = 0;
if (entry.events.includes(event)) index += 1;
if (entry.squirrel) index += 2;
table[index] += 1;
}
return table;
}
console.log(tableFor("pizza", JOURNAL));
JOURNAL file
I'm reading eloquent javascript third edition and I find it difficult to understand this code, especially the if conditions. My question is why in the entry.events.includes(event), index should increment by 1 and in entry.squirrel should increment by 2?
I addition, if both ifs in the for loop are true, table[index] += 1 will be incremented or not?

There are 4 states to consider:
If the event in the journal does not contain pizza and is not a squirrel then increment table at index 0
If the event in the journal contains 'pizza' but not squirrel table is incremented at index 1
If the event does not contain 'pizza' but squirrel is true then the table will be incremented at index 2
If BOTH the event contains "pizza" and squirrel is true then the table will be incremented at index 3
This will loop over the JSON object in the JOURNAL file. For each "event" one of these 4 states will be chosen and the table variable will be affected in the above manner.

Related

Couchbase: Create Index on Array containing Array of objects

I am trying to create an index on the following structure:
"creators": [
{
"ag_name": "Travel",
"ag_ids": [
{
"id": "1234",
"type": "TEST"
}
]
}
]
The index that I created is the following:
CREATE INDEX `Test_Index` ON `bucket`((ARRAY(ARRAY [t.ag_name, v] FOR v IN OBJECT_VALUES(t.`ag_ids`) END) FOR t IN `indexed_data`.`pos` END))
WHERE ((SUBSTR0((META().`id`), 0, 2) = "tt") AND (`indexed_data` IS VALUED))
Question
I started using couchbase a couple of hours ago. I was wondering. Is the index that I created correct? I mean it is being created successfully. But I am not sure if it’s covering all the fields including the ones in the substructure array
Query
SELECT META().id
FROM bucket
WHERE SUBSTR0((META().`id`), 0, 2) = "tt"
AND indexed_data.reservation_type = "HOLA"
AND chain_code="FOO1"
AND indexed_data.property_code="BAR1"
AND ANY creator IN indexed_data.creators SATISFIES creator.ag_name="FOO" END
AND ANY creator IN indexed_data.creators SATISFIES (ANY ag in creator.ag_ids SATISFIES ag.id="1234" END AND ANY ag in creator.ag_ids SATISFIES ag.type="TEST" END) END
The only way above query you can have covering index indexed_data.creators ARRAY as whole. Example 1 at https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/indexing-arrays.html#covering-array-index. You can also create ARRAY index one of the field from ARRAY. As you are referencing multiple fields from array you will not able to use Implicit Covering Array Index that described above link
CREATE INDEX ix1 ON bucket (chain_code,indexed_data.reservation_type, indexed_data.property_code, indexed_data.creators )
WHERE SUBSTR0((META().`id`), 0, 2) = "tt";
Also you are doing AND of multiple ANY clauses of same ARRAY. i.e. means it can match with any position in the array If need same position have all matched you should use following query.
SELECT META().id
FROM bucket
WHERE SUBSTR0((META().`id`), 0, 2) = "tt"
AND indexed_data.reservation_type = "HOLA"
AND chain_code="FOO1"
AND indexed_data.property_code="BAR1"
AND (ANY c IN indexed_data.creators
SATISFIES c.ag_name = "FOO"
AND (ANY ag IN c.ag_ids
SATISFIES ag.id = "1234" AND ag.type = "TEST"
END)
END);
I don't know if this is the best way to determine if an index is covering or not, but if you click "Plan" in the Query Workbench, you will see all the various steps visualized. If you see a "Fetch" step, then the index(es) being used are not covering your query.
Further, if you click "Advice", a covering index will be recommended for your query.

IBM ODM - How Iterate over column rows (like in a given example)

How in IBM ODM to iterate through each row of decision table column and insert each value of row into method for calculations and finally check which values returned by method is the smallest.
For example:
1. Step
For every row of column Bn where A = A1 do:
SomeMethod.calculate(B1) return C1 = 10;
SomeMethod.calculate(B2) return C2 = 40;
SomeMethod.calculate(B3) return C3 = 5;
2. Step
And get minimum of Cn values.
In my example it is 5.
Thanks in advance!

Compare two arrays in VueJS with their ID

I have two arrays the first array is gulod_transaction and the second is gulod_medicine. I want to compare the two arrays with there id’s to minus the quantity from the medicine table to the quantity of transaction table. The main problem is when I add the second row first from table medicine to the transaction table. And click the Save Transaction button. In my console.log it’s minus the first row which is 40 from my first table to transaction table quantity which is equal to 10 and the result becomes 30. I want to minus both quantity if they have the same id’s.
for(let index = 0; index < this.gulod_transactions.length; index++) {
if (this.gulod_transactions[index].id === this.gulod_medicines[index].medicine_id) {
this.medicine_id = this.gulod_transactions[index].id;
this.medicine_name = this.gulod_transactions[index].name;
this.medicine_quantity = (parseInt(this.gulod_medicines[index].quantity) - parseInt(this.gulod_transactions[index].quantity));
this.medicine_price = this.gulod_transactions[index].price;
this.gulod_medicine_barangay_id = 1;
console.log(this.gulod_medicines[index].quantity);
console.log(this.gulod_transactions[index].quantity);
console.log(this.medicine_quantity);
}
}

Replace first 4 rows with values from rows 5:8 arcpy

I am trying to replace the values in the first 4 rows of a attribute table with the row values from the next 4 rows 5:8 in an attribute table using arcpy.da.UpdateCursor. Is there a simple way to index rows 1:4 (all columns) and replace with values from rows 5:8.
Here is my code thus far:
targetFC = r"D:\ZOC\POLYPGDIS_MASTER_1.shp"
dsc = arcpy.Describe(sourceFC)
fields = dsc.fields
# List all field names except the OID field
fieldnames = [field.name for field in fields if field.name != dsc.OIDFieldName]
with arcpy.da.UpdateCursor(targetFC, fieldnames) as cursor:
for row in cursor:
row[1:4] = row[5:8]
cursor.updateRow(row)
Thanks
I think the easiest way would be to load the data into a list.
Note if you are working with a RDBMS the row order is not certain so this probably would not work unless you sorted by something.
eight_rows = []
k = 0
with arcpy.da.SearchCursor(targetFC, fieldnames) as rows:
for row in rows:
eight_rows.append(row)
k += 1
if k == 8: break
k = 5
with arcpy.da.UpdateCursor(targetFC, fieldnames) as rows:
for row in rows:
row = eight_rows[k]
k += 1
rows.updateRow(row)
if k == 8: break

How do I make this specific code run faster in Matlab?

I have an array with a set of chronological serial numbers and another source array with random serial numbers associated with a numeric value. The code creates a new cell array in MATLAB with the perfectly chronological serial numbers in one column and in the next column it inserts the associated numeric value if the serial numbers match in both original source arrays. If they don't the code simply copies the previous associated value until there is a new match.
j = 1;
A = {random{1:end,1}};
B = cell2mat(A);
value = random{1,2};
data = cell(length(serial), 1);
data(:,1) = serial(:,1);
h = waitbar(0,'Please Wait...');
steps = length(serial);
for k = 1:length(serial)
[row1, col1, vec1] = find(B == serial{k,1});
tf1 = isempty(vec1);
if (tf1 == 0)
prices = random{col1,2};
data(j,2) = num2cell(value);
j = j + 1;
else
data(j,2) = num2cell(value);
j = j + 1;
end
waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
close(h);
Right now, the run-time for the code is approximately 4 hours. I would like to make this code run faster. Please suggest any methods to do so.
UPDATE
source input (serial)
1
2
3
4
5
6
7
source input (random)
1 100
2 105
4 106
7 107
desired output (data)
SR No Value
1 100
2 105
3 105
4 106
5 106
6 106
7 107
Firstly, run the MATLAB profiler (see 'doc profile') and see where the bulk of the execution time is occuring.
Secondly, don't update the waitbar on every iteration> Particularly if serial contains a large (> 100) number of elements.
Do something like:
if (mod(k, 100)==0) % update on every 100th iteration
waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
Some points:
Firstly it would help a lot if you gave us some sample input and output data.
Why do you initialize data as one column and then fill it's second in the loop? Rather initialize it as 2 columns upfront: data = cell(length(serial), 2);
Is j ever different from k, they look identical to me and you could just drop both the j = j + 1 lines.
tf1 = isempty(vec1); if (tf1 == 0)... is the same as the single line: if (!isempty(vec1)) or even better if(isempty(vec1)) and then swap the code from your else and your if.
But I think you can probably find a fast vecotrized solution if you provide some (short) sample input and output data.

Resources