Untitled
unknown
javascript
4 years ago
3.4 kB
6
Indexable
function solution(house_trades, street_trades) {
const sorted_house_trades = house_trades.sort();
const sorted_street_trades = street_trades.sort();
const parsed_house_trades = sorted_house_trades.map(parse);
const house_trades_map = mapify(parsed_house_trades);
const parsed_street_trades = sorted_street_trades.map(parse);
const street_trades_map = mapify(parsed_street_trades);
console.log({house_trades_map: JSON.stringify(house_trades_map), street_trades_map: JSON.stringify(street_trades_map)});
filtered_house_trades = parsed_house_trades.filter(({
string: trade,
symbol,
type,
quantity,
id,
}) => {
const symbolMap = street_trades_map[symbol];
if (symbolMap == null) {
return true;
}
const typeMap = symbolMap[type];
if (typeMap == null) {
return true;
}
const quantityMap = typeMap[quantity];
if (quantityMap == null) {
return true;
}
let mappedId = quantityMap[id] != null && quantityMap[id] > 0
? id
: Object.keys(quantityMap).find(key => quantityMap[key] > 0);
if (mappedId == null) {
return true;
}
quantityMap[mappedId] = quantityMap[mappedId] - 1;
return false;
});
filtered_street_trades = parsed_street_trades.filter(
({
string: trade,
symbol,
type,
quantity,
id,
}) => {
const symbolMap = house_trades_map[symbol];
if (symbolMap == null) {
return true;
}
const typeMap = symbolMap[type];
if (typeMap == null) {
return true;
}
const quantityMap = typeMap[quantity];
if (quantityMap == null) {
return true;
}
let mappedId = quantityMap[id] != null && quantityMap[id] > 0
? id
: Object.keys(quantityMap).find(key => quantityMap[key] > 0);
if (mappedId == null) {
return true;
}
quantityMap[mappedId] = quantityMap[mappedId] - 1;
return false;
});
console.log({house_trades_map: JSON.stringify(house_trades_map), street_trades_map: JSON.stringify(street_trades_map)});
console.log({filtered_house_trades, filtered_street_trades});
const all_unmatched_trades = [...filtered_house_trades.map(t => t.string), ...filtered_street_trades.map(t => t.string)];
return all_unmatched_trades.sort();
}
function parse(trade) {
const [symbol, type, quantity, id] = trade.split(',');
return {
string: trade,
symbol,
type,
quantity,
id,
};
}
function mapify(trades) {
const map = {};
trades.forEach(({
string: trade,
symbol,
type,
quantity,
id,
}) => {
const symbolMap = (map[symbol] = map[symbol] != null ? map[symbol] : {});
const typeMap = (symbolMap[type] = symbolMap[type] != null ? symbolMap[type] : {});
const quantityMap = (typeMap[quantity] = typeMap[quantity] != null ? typeMap[quantity] : {});
quantityMap[id] = quantityMap[id] != null ? quantityMap[id] + 1 : 1;
});
return map;
}Editor is loading...