如何将我的输出转换为 Javascript 中的矩阵
How to convert my output to Matrices in Javascript
我是 javascript.I 的新手,我很困惑如何将我的输出转换为这样的矩阵形式..
从这个输出到
输出
MachineId:M1 Jobs done By this Machine:(j5 = 8),(j4 = 7),(j3 = 1) Time consumed to finish all jobs:16
MachineId:M2 Jobs done By this Machine:(j2 = 9),(j6 = 5),(j1 = 4) Time consumed to finish all jobs:18
MachineId:M3 Jobs done By this Machine:(j7 = 22) Time consumed to finish all jobs:22
MachineId:M4 Jobs done By this Machine:(j8 = 32) Time consumed to finish all jobs:32
应打印此输出
[8,7,1][16],
[9,5,4][18],
[22,0,0][22],
[32,0,0][32]
这是我的实际代码产生第一个提到的输出
// real algorithm
//total Jobs
var _ = require('underscore');
var njobs = [{
jobname: "j1",
time: 4
}, {
jobname: "j2",
time: 9
}, {
jobname: "j3",
time: 1
}, {
jobname: "j4",
time: 7
}, {
jobname: "j5",
time: 8
}, {
jobname: "j6",
time: 5
}, {
jobname: "j7",
time: 22
},
{
jobname: "j8",
time: 32
}
];
//sort this data with time
var sorted = njobs.sort((a, b) => b.time - a.time);
console.log('Jobs Inserted ====================>')
sorted.map(function (data) {
console.log('JobId:'+data.jobname+' Time to finish this Job :'+data.time)
});
//List all the machines
var machines = [
{
id:1,
value:0,
jobs:[],
name:'M1'
},
{
id:2,
value:0,
jobs:[],
name:'M2'
},
{
id:3,
value:0,
jobs:[],
name:'M3'
},
{
id:3,
value:0,
jobs:[],
name:'M4'
}
];
console.log('Machines Available :'+ machines.length);
//Loop it and assign it
sorted.forEach(job => {
var minMachine = machines
.slice(1)
.reduce((res, cur) =>
res.value < cur.value ? res : cur, machines[0]);
minMachine.jobs.push(job);
minMachine.value += job.time;
});
//Prints the value
console.log('Output =====================================>');
machines.map(function (data) {
console.log('MachineId:'+data.name+' Jobs done By this Machine:'+data.jobs.map(data => '('+data.jobname+' = '+data.time+')')+' Time consumed to finish all jobs:'+data.value)
});
var high = Math.max.apply(Math,machines.map(function (o) {
return o.value;
}));
console.log('______________________________________________')
console.log('Highest Time consuming Machine =========>');
var result = _.findWhere(machines,{value:high});
console.log(result.name+' is the highest running Machine '+result.value);
// machines.map(function (){...}).reduce(function (total, curr) { return total + curr; });
将此添加到 JavaScript 的底部。
// Build the multidimensional array.
var arr = [];
var maxJobsPerMachine = 0;
machines.map(function (data) {
var marr = [];
marr.push(data.jobs.map(data => data.time));
maxJobsPerMachine=Math.max(maxJobsPerMachine, marr[0].length);
marr.push([data.value])
arr.push(marr);
});
arr.forEach(e => { while (e[0].length < maxJobsPerMachine) e[0].push(0); });
// Print it.
arr.forEach(e => {
var line = "";
e.forEach(f => line += "[" + f.join(",") + "]");
console.log(line);
});
这是输出:
[8,7,1][16]
[9,5,4][18]
[22,0,0][22]
[32,0,0][32]
我是 javascript.I 的新手,我很困惑如何将我的输出转换为这样的矩阵形式..
从这个输出到
输出
MachineId:M1 Jobs done By this Machine:(j5 = 8),(j4 = 7),(j3 = 1) Time consumed to finish all jobs:16
MachineId:M2 Jobs done By this Machine:(j2 = 9),(j6 = 5),(j1 = 4) Time consumed to finish all jobs:18
MachineId:M3 Jobs done By this Machine:(j7 = 22) Time consumed to finish all jobs:22
MachineId:M4 Jobs done By this Machine:(j8 = 32) Time consumed to finish all jobs:32
应打印此输出
[8,7,1][16],
[9,5,4][18],
[22,0,0][22],
[32,0,0][32]
这是我的实际代码产生第一个提到的输出
// real algorithm
//total Jobs
var _ = require('underscore');
var njobs = [{
jobname: "j1",
time: 4
}, {
jobname: "j2",
time: 9
}, {
jobname: "j3",
time: 1
}, {
jobname: "j4",
time: 7
}, {
jobname: "j5",
time: 8
}, {
jobname: "j6",
time: 5
}, {
jobname: "j7",
time: 22
},
{
jobname: "j8",
time: 32
}
];
//sort this data with time
var sorted = njobs.sort((a, b) => b.time - a.time);
console.log('Jobs Inserted ====================>')
sorted.map(function (data) {
console.log('JobId:'+data.jobname+' Time to finish this Job :'+data.time)
});
//List all the machines
var machines = [
{
id:1,
value:0,
jobs:[],
name:'M1'
},
{
id:2,
value:0,
jobs:[],
name:'M2'
},
{
id:3,
value:0,
jobs:[],
name:'M3'
},
{
id:3,
value:0,
jobs:[],
name:'M4'
}
];
console.log('Machines Available :'+ machines.length);
//Loop it and assign it
sorted.forEach(job => {
var minMachine = machines
.slice(1)
.reduce((res, cur) =>
res.value < cur.value ? res : cur, machines[0]);
minMachine.jobs.push(job);
minMachine.value += job.time;
});
//Prints the value
console.log('Output =====================================>');
machines.map(function (data) {
console.log('MachineId:'+data.name+' Jobs done By this Machine:'+data.jobs.map(data => '('+data.jobname+' = '+data.time+')')+' Time consumed to finish all jobs:'+data.value)
});
var high = Math.max.apply(Math,machines.map(function (o) {
return o.value;
}));
console.log('______________________________________________')
console.log('Highest Time consuming Machine =========>');
var result = _.findWhere(machines,{value:high});
console.log(result.name+' is the highest running Machine '+result.value);
// machines.map(function (){...}).reduce(function (total, curr) { return total + curr; });
将此添加到 JavaScript 的底部。
// Build the multidimensional array.
var arr = [];
var maxJobsPerMachine = 0;
machines.map(function (data) {
var marr = [];
marr.push(data.jobs.map(data => data.time));
maxJobsPerMachine=Math.max(maxJobsPerMachine, marr[0].length);
marr.push([data.value])
arr.push(marr);
});
arr.forEach(e => { while (e[0].length < maxJobsPerMachine) e[0].push(0); });
// Print it.
arr.forEach(e => {
var line = "";
e.forEach(f => line += "[" + f.join(",") + "]");
console.log(line);
});
这是输出:
[8,7,1][16]
[9,5,4][18]
[22,0,0][22]
[32,0,0][32]