Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 03. Plant Discovery
- // 02. Programming Fundamentals Final Exam
- // https://judge.softuni.org/Contests/Practice/Index/2518#2
- function solve(input) {
- input.pop();
- let plants = {};
- let nPlants = input.shift();
- for (let i = 0; i < nPlants; i++) {
- let [name, rarityAsStr] = input.shift().split('<->');
- rarity = Number(rarityAsStr);
- plants[name] = [rarity, 0];
- }
- for (let i = 0; i < input.length; i++) {
- let command = input[i].split(': ');
- let plantName = command[1].split(' - ')[0];
- if (!Object.keys(plants).includes(plantName)) {
- console.log('error');
- continue;
- }
- if (command[0] === 'Rate') {
- let [plantName, rating] = command[1].split(' - ');
- if (plants[plantName][1] != 0) {
- plants[plantName][1] += Number(rating);
- plants[plantName][1] /= 2;
- } else {
- plants[plantName][1] += Number(rating);
- }
- } else if (command[0] === 'Update') {
- let [plantName, newRarity] = command[1].split(' - ');
- plants[plantName][0] = Number(newRarity);
- } else {
- let plantName = command[1];
- plants[plantName][1] = 0;
- }
- }
- console.log('Plants for the exhibition:')
- Object.entries(plants).forEach(p => {
- let name = p[0];
- let rarity = p[1][0];
- let rating = p[1][1];
- console.log(`- ${name}; Rarity: ${rarity}; Rating: ${rating.toFixed(2)}`)
- })
- }
- solve((["3",
- "Arnoldii<->4",
- "Woodii<->7",
- "Welwitschia<->2",
- "Rate: Wo8odii - 10",
- "Rate: Welwitschia - 7",
- "Rate: Arnoldii - 3",
- "Rate: Woodii - 5",
- "Update: Woodii - 5",
- "Reset: Arnoldii",
- "Exhibition"]))
- // solve((["2",
- // "Candelabra<->10",
- // "Oahu<->10",
- // "Rate: Oahu - 7",
- // "Rate: Candelabra - 6",
- // "Exhibition"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement