Advertisement
GeorgiLukanov87

03. Plant Discovery - 02. Programming Fundamentals Final Exam

Apr 6th, 2023
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 03. Plant Discovery
  2. // 02. Programming Fundamentals Final Exam
  3. // https://judge.softuni.org/Contests/Practice/Index/2518#2
  4.  
  5. function solve(input) {
  6.     input.pop();
  7.     let plants = {};
  8.     let nPlants = input.shift();
  9.     for (let i = 0; i < nPlants; i++) {
  10.         let [name, rarityAsStr] = input.shift().split('<->');
  11.         rarity = Number(rarityAsStr);
  12.         plants[name] = [rarity, 0];
  13.     }
  14.  
  15.     for (let i = 0; i < input.length; i++) {
  16.         let command = input[i].split(': ');
  17.         let plantName = command[1].split(' - ')[0];
  18.         if (!Object.keys(plants).includes(plantName)) {
  19.             console.log('error');
  20.             continue;
  21.         }
  22.  
  23.         if (command[0] === 'Rate') {
  24.             let [plantName, rating] = command[1].split(' - ');
  25.             if (plants[plantName][1] != 0) {
  26.                 plants[plantName][1] += Number(rating);
  27.                 plants[plantName][1] /= 2;
  28.             } else {
  29.                 plants[plantName][1] += Number(rating);
  30.             }
  31.  
  32.         } else if (command[0] === 'Update') {
  33.             let [plantName, newRarity] = command[1].split(' - ');
  34.             plants[plantName][0] = Number(newRarity);
  35.  
  36.         } else {
  37.             let plantName = command[1];
  38.             plants[plantName][1] = 0;
  39.         }
  40.  
  41.     }
  42.     console.log('Plants for the exhibition:')
  43.     Object.entries(plants).forEach(p => {
  44.         let name = p[0];
  45.         let rarity = p[1][0];
  46.         let rating = p[1][1];
  47.         console.log(`- ${name}; Rarity: ${rarity}; Rating: ${rating.toFixed(2)}`)
  48.     })
  49.  
  50. }
  51.  
  52. solve((["3",
  53.     "Arnoldii<->4",
  54.     "Woodii<->7",
  55.     "Welwitschia<->2",
  56.     "Rate: Wo8odii - 10",
  57.     "Rate: Welwitschia - 7",
  58.     "Rate: Arnoldii - 3",
  59.     "Rate: Woodii - 5",
  60.     "Update: Woodii - 5",
  61.     "Reset: Arnoldii",
  62.     "Exhibition"]))
  63.  
  64.  
  65.  
  66. // solve((["2",
  67. //     "Candelabra<->10",
  68. //     "Oahu<->10",
  69. //     "Rate: Oahu - 7",
  70. //     "Rate: Candelabra - 6",
  71. //     "Exhibition"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement