Advertisement
BbJLeB

Untitled

Apr 24th, 2024
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. function coinCollection(input) {
  2.     const n = parseInt(input[0]);
  3.     let coinList = input.slice(1, n + 1);
  4.     let commands = input.slice(n + 1);
  5.  
  6.     for (let command of commands) {
  7.         if (command.startsWith("Examine")) {
  8.             if (coinList.length > 0) {
  9.                 const examinedCoin = coinList.shift();
  10.                 console.log(`Examined: ${examinedCoin}!`);
  11.             }
  12.         } else if (command.startsWith("Acquire")) {
  13.             const coinName = command.substring(8); // Removes "Acquire " from the beginning
  14.             coinList.push(coinName);
  15.         } else if (command.startsWith("Swap")) {
  16.             const [_, startIndex, endIndex] = command.split(" ").map(Number);
  17.             if (!isNaN(startIndex) && !isNaN(endIndex) && startIndex >= 0 && endIndex >= 0 && startIndex < coinList.length && endIndex < coinList.length) {
  18.                 [coinList[startIndex], coinList[endIndex]] = [coinList[endIndex], coinList[startIndex]];
  19.                 console.log("Swapped!");
  20.             }
  21.         } else if (command === "Conclude") {
  22.             break;
  23.         }
  24.     }
  25.  
  26.     if (coinList.length > 0) {
  27.         console.log("Remaining coins: " + coinList.join(", "));
  28.     } else {
  29.         console.log("The collection is empty");
  30.     }
  31. }
  32.  
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement