Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let users = [];
- let articles = new Map();
- for (let info of arr) {
- if (info.includes('article')) {
- const article = info.substring(info.indexOf(' ') + 1,);
- articles.set(article, new Map());
- } else if (info.includes('user')) {
- const user = info.substring(info.indexOf(' ') + 1,);
- users.push(user);
- } else {
- const user = info.substring(0, info.indexOf(' '));
- const article = info.substring(info.indexOf('on') + 3, info.indexOf(':'));
- const title = info.substring(info.indexOf(':') + 2, info.indexOf(','));
- const comment = info.substring(info.indexOf(',') + 2,);
- if (doesExistUser(user) && doesExistArticle(article)) {
- const cmToPush = [title, comment];
- if (!doesUserExistInArticle(user, article)) {
- articles.get(article).set(user, []);
- }
- findUserComments(user).push(cmToPush);
- }
- }
- }
- // console.log(articles);
- for (const article of [...articles.entries()].sort((a, b) => compare(a, b))) {
- console.log(`${article[0]}`);
- }
- function doesExistUser(x) {
- return users.includes(x);
- }
- function doesExistArticle(x) {
- return articles.has(x);
- }
- function doesUserExistInArticle(x, y) {
- return articles.get(y).has(x);
- }
- function findUserComments (x) {
- for (let article of articles.entries()) {
- if (article[1].has(x)) {
- return article[1].get(x);
- }
- }
- }
- function compare(a,b) {
- // let art1 = [...a.entries()];
- // let art2 = [...b.entries()];
- // return [...art1[1].values()].reduce((a,b) => a + b.length,0) - [...art2[1].values()].reduce((a,b) => a +b.length,0);
- return [...a[1].values()].reduce((a,b) => a + b.length, 0) - [...b[1].values()].reduce((a,b) => a + b.length, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment