Advertisement
isebs

Untitled

Feb 26th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. function solve(input) {
  2.  
  3. let allPaintingNumbers = input.shift()
  4. .split(' ')
  5. //.map(Number);
  6.  
  7. let instruction = '';
  8.  
  9. while (true) {
  10. instruction = input.shift();
  11.  
  12. if (instruction === 'END') {
  13. break;
  14. }
  15.  
  16. instruction = instruction.split(' '); // an array
  17. let command = instruction[0];
  18.  
  19. if (command === 'Change') {
  20. allPaintingNumbers = change(instruction[1], instruction[2]);
  21.  
  22. } else if (command === 'Hide') {
  23. allPaintingNumbers = hide(instruction[1]);
  24.  
  25. } else if (command === 'Switch') {
  26. allPaintingNumbers = swap(instruction[1], instruction[2]);
  27.  
  28. } else if (command === 'Insert') {
  29. let place = Number(instruction[1]);
  30. let newPainting = instruction[2];
  31. place++;
  32. if (place < allPaintingNumbers.length && place > -1) {
  33. allPaintingNumbers.splice(place, 0, newPainting);
  34. }
  35.  
  36. } else if (command === 'Reverse') { //reverse
  37. allPaintingNumbers = allPaintingNumbers.reverse();
  38. }
  39.  
  40. }
  41.  
  42. function change(oldNumber, newNumber) {
  43. for (let i = 0; i < allPaintingNumbers.length; i++) {
  44. if (allPaintingNumbers[i] === oldNumber) {
  45. allPaintingNumbers[i] = newNumber;
  46. break;
  47. }
  48. }
  49. return allPaintingNumbers;
  50. }
  51.  
  52. function hide(paintingNumber) {
  53. for (let i = 0; i < allPaintingNumbers.length; i++) {
  54. if (allPaintingNumbers[i] === paintingNumber) {
  55. allPaintingNumbers.splice(i, 1);
  56. break;
  57. }
  58. }
  59. return allPaintingNumbers;
  60. }
  61.  
  62. function swap(paintingNumberFirst, paintingNumberSecond) {
  63. let temp;
  64. let isFirstExist = false;
  65. let isSecondExist = false;
  66. let indexFirst;
  67. let indexSecond;
  68.  
  69. for (let i = 0; i < allPaintingNumbers.length; i++) {
  70. if (allPaintingNumbers[i] === paintingNumberFirst) {
  71. isFirstExist = true;
  72. indexFirst = i;
  73. }
  74. if (allPaintingNumbers[i] === paintingNumberSecond) {
  75. isSecondExist = true;
  76. indexSecond = i;
  77. }
  78. }
  79. if (isFirstExist && isSecondExist) {
  80. temp = allPaintingNumbers[indexFirst];
  81. allPaintingNumbers[indexFirst] = allPaintingNumbers[indexSecond];
  82. allPaintingNumbers[indexSecond] = temp;
  83. }
  84. return allPaintingNumbers;
  85. }
  86.  
  87. let result = '';
  88. for (let item of allPaintingNumbers) {
  89. result += item + ' ';
  90. }
  91. console.log(result);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement