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
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`;
Basically i want to render my chart as like this
In fusioncharts said, my data should be like this in order to render
"data": [{
"label": "January 21, 2020",
"value": "3"
},
{
"label": "January 22, 2020",
"value": "1"
},
{
"label": "January 23, 2020",
"value": "2"
},
{
"label": "January 24, 2020",
"value": "1"
},
But in my query like this
public function getGraph(){
$ext = \DB::table('checkers')
->where('remarks_id',2)
->join('schedules','schedules.id','=','checkers.schedule_id')
->join('teachers','schedules.teacher_id','=','teachers.id')
->where('schedules.teacher_id',1)
->count();
$date = \DB::table('checkers')
->where('remarks_id',2)
->select(\DB::raw("DATE_FORMAT(checkers.created_at, '%M %d, %Y') date "))
->join('schedules','schedules.id','=','checkers.schedule_id')
->join('teachers','schedules.teacher_id','=','teachers.id')
->get();
return response()->json([
'count' => $ext,
'date' => $date
]);
}
Note remarks_id = 2 and teacher_id is based on the teacher I select.
The result is like this
{
"count": 1,
"date": [
{
"date": "January 21, 2020"
},
{
"date": "January 23, 2020"
}
]
}
It says count : 1 but the correct value is two because there are two dates indicated as absent.
here is my tables included
checkers
+----+-------------+---------+------------+---------------------------+---------------------+------------+------------+
| id | schedule_id | user_id | remarks_id | status | created_at | updated_at | deleted_at |
+----+-------------+---------+------------+---------------------------+---------------------+------------+------------+
| 1 | 1 | 1 | 2 | Round 1 and 2 For sched 1 | 2020-01-21 00:00:00 | NULL | NULL |
| 5 | 3 | 1 | 9 | Schedule 3 | 2020-01-22 00:00:00 | NULL | NULL |
| 6 | 2 | 1 | 2 | Schedule 2 | 2020-01-23 00:00:00 | NULL | NULL |
+----+-------------+---------+------------+---------------------------+---------------------+------------+------------+
When I was going to get the absent, i used `created_at` column for displaying the dates, I used `DATE_FORMAT(created_at)` in order to format it.
Schedules
+----+-----------------+------------+---------+-------------+------------+----------+-----------+
| id | subject_code_id | teacher_id | room_id | school_year | start_time | end_time | day |
+----+-----------------+------------+---------+-------------+------------+----------+-----------+
| 1 | 1 | 1 | 1 | 2020 | 10:30 PM | 12:30 AM | M-T-W-T-F |
| 2 | 2 | 2 | 2 | 2020 | 5:30 PM | 6:30 PM | M-T-W-T-F |
| 3 | 3 | 1 | 3 | 2020 | 10:30 PM | 11:30 AM | SAT |
+----+-----------------+------------+---------+-------------+------------+----------+-----------+
I just want to graph the absences of the employee per day and counting it i dont know why im getting count 1 there are two dates so that means teacher_id = 1 has two absences. as you can see in checkers there is remarks_id = 2 means absent for teacher_id = 1 in schedules.
the teacher_id = 1 has two schedules which is 1 and 3 in the schedules table .. and you can see in checkers there are schedule_id = 1 and 3 that is for teacher_id = 1
How could i make it to display at the line charts? The query seems tricky for me to do. I hope you understand what I mean. And also if the teacher has two schedules and he is absent for the day, his count for that day will be 2 since he got two schedules. Thanks for the help.
You have the right counting. Your query should return 1 not 2 as you assume. From table checkers you are selecting 2 rows with values of field schedule_id which is equal to 1 and 2. Then you selected by this field next 2 rows from table Schedules which contain values 1 and 2 of field teacher_id. But we have condition that we should select from table schedules only rows with value teacher_id = 1 that is why your second select result with field teacher_id = 2 is filtered and you get only one row which satisfies all two where conditions.
I assume you have made some errors when added manually test data to tables. That is why you have other result then you have expected.
You can modify your second sql to something like this:
->select(\DB::raw("COUNT(checkers.id) `value` "), \DB::raw("DATE_FORMAT(checkers.created_at, '%M %d, %Y') label "))
And don't forget to group by and you will get appropriate json for fusioncharts.
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/"
]
},
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 | | | | | | |