Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. const shortestWordLength = (sentence) => {
  2. return Math.min(
  3. ...sentence.trim()
  4. .split(' ')
  5. .map(item => item.length)
  6. );
  7. };
  8.  
  9. console.log(shortestWordLength("bitcoin take over the world maybe who knows perhaphs"));
  10.  
  11.  
  12.  
  13. const countAllElements = (arr) => {
  14. let count = arr.length;
  15.  
  16. arr.forEach(item => {
  17. if(Array.isArray(item)) {
  18. count += countAllElements(item);
  19. }
  20. })
  21.  
  22. return count;
  23. };
  24.  
  25. console.log(countAllElements([1, 2, [3, 4, 6, [[], [1]]]]));
  26.  
  27.  
  28.  
  29. const replaceString = (str) => {
  30. const charMap = new Map();
  31. str = str.toLowerCase();
  32.  
  33. for(let letter of str) {
  34. if(charMap.get(letter) !== undefined) {
  35. charMap.set(letter, ')');
  36. continue;
  37. }
  38. charMap.set(letter, '(');
  39. }
  40.  
  41. return str.split('')
  42. .map(letter => charMap.get(letter))
  43. .join('');
  44.  
  45. };
  46.  
  47. console.log(replaceString('rEceDe'));
  48.  
  49.  
  50.  
  51. // 3(a)3(b3(c))
  52. // 2(a2(b2(c)))
  53. // 1(a2(b)3(c4(d2(e)f)))
  54. const solve = (str) => {
  55. const regex = /(\d+)\(([a-z]+)\)/;
  56. const match = str.match(regex);
  57.  
  58. if(!match) {
  59. return str;
  60. }
  61.  
  62. const [fullMatch, multiplier, subject] = match;
  63. const replaceWith = subject.repeat(parseInt(multiplier, 10));
  64.  
  65. return solve(str.replace(fullMatch, replaceWith));
  66.  
  67. };
  68.  
  69. console.log(solve('3(ab)'));
  70.  
  71.  
  72.  
  73. const rangeExtraction = (arr) => {
  74. for(let i = 0; i < arr.length - 2; i++) {
  75. let counter;
  76.  
  77. for(counter = 2; counter < arr.length - i; counter++) {
  78. if(arr[i + counter] - arr[i] !== counter) {
  79. break;
  80. }
  81. }
  82.  
  83. if(counter > 2) {
  84. arr.splice(i, counter, `${arr[i]}-${arr[i + counter - 1]}`);
  85. }
  86. }
  87. return arr.join(',');
  88. };
  89.  
  90. console.log(rangeExtraction([-30, -20, -19, -18, -17, -16, -15, -10, 0, 1, 5, 6, 7, 8, 9, 10, 30]))
  91.  
  92.  
  93. const snail = (matrix) => {
  94. const finalArray = [];
  95.  
  96. // directions change with this order
  97. // starting with {x: 1, y: 0}
  98. const directionValues = [1, 0, -1, 0];
  99.  
  100. const directionIds = {
  101. x: 3,
  102. y: 0,
  103. };
  104.  
  105. const current = {
  106. x: 0,
  107. y: 0,
  108. }
  109.  
  110. const limits = {
  111. top: 0,
  112. left: 0,
  113. bottom: matrix.length - 1,
  114. right: matrix.length - 1,
  115. };
  116.  
  117. const nextStep = () => {
  118. current.x += directionValues[directionIds.x];
  119. current.y += directionValues[directionIds.y];
  120. }
  121.  
  122. const nextDirection = () => {
  123. directionIds.x = (directionIds.x + 1) % 4;
  124. directionIds.y = (directionIds.y + 1) % 4;
  125. }
  126.  
  127. while (limits.top <= limits.bottom && limits.left <= limits.right) {
  128. finalArray.push(matrix[current.x][current.y]);
  129. nextStep();
  130.  
  131. if (current.y > limits.right) {
  132. current.y = limits.right;
  133. current.x = ++limits.top;
  134. nextDirection();
  135. }
  136.  
  137. if (current.x > limits.bottom) {
  138. current.x = limits.bottom;
  139. current.y = --limits.right;
  140. nextDirection();
  141. }
  142.  
  143. if (current.y < limits.left) {
  144. current.y = limits.left;
  145. current.x = --limits.bottom;
  146. nextDirection();
  147. }
  148.  
  149. if (current.x < limits.top) {
  150. current.x = limits.top;
  151. current.y = ++limits.left;
  152. nextDirection();
  153. }
  154. }
  155.  
  156. return finalArray;
  157. };
  158.  
  159. console.log(
  160. snail(
  161. [
  162. [1, 2, 3, 4, 5],
  163. [16, 17, 18, 19, 6],
  164. [15, 24, 25, 20, 7],
  165. [14, 23, 22, 21, 8],
  166. [13, 12 ,11 ,10 ,9],
  167. ]
  168. )
  169. );
  170.  
  171. console.log(
  172. snail(
  173. [
  174. [1, 2, 3, 4],
  175. [12, 13, 14, 5],
  176. [11, 16, 15, 6],
  177. [10, 9, 8, 7],
  178. ]
  179. )
  180. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement