Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. const Memory = require('./memory');
  2. let memory = new Memory();
  3. class Array{
  4. constructor(){
  5. this.length = 0;
  6. this.ptr = memory.allocate(this.length);
  7. }
  8. push(value){
  9. this._resize(this.length+1);
  10. memory.set(this.ptr + this.length, value);
  11. this.length++;
  12. }
  13. _resize(size){
  14. const oldPtr = this.ptr;
  15. this.prt = memory.allocate(size);
  16. if(this.ptr === null){
  17. throw new Error('Out of Memory');
  18. }
  19. memory.copy(this.ptr, oldPtr, this.length);
  20. memory.free(oldPtr);
  21. this._capacity = size;
  22. }
  23. get(index){
  24. if(index < 0 || index >= this.length){
  25. throw new Error('Index Error');
  26. }
  27. return memory.get(this.ptr + index);
  28. }
  29. pop(){
  30. if(this.length === 0){
  31. throw new Error('Index Error');
  32. }
  33. const value = memory.get(this.ptr + this.length - 1);
  34. this.length--;
  35. return value;
  36. }
  37. insert(index, value){
  38. if(index < 0 || index >= this.length){
  39. throw new Error('Index error');
  40. }
  41. if (this.length >= this._capacity) {
  42. this._resize((this.length + 1) * Array.SIZE_RATIO);
  43. }
  44. memory.copy(this.ptr + index + 1, this.ptr + index, this.length - index);
  45. memory.set(this.ptr + index, value);
  46. this.length++;
  47. }
  48. remove(index) {
  49. if (index < 0 || index >= this.length) {
  50. throw new Error('Index error');
  51. }
  52. memory.copy(this.ptr + index, this.ptr + index + 1, this.length - index - 1);
  53. this.length--;
  54. }
  55. } //array end
  56.  
  57. let arr = new Array();
  58. arr.push(3);
  59. arr.push(2);
  60. arr.push(4);
  61. arr.push(5);
  62. console.log(arr);
  63.  
  64. // function filter(arr){
  65. // let newArray = [];
  66. // for(let i = 0; i < arr.length; i++){
  67. // if(arr[i] > 5){
  68. // newArray.push(arr[i]);
  69. // }
  70. // }
  71. // return newArray;
  72. // }
  73. //
  74. // function maximumSum(arr){
  75. // let sum = 0;
  76. // for(let i = 0; i < arr.length; i++){
  77. // if(arr[i] > 0){
  78. // sum += arr[i];
  79. // }
  80. // }
  81. // return sum;
  82. // }
  83. //
  84. // function merge(arr1, arr2){
  85. // let newArr = [];
  86. // for(let i = 0; i < arr1.length; i++){
  87. // newArr.push(arr1[i])
  88. // }
  89. // for(let j = 0; j < arr2.length; j++){
  90. // newArr.push(arr2[j])
  91. // }
  92. // let answer = newArr.sort((a, b)=>{return a-b});
  93. // return answer;
  94. // }
  95. //
  96. // let theArray = [1, 2, 6, 7, 8];
  97. // let theArray2 = [5, 3, 9, 4, 10];
  98. // // console.log(maximumSum(theArray));
  99. // console.log(merge(theArray, theArray2))
  100. //
  101. // function removeChars(str){
  102. // for(let i = 0; i < str.length; i++){
  103. // if(str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u'){
  104. // str = str.substring(0, i) + str.substring(i + 1);
  105. // }
  106. // }
  107. // return str;
  108. // }
  109. //
  110. // console.log(removeChars('There is a space'));
  111. //
  112. //
  113. // function products(arr){
  114. // let endArr = [];
  115. // for(let i = 0; i < arr.length; i++){
  116. // let newArr = arr.filter(item => item !== arr[i]);
  117. // console.log(newArr);
  118. // let product = 1;
  119. // for(let j = 0; j < newArr.length; j++){
  120. // product = product * newArr[j];
  121. // }
  122. // endArr.push(product);
  123. // }
  124. // return endArr;
  125. // }
  126. // let theArray = [1, 2, 6, 7, 8];
  127. // console.log(products(theArray));
  128. //
  129. // function twoD(arr){
  130. // let a = [];
  131. // let b = [];
  132. // let newArray = [];
  133. // for(let i = 0; i < arr.length; i++){
  134. // for(let j = 0; j < arr[i].length; j++){
  135. // if(arr[i][j] === 0){
  136. // a[i] === true;
  137. // b[j] === true;
  138. // }
  139. // }
  140. // }
  141. // for(let i = 0; i < arr.length; i++){
  142. // for(let j = 0; j < arr[i].length; j++){
  143. // if(a[i] || b[j] === true){
  144. // arr[i][j] === 0;
  145. // }
  146. // }
  147. // }
  148. // return arr;
  149. // }
  150. // console.log(twoD([[1,0,1,1,0],
  151. // [0,1,1,1,0],
  152. // [1,1,1,1,1],
  153. // [1,0,1,1,1],
  154. // [1,1,1,1,1]]));
  155.  
  156. function isRotation(str1, str2){
  157. for(let i = 0; i < str1.length; i++){
  158. let firstHalf = str1.substring(0, i);
  159. let secondHalf = str1.substring(i);
  160. let full = secondHalf + firstHalf;
  161. if(full === str2){
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167.  
  168.  
  169. console.log(isRotation('amazon', 'onamaz'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement