I have the following recursive function to count the number of ways change can be returned given various coin denominations:
function makeChange(amount, coins) {
// Note: using Floats here will not work
console.log(`Amount: ${amount}, Coins: ${coins}`);
return (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
makeChange(amount-coins[0], coins)
+ makeChange(amount, coins.slice(1));
}
console.log(
makeChange(11, [7,3,1])
);
However, I'd like to improve the visual-aspect of the debugging to really see what might be going on behind the scenes of the recursive function -- almost like each level of the stack and how to show that. I've improved it to pass a level parameter so I can do indentation and I then have:
function makeChange(amount, coins, level=0) {
console.log(`${' '.repeat(level)}Amount: ${amount}, Coins: ${coins}`);
return (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
makeChange(amount-coins[0], coins, level+1)
+ makeChange(amount, coins.slice(1), level+1);
}
console.log(
makeChange(10, [5,1])
);
But even this is a bit difficult to understand as it has so much superfluous input. What might be a better way to add in various debug helpers to better visualize this?
One possibility is just to also track the returns and use a bit more formatting to group together the events at a single level.
You can run this here, but you will need to open the browser console to see the results, as there are too many lines for StackOverflow's one:
function makeChange(amount, coins, level=0) {
console.log(`${'| '.repeat(level)}makeChange(${amount}, [${coins.join (', ')}])`);
const result = (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
makeChange(amount-coins[0], coins, level+1)
+ makeChange(amount, coins.slice(1), level+1);
console.log(`${'| '.repeat(Math .max(level - 1, 0))}${level > 0 ? '| ' : ''}+--> ${result}`);
return result;
}
console .log (
makeChange(11, [7, 3, 1])
);
.as-console-wrapper {max-height: 100% !important; top: 0}
It will give you output like this:
makeChange(11, [7, 3, 1])
| makeChange(4, [7, 3, 1])
| | makeChange(-3, [7, 3, 1])
| | +--> 0
| | makeChange(4, [3, 1])
| | | makeChange(1, [3, 1])
| | | | makeChange(-2, [3, 1])
| | | | +--> 0
| | | | makeChange(1, [1])
| | | | | makeChange(0, [1])
| | | | | +--> 1
| | | | | makeChange(1, [])
| | | | | +--> 0
| | | | +--> 1
| | | +--> 1
| | | makeChange(4, [1])
| | | | makeChange(3, [1])
| | | | | makeChange(2, [1])
| | | | | | makeChange(1, [1])
| | | | | | | makeChange(0, [1])
| | | | | | | +--> 1
| | | | | | | makeChange(1, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(2, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(3, [])
| | | | | +--> 0
| | | | +--> 1
| | | | makeChange(4, [])
| | | | +--> 0
| | | +--> 1
| | +--> 2
| +--> 2
| makeChange(11, [3, 1])
| | makeChange(8, [3, 1])
| | | makeChange(5, [3, 1])
| | | | makeChange(2, [3, 1])
| | | | | makeChange(-1, [3, 1])
| | | | | +--> 0
| | | | | makeChange(2, [1])
| | | | | | makeChange(1, [1])
| | | | | | | makeChange(0, [1])
| | | | | | | +--> 1
| | | | | | | makeChange(1, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(2, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | +--> 1
| | | | makeChange(5, [1])
| | | | | makeChange(4, [1])
| | | | | | makeChange(3, [1])
| | | | | | | makeChange(2, [1])
| | | | | | | | makeChange(1, [1])
| | | | | | | | | makeChange(0, [1])
| | | | | | | | | +--> 1
| | | | | | | | | makeChange(1, [])
| | | | | | | | | +--> 0
| | | | | | | | +--> 1
| | | | | | | | makeChange(2, [])
| | | | | | | | +--> 0
| | | | | | | +--> 1
| | | | | | | makeChange(3, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(4, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(5, [])
| | | | | +--> 0
| | | | +--> 1
| | | +--> 2
| | | makeChange(8, [1])
| | | | makeChange(7, [1])
| | | | | makeChange(6, [1])
| | | | | | makeChange(5, [1])
| | | | | | | makeChange(4, [1])
| | | | | | | | makeChange(3, [1])
| | | | | | | | | makeChange(2, [1])
| | | | | | | | | | makeChange(1, [1])
| | | | | | | | | | | makeChange(0, [1])
| | | | | | | | | | | +--> 1
| | | | | | | | | | | makeChange(1, [])
| | | | | | | | | | | +--> 0
| | | | | | | | | | +--> 1
| | | | | | | | | | makeChange(2, [])
| | | | | | | | | | +--> 0
| | | | | | | | | +--> 1
| | | | | | | | | makeChange(3, [])
| | | | | | | | | +--> 0
| | | | | | | | +--> 1
| | | | | | | | makeChange(4, [])
| | | | | | | | +--> 0
| | | | | | | +--> 1
| | | | | | | makeChange(5, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(6, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(7, [])
| | | | | +--> 0
| | | | +--> 1
| | | | makeChange(8, [])
| | | | +--> 0
| | | +--> 1
| | +--> 3
| | makeChange(11, [1])
| | | makeChange(10, [1])
| | | | makeChange(9, [1])
| | | | | makeChange(8, [1])
| | | | | | makeChange(7, [1])
| | | | | | | makeChange(6, [1])
| | | | | | | | makeChange(5, [1])
| | | | | | | | | makeChange(4, [1])
| | | | | | | | | | makeChange(3, [1])
| | | | | | | | | | | makeChange(2, [1])
| | | | | | | | | | | | makeChange(1, [1])
| | | | | | | | | | | | | makeChange(0, [1])
| | | | | | | | | | | | | +--> 1
| | | | | | | | | | | | | makeChange(1, [])
| | | | | | | | | | | | | +--> 0
| | | | | | | | | | | | +--> 1
| | | | | | | | | | | | makeChange(2, [])
| | | | | | | | | | | | +--> 0
| | | | | | | | | | | +--> 1
| | | | | | | | | | | makeChange(3, [])
| | | | | | | | | | | +--> 0
| | | | | | | | | | +--> 1
| | | | | | | | | | makeChange(4, [])
| | | | | | | | | | +--> 0
| | | | | | | | | +--> 1
| | | | | | | | | makeChange(5, [])
| | | | | | | | | +--> 0
| | | | | | | | +--> 1
| | | | | | | | makeChange(6, [])
| | | | | | | | +--> 0
| | | | | | | +--> 1
| | | | | | | makeChange(7, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(8, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(9, [])
| | | | | +--> 0
| | | | +--> 1
| | | | makeChange(10, [])
| | | | +--> 0
| | | +--> 1
| | | makeChange(11, [])
| | | +--> 0
| | +--> 1
| +--> 4
+--> 6
6
It's not beautiful, but it's not too bad.
It seems like the following would be useful to potentially print depending on the verbosity level of the script:
The stack level. Of this can be done using tabs or something similar rather than just "Level=1".
Whether the base case is reached or not. For example, the above function might only reach the base case three times out of 1000 recursive function calls.
The variables that the function is applied with.
With the above, we can improve the function to something like the following:
function makeChange(amount, coins, verbose) {
console.log(`Changing ${amount}...`);
return (function _makeChange(amount, coins, verbose, debugLevel=0, debugCoinsUsed=[]) {
if (verbose || amount===0)
console.log(`${' '.repeat(debugLevel)}Amount: ${amount}, Coins: ${coins}${ amount===0? ' ==> ['+ debugCoinsUsed + ']' :''}`);
return (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
_makeChange(amount-coins[0], coins, verbose, debugLevel+1, [...debugCoinsUsed, coins[0]])
+ _makeChange(amount, coins.slice(1), verbose, debugLevel+1, [...debugCoinsUsed]);
})(amount, coins, verbose);
}
console.log(
makeChange(10, [5,1]),
makeChange(10, [5,1], true)
);
And the non-verbose call would log:
makeChange(10, [5,1], false)
Changing 10...
Amount: 0, AvailableCoins: 5,1 ==> [5,5]
Amount: 0, AvailableCoins: 1 ==> [5,1,1,1,1,1]
Amount: 0, AvailableCoins: 1 ==> [1,1,1,1,1,1,1,1,1,1]
3
Related
I need to group by month or year in the Date column of the table. and I currently used the group function also. I need to add an additional group condition. Is it possible having two grouping conditions and grouping monthly, yearly in Sequelize? Please if someone is using it, share it with me.
Table
-----------------------------------------------------------
| bulk_id | date | grade_GL | gross_weight | lot_id |
-----------------------------------------------------------
| 100 | 2020-10-02 | A | 100 | 1000 |
| 100 | 2020-10-02 | B | 120 | 1002 |
| 100 | 2020-10-02 | C | 140 | 1004 |
| 101 | 2020-10-22 | A | 210 | 1006 |
| 101 | 2020-10-22 | B | 230 | 1008 |
| 101 | 2020-10-22 | C | 250 | 1010 |
| 102 | 2020-11-03 | A | 110 | 1013 |
| 102 | 2020-11-03 | B | 130 | 1015 |
| 102 | 2020-11-03 | C | 200 | 1017 |
| 103 | 2020-11-23 | A | 220 | 1019 |
| 103 | 2020-11-23 | B | 240 | 1021 |
| 103 | 2020-11-23 | C | 150 | 1023 |
Expected Result
------------------------------------------------
| grade_GL | month | sum_of_gross_weight |
------------------------------------------------
| A | October | 310 |
| B | October | 350 |
| C | October | 390 |
| A | November | 330 |
| B | November | 370 |
| C | November | 350 |
** When bulk_Id 100,101,102,103 is given expected result should be above table.
Here is my current Sequeilize:
const gradeWiseTotalLots = await Lot.findAll({
attributes: ['grade_GL', [sequelize.fn('sum', sequelize.col('gross_weight')), 'total_Gross_weight'],],
where: {BulkBulkId: bulk_id_ele.dataValues.bulk_id,},
group: ['grade_GL']
});
Select statement I would expect is:
SELECT `grade_GL`, sum(`gross_weight`) AS `total_Gross_weight` FROM `Lots` AS `Lot` WHERE `Lot`.`BulkBulkId` = 103 GROUP BY `grade_GL`,`month`;
I'm setting some new configurations to jest, I got ignore some files like *.stories.js, but when I use *.js.snap or *.snap, the jest don't work well.
I'm using react-scripts, where the same use jest
So for example, if I only ignore *.stories.js like this below command:
react-scripts test --coverage --collectCoverageFrom=!src/**/*.stories.js
------------------------------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
------------------------------------------------|----------|----------|----------|----------|-------------------|
All files | 92.98 | 90 | 96.43 | 94.34 | |
components/atoms/grid | 100 | 100 | 100 | 100 | |
Grid.js | 100 | 100 | 100 | 100 | |
index.js | 0 | 0 | 0 | 0 | |
components/atoms/grid/__snapshots__ | 0 | 100 | 100 | 0 | |
Grid.test.js.snap | 0 | 100 | 100 | 0 | 1 |
components/atoms/grid/components | 100 | 100 | 100 | 100 | |
Cell.js | 100 | 100 | 100 | 100 | |
index.js | 0 | 0 | 0 | 0 | |
components/atoms/grid/components/__snapshots__ | 0 | 100 | 100 | 0 | |
Cell.test.js.snap | 0 | 100 | 100 | 0 | 1 |
components/atoms/grid/helpers | 0 | 0 | 0 | 0 | |
calcOffset.js | 0 | 0 | 0 | 0 | |
index.js | 0 | 0 | 0 | 0 | |
components/atoms/text | 100 | 100 | 100 | 100 | |
Text.js | 100 | 100 | 100 | 100 | |
index.js | 0 | 0 | 0 | 0 | |
components/atoms/text/__snapshots__ | 0 | 100 | 100 | 0 | |
Text.test.js.snap | 0 | 100 | 100 | 0 | 1 |
helpers | 100 | 100 | 100 | 100 | |
breakpoints.js | 100 | 100 | 100 | 100 | |
calcPercent.js | 100 | 100 | 100 | 100 | |
index.js | 0 | 0 | 0 | 0 | |
provider | 100 | 50 | 100 | 100 | |
style.js | 100 | 50 | 100 | 100 | 9 |
theme | 87.5 | 100 | 50 | 100 | |
GlobalStyle.js | 100 | 100 | 100 | 100 | |
colors.js | 100 | 100 | 100 | 100 | |
index.js | 83.33 | 100 | 50 | 100 | |
------------------------------------------------|----------|----------|----------|----------|-------------------|
But if I add *.js.snap don't find the other files that doesn't is snap
react-scripts test --coverage --collectCoverageFrom=!src/**/*.stories.js --collectCoverageFrom=!src/**/*.js.snap
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|
I solved did add the configuration inside package.json
"jest": {
"collectCoverageFrom": [
"!<rootDir>/src/**/*.stories.js",
"src/**/*.{js,jsx}",
"!<rootDir>/node_modules/"
]
},
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
var arr = [{id: 1, name: 'John'}, {id: 2, name: 'Mike'}];
var member = arr[0];
arr.splice(0, 1);
console.log(member);
You can keep accessing it because you still have a reference to it. It's no longer in the array, but it still exists. It will continue to exist until all references to it are dropped, and when they are, it becomes eligible for garbage collection.
After this:
var arr = [{id: 1, name: 'John'}, {id: 2, name: 'Mike'}];
you have something like this in memory (several details omitted):
+−−−−−−−−−−−−+
arr:Ref4875−−−−−−>| (array) |
+−−−−−−−−−−−−+ +−−−−−−−−−−−−−−+
| 0: Ref8612 |−−−−−>| (object) |
| 1: Ref4631 |−−+ +−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ | | id: 1 |
| | name: "John" |
| +−−−−−−−−−−−−−−+
|
| +−−−−−−−−−−−−−−+
+−−>| (object) |
+−−−−−−−−−−−−−−+
| id: 1 |
| name: "John" |
+−−−−−−−−−−−−−−+
(The "Ref1234" values are called object references. You never see their actual values, but you can think of them as a number the JavaScript engine uses to look up where the object is in memory.)
Then after this:
var member = arr[0];
both member and the array refer to the object, something like this:
member:Ref8612−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ |
arr:Ref4875−−−−−−>| (array) | |
+−−−−−−−−−−−−+ | +−−−−−−−−−−−−−−+
| 0: Ref8612 |−−−+−>| (object) |
| 1: Ref4631 |−−+ +−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ | | id: 1 |
| | name: "John" |
| +−−−−−−−−−−−−−−+
|
| +−−−−−−−−−−−−−−+
+−−>| (object) |
+−−−−−−−−−−−−−−+
| id: 1 |
| name: "John" |
+−−−−−−−−−−−−−−+
Then after:
arr.splice(0, 1);
the array no longer has a reference to it, but member still does:
+−−−−−−−−−−−−−−+
member:Ref8612−−−−−−−−−−−−−−−−−−−−−−−>| (object) |
+−−−−−−−−−−−−+ +−−−−−−−−−−−−−−+
arr:Ref4875−−−−−−>| (array) | | id: 1 |
+−−−−−−−−−−−−+ | name: "John" |
| 0: Ref4631 |−−+ +−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ |
|
|
|
|
| +−−−−−−−−−−−−−−+
+−−>| (object) |
+−−−−−−−−−−−−−−+
| id: 1 |
| name: "John" |
+−−−−−−−−−−−−−−+
I have these data in mysql.
| code | date | value |
| A | 2016-04-04 00:00:00 | 0.1 |
| B | 2016-04-04 00:00:02 | 0.5 |
| C | 2016-04-04 00:00:05 | 1 |
| A | 2016-04-04 00:11:00 | 0.2 |
| B | 2016-04-04 00:12:25 | 0.6 |
| C | 2016-04-04 00:15:30 | 0.4 |
and i want adjust and send a data to json using nodejs/expressjs
with daily, monthly, yearly sum.
First i try this query.
SELECT date, value FROM 'table'
WHERE code = 'A'
AND date >= '2016-04-05 00:00:00'
GROUP BY DATE_FORMAT(date, "%y-%m-%d-%H") // or "%y-%m-%d", "%y-%m"
ORDER BY date;
this query runs well. but this is wasteful i think.
because repeatedly querying for number of codes.
Output:
+---------------------+-------+
| date | value |
+---------------------+-------+
| 2016-04-05 00:01:56 | 0 |
| 2016-04-05 01:01:56 | 0 |
| 2016-04-05 02:01:58 | 0 |
| 2016-04-05 03:01:57 | 0 |
| 2016-04-05 04:01:58 | 0 |
| 2016-04-05 05:01:58 | 0 |
| 2016-04-05 06:01:59 | 0 |
| 2016-04-05 07:01:58 | 0 |
| 2016-04-05 08:01:58 | 0 |
| 2016-04-05 09:01:59 | 0 |
| 2016-04-05 10:01:59 | 0.009 |
| 2016-04-05 11:02:00 | 0.007 |
+---------------------+-------+
Can i get all data with one query?
Finally, i want send this form of data with expressjs.
[
A: [{ date: '2016-04-04 00:00:00', sum: 4},
{ date: '2016-04-04 00:01:00', sum: 6}],
B: [{ date: '2016-04-04 00:00:00', sum: 4},
{ date: '2016-04-04 00:01:00', sum: 6}]
]
or this form
[
{ date: '2016-04-04 00:00:00', A: 4, B: 4 },
{ date: '2016-04-04 00:01:00', A: 4, B: 4 }
]
i've try mysql query, sequelize, lodash.
but i can't find right way.
You can use a subquery to format your date so you can group on it. Example with daily sums:
SELECT code, date, sum(value) as value
FROM (select code, DATE_FORMAT(date, "%y-%m-%d") as date, value from t1) as t2
GROUP BY code, date
ORDER BY date
The result:
"A";"16-04-04";"0.30000000447034836"
"B";"16-04-04";"1.100000023841858"
"C";"16-04-04";"1.4000000059604645"
hello all i'm very new to angular js.i'm having the stores with different id i need to show the report by date followed by the store id.here is the json data i'm having.i went to the previous questions from stack over flow,but i didn't get the required o/p.help me
My Controller Code
var app = angular.module('app',[]);
app.controller('TestCtrl',['$scope',function($scope){
var items =[
{store:'116',date: "14/11/2015", deptnames : "alchol", value : "-7" },
{store:'116',date: "14/11/2015", deptnames : "tobacoo", value : "98" },
{store:'116',date: "14/11/2015", deptnames : "beer", value : "-5" },
{store:'116',date: "14/11/2015", deptnames : "candy", value : "93" },
{store:'116',date: "15/11/2015", deptnames : "alchol", value : "-7" },
{store:'116',date: "15/11/2015", deptnames : "tobacoo", value : "98" },
{store:'116',date: "15/11/2015", deptnames : "beer", value : "-5" },
{store:'116',date: "15/11/2015", deptnames : "candy", value : "93" },
{store:'117',date: "14/11/2015", deptnames : "alchol", value : "2" },
{store:'117',date: "14/11/2015", deptnames : "tobacoo", value : "8" },
{store:'117',date: "14/11/2015", deptnames : "beer", value : "5" },
{store:'117',date: "14/11/2015", deptnames : "candy", value : "9" },
{store:'118',date: "15/11/2015", deptnames : "alchol", value : "-7" },
{store:'118',date: "15/11/2015", deptnames : "tobacoo", value : "98" },
{store:'118',date: "15/11/2015", deptnames : "beer", value : "-5" },
{store:'118',date: "15/11/2015", deptnames : "candy", value : "93" },
]
$scope.headCells = _.keys(_.groupBy(items, function(item){ return item.deptnames}));
$scope.rows = _.groupBy(items, function(item){ return item.date});
$scope.rowsstore = _.groupBy(items, function (item) { return item.store });
$scope.sortByYearProp = function(values){
return _.sortBy(values, function(value){
return values.deptname;
});
}
}])
My HTML
<table ng-controller="TestCtrl as test">
<tr><th>BDate</th><th>location</th><th ng-repeat="deptnames in headCells">{{deptnames}}</th></tr>
<tr data-ng-repeat="(date, value) in rows">
<td>{{date}}</td>
<td data-ng-repeat="(store,value) in rowsstore">{{store}} </td>
<td ng-repeat="obj in sortByYearProp(value)">{{obj.value}}</td>
</tr>
</table>
actual op like dis
+
| | | | | | | | | | | | |
+------------+----------+--------+---------+------+-------+----+----+----+----+----+----+
| BDate | location | alchol | tobacoo | beer | candy | | | | | | |
| 14/11/2015 | 116 | 117 | 118 | -7 | 98 | -5 | 93 | 2 | 8 | 5 | 9 |
| 15/11/2015 | 116 | 117 | 118 | -7 | 98 | -5 | 93 | -7 | 98 | -5 | 93 |
+------------+----------+--------+---------+------+-------+----+----+----+----+----+----+
my required op is
+----------------------------+----------------+--------+---------+------+-------+--+--+--+--+--+--+
| BDate | location | alchol | tobacoo | beer | candy | | | | | | |
| 14/11/2015 | 116 | -7 | 98 | -5 | 93 | | | | | | |
| 14/11/2015 | 117 | 2 | 8 | 5 | 9 | | | | | | | | |
| 15/11/2015 | 116 | -7 | 98 | -5 | 93 | | | | | | |
| 15/11/2015 | 118 | -7 | 98 | -5 | 93 | | | | | | |