Liliana797979

3_2_message - final exam

Aug 17th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let capacity = Number(input.shift())
  3.     let line = input.shift()
  4.     let users = {}
  5.  
  6.     while (line != "Statistics") {
  7.         let [command, x, a, b] = line.split('=')
  8.         if (command == 'Add') {
  9.             if(!users.hasOwnProperty(x)) {
  10.                 users[x] = {sent: Number(a), recieved: Number(b)}
  11.             }
  12.         }else if (command == 'Message') {
  13.             if(users.hasOwnProperty(x) && users.hasOwnProperty(a)){
  14.                 users[x].sent += 1
  15.                 users[a].recieved += 1
  16.  
  17.             if(users[x].sent + users[x].recieved >= capacity) {
  18.                 delete users[x]
  19.                 console.log(`${x} reached the capacity!`)
  20.             }
  21.             if(users[a].sent + users[a].recieved >= capacity) {
  22.                 delete users[a]
  23.                 console.log(`${a} reached the capacity!`)
  24.             }
  25.         }
  26.         }else if (command == 'Empty'){
  27.             if(users.hasOwnProperty(x)) {
  28.                 delete users[x]
  29.             }
  30.             if (x == "All"){
  31.                 users = {}
  32.             }
  33.         }
  34.         line = input.shift()
  35.     }
  36.     let usersCount = Object.keys(users).length
  37.     console.log(`Users count: ${usersCount}`)
  38.  
  39.     let sorted = Object.entries(users).sort((a,b) => Number(b[1].recieved) - Number(a[1].recieved) || a[0].localeCompare(b[0]))
  40.     for(let user of sorted){
  41.         console.log(`${user[0]} - ${Number(user[1].sent) + Number(user[1].recieved)}`)
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment