simeonshopov

Comments

Apr 15th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   let users = [];
  3.   let articles = new Map();
  4.  
  5.   for (let info of arr) {
  6.     if (info.includes('article')) {
  7.       const article = info.substring(info.indexOf(' ') + 1,);
  8.       articles.set(article, new Map());
  9.     } else if (info.includes('user')) {
  10.       const user = info.substring(info.indexOf(' ') + 1,);
  11.       users.push(user);
  12.     } else {
  13.       const user = info.substring(0, info.indexOf(' '));
  14.       const article = info.substring(info.indexOf('on') + 3, info.indexOf(':'));
  15.       const title = info.substring(info.indexOf(':') + 2, info.indexOf(','));
  16.       const comment = info.substring(info.indexOf(',') + 2,);
  17.       if (doesExistUser(user) && doesExistArticle(article)) {
  18.         const cmToPush = [title, comment];
  19.         if (!doesUserExistInArticle(user, article)) {
  20.           articles.get(article).set(user, []);
  21.         }
  22.         findUserComments(user).push(cmToPush);
  23.       }
  24.     }
  25.   }
  26.  
  27.   // console.log(articles);
  28.  
  29.   for (const article of [...articles.entries()].sort((a, b) => compare(a, b))) {
  30.     console.log(`${article[0]}`);
  31.   }
  32.  
  33.  
  34.  
  35.   function doesExistUser(x) {
  36.     return users.includes(x);
  37.   }
  38.  
  39.   function doesExistArticle(x) {
  40.     return articles.has(x);
  41.   }
  42.  
  43.   function doesUserExistInArticle(x, y) {
  44.     return articles.get(y).has(x);
  45.   }
  46.  
  47.   function findUserComments (x) {
  48.     for (let article of articles.entries()) {
  49.       if (article[1].has(x)) {
  50.         return article[1].get(x);
  51.       }
  52.     }
  53.   }
  54.  
  55.   function compare(a,b) {
  56.     // let art1 = [...a.entries()];
  57.     // let art2 = [...b.entries()];
  58.     // return [...art1[1].values()].reduce((a,b) => a + b.length,0) - [...art2[1].values()].reduce((a,b) => a +b.length,0);
  59.     return [...a[1].values()].reduce((a,b) => a + b.length, 0) - [...b[1].values()].reduce((a,b) => a + b.length, 0);
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment