Advertisement
tetris555

softuni-students

Nov 25th, 2022
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     const courses = new Map();
  3.     // course regex
  4.     const coursePattern = /(?<name>[\w#]+):\s(?<cap>\d+)/;
  5.     const studentPattern = /(?<username>\w+)\[(?<credit>\d+)\] with email (?<email>\w+@\w+\.\w+) joins (?<course>[\w#]+)/;
  6.  
  7.     for (const e of arr) {
  8.         if (coursePattern.test(e)) {
  9.             const {name: course, cap: capacity} = e.match(coursePattern).groups;
  10.             if (!courses.has(course)) {
  11.                 courses.set(course, {students: [], capacity: Number(capacity)});
  12.             } else {
  13.                 courses.get(course).capacity += Number(capacity);
  14.             }
  15.         } else if (studentPattern.test(e)) {
  16.             const {username, email,credit, course} = e.match(studentPattern).groups;
  17.  
  18.             if (courses.has(course)) {
  19.                 if (courses.get(course).capacity > 0) {
  20.                     courses.get(course).students.push({username, email, credit: Number(credit)});
  21.                     courses.get(course).capacity--;
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     [...courses.entries()]
  27.         .sort((a, b) => {
  28.             return b[1].students.length - a[1].students.length;
  29.         })
  30.         .forEach(e => {
  31.             console.log(`${e[0]}: ${courses.get(e[0]).capacity} places left`);
  32.             courses.get(e[0]).students
  33.                 .sort((a, b) => {
  34.                     return b.credit - a.credit;
  35.                 })
  36.                 .forEach(s => console.log(`--- ${s.credit}: ${s.username}, ${s.email}`));
  37.         });
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement