Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Memory = require('./memory');
- let memory = new Memory();
- class Array{
- constructor(){
- this.length = 0;
- this.ptr = memory.allocate(this.length);
- }
- push(value){
- this._resize(this.length+1);
- memory.set(this.ptr + this.length, value);
- this.length++;
- }
- _resize(size){
- const oldPtr = this.ptr;
- this.prt = memory.allocate(size);
- if(this.ptr === null){
- throw new Error('Out of Memory');
- }
- memory.copy(this.ptr, oldPtr, this.length);
- memory.free(oldPtr);
- this._capacity = size;
- }
- get(index){
- if(index < 0 || index >= this.length){
- throw new Error('Index Error');
- }
- return memory.get(this.ptr + index);
- }
- pop(){
- if(this.length === 0){
- throw new Error('Index Error');
- }
- const value = memory.get(this.ptr + this.length - 1);
- this.length--;
- return value;
- }
- insert(index, value){
- if(index < 0 || index >= this.length){
- throw new Error('Index error');
- }
- if (this.length >= this._capacity) {
- this._resize((this.length + 1) * Array.SIZE_RATIO);
- }
- memory.copy(this.ptr + index + 1, this.ptr + index, this.length - index);
- memory.set(this.ptr + index, value);
- this.length++;
- }
- remove(index) {
- if (index < 0 || index >= this.length) {
- throw new Error('Index error');
- }
- memory.copy(this.ptr + index, this.ptr + index + 1, this.length - index - 1);
- this.length--;
- }
- } //array end
- let arr = new Array();
- arr.push(3);
- arr.push(2);
- arr.push(4);
- arr.push(5);
- console.log(arr);
- // function filter(arr){
- // let newArray = [];
- // for(let i = 0; i < arr.length; i++){
- // if(arr[i] > 5){
- // newArray.push(arr[i]);
- // }
- // }
- // return newArray;
- // }
- //
- // function maximumSum(arr){
- // let sum = 0;
- // for(let i = 0; i < arr.length; i++){
- // if(arr[i] > 0){
- // sum += arr[i];
- // }
- // }
- // return sum;
- // }
- //
- // function merge(arr1, arr2){
- // let newArr = [];
- // for(let i = 0; i < arr1.length; i++){
- // newArr.push(arr1[i])
- // }
- // for(let j = 0; j < arr2.length; j++){
- // newArr.push(arr2[j])
- // }
- // let answer = newArr.sort((a, b)=>{return a-b});
- // return answer;
- // }
- //
- // let theArray = [1, 2, 6, 7, 8];
- // let theArray2 = [5, 3, 9, 4, 10];
- // // console.log(maximumSum(theArray));
- // console.log(merge(theArray, theArray2))
- //
- // function removeChars(str){
- // for(let i = 0; i < str.length; i++){
- // if(str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u'){
- // str = str.substring(0, i) + str.substring(i + 1);
- // }
- // }
- // return str;
- // }
- //
- // console.log(removeChars('There is a space'));
- //
- //
- // function products(arr){
- // let endArr = [];
- // for(let i = 0; i < arr.length; i++){
- // let newArr = arr.filter(item => item !== arr[i]);
- // console.log(newArr);
- // let product = 1;
- // for(let j = 0; j < newArr.length; j++){
- // product = product * newArr[j];
- // }
- // endArr.push(product);
- // }
- // return endArr;
- // }
- // let theArray = [1, 2, 6, 7, 8];
- // console.log(products(theArray));
- //
- // function twoD(arr){
- // let a = [];
- // let b = [];
- // let newArray = [];
- // for(let i = 0; i < arr.length; i++){
- // for(let j = 0; j < arr[i].length; j++){
- // if(arr[i][j] === 0){
- // a[i] === true;
- // b[j] === true;
- // }
- // }
- // }
- // for(let i = 0; i < arr.length; i++){
- // for(let j = 0; j < arr[i].length; j++){
- // if(a[i] || b[j] === true){
- // arr[i][j] === 0;
- // }
- // }
- // }
- // return arr;
- // }
- // console.log(twoD([[1,0,1,1,0],
- // [0,1,1,1,0],
- // [1,1,1,1,1],
- // [1,0,1,1,1],
- // [1,1,1,1,1]]));
- function isRotation(str1, str2){
- for(let i = 0; i < str1.length; i++){
- let firstHalf = str1.substring(0, i);
- let secondHalf = str1.substring(i);
- let full = secondHalf + firstHalf;
- if(full === str2){
- return true;
- }
- }
- return false;
- }
- console.log(isRotation('amazon', 'onamaz'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement