noo*_*ere 10 javascript algorithm data-structures
原谅n00b-ish问题,但我是数据结构的新手.我最近被要求将一个给定的数组聚合在另一个数组上,并产生一个基于树的结果.
有人可以给我一些关于如何获得这个输出的指示吗?
INPUT
var T = [
['COUNTRY', 'GENDER', 'MARITAL STATUS', 'SALES'],
['India', 'Female', 'Single', 2400],
['India', 'Male', 'Single', 5200],
['India', 'Female', 'Married', 4300],
['India', 'Male', 'Married', 3200],
['England', 'Female', 'Single', 1600],
['England', 'Female', 'Married', 2000],
['England', 'Male', 'Single', 4800],
['England', 'Male', 'Married', 6400],
];
var A = ['GENDER', 'MARITAL STATUS', 'COUNTRY'];
Run Code Online (Sandbox Code Playgroud)
输出:为每个叶节点使用2*空格.
TOTAL 29900
Female <Female Total>
Single <Single Female Total>
India <Single Female Total India>
England <Single Female Total England>
Married <Married Female Total>
India <Married Female Total India>
England <Married Female Total England>
Male <Male Total>
Single <Single Male Total>
India <Single Male Total India>
England <Single Male Total England>
Married <Married Male Total>
India <Married Male Total India>
England <Married Male Total England>
Run Code Online (Sandbox Code Playgroud)
结果可以用嵌套对象表示,每个内部对象都是一个子树,其总数为:
{
total: 29900,
Female: {
total: 10300,
Single: {
total: 4000,
India: {
total: 2400
},
...
},
...
},
...
}
Run Code Online (Sandbox Code Playgroud)
只需循环遍历所有条目,并将值添加到相应的子树节点.
对于输出,您可以使用JSON.stringify和删除不必要的文本.
警告:下面的剧透
const T = [
['COUNTRY', 'GENDER', 'MARITAL STATUS', 'SALES'],
['India', 'Female', 'Single', 2400],
['India', 'Male', 'Single', 5200],
['India', 'Female', 'Married', 4300],
['India', 'Male', 'Single', 3200],
['England', 'Female', 'Single', 1600],
['England', 'Female', 'Married', 2000],
['England', 'Male', 'Single', 4800],
['England', 'Male', 'Married', 6400],
]
const A = ['GENDER', 'MARITAL STATUS', 'COUNTRY']
function aggregate(T, A) {
const [fields, ...data] = T
const columns = A.map(name => fields.findIndex(it => name === it))
const count = fields.length - 1
const result = { total: 0 }
data.forEach(values => {
result.total += values[count]
//Go through the tree path, reduce is used here
//to avoid creating extra tracking variable for current position
columns.reduce((ref, index) => {
const key = values[index]
const next = ref[key] || (ref[key] = { total: 0 })
next.total += values[count]
return next
}, result)
})
return result
}
function pack(data) {
return Object.keys(data).reduce((result, key) => {
if (key !== 'total') {
const name = key + " " + data[key].total
result[name] = pack(data[key])
}
return result
}, {})
}
function format(result) {
return JSON.stringify(pack(result), null, 2)
.replace(/[^A-z0-9\n\s]/g, '')
.replace(/\n?\s*\n/g, '\n')
}
function output(it) {
const result = "TOTAL " + it.total + format(it)
console.log(result)
}
output(aggregate(T, A))Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
225 次 |
| 最近记录: |