Skip to content

Instantly share code, notes, and snippets.

@mariohmol
Last active November 23, 2018 00:20
Show Gist options
  • Save mariohmol/ee52c315fb81d89d7b5bfa56682f5bd9 to your computer and use it in GitHub Desktop.
Save mariohmol/ee52c315fb81d89d7b5bfa56682f5bd9 to your computer and use it in GitHub Desktop.
Data Structures in JS
/*
Array with 10 spaces:
H is the HEAD
*/
[ H, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0]
[ 1, H, 0, 0, 0, 0, 0, 0, 0, 0 , 0] //push 1 - ptr=0, length=1, head=1
[ 1, 1, 2, H, 0, 0, 0, 0, 0, 0 , 0] //push 2 - ptr=1, length=2, head=3
[ 1, 1, 2, 1, 2, 3, H, 0, 0, 0 , 0] //push 3 - ptr=3, length=3, , head=6
[ 1, 1, 2, 1, 2, 3, 1, 2, 3, 4 , H] //push 4 - ptr=3, length=3, , head=
[ 1, 1, 2, 1, 2, 3, 1, 3, 4, 4(H), 0] //remove index of number 2, so move left array to the right and legth--
/*
ptr is the same =3 , and length will be 2
/Number 3 will be in head as a trash value, can ignore
Ex. with ratio x 3
*/
[ H, 0, 0, 0, 0, 0, 0, 0, 0, 0] //empty memory
[ 1, 0, 0, H, 0, 0, 0, 0, 0, 0] //ad
[ 1, 2, 0, H, 0, 0, 0, 0, 0, 0] //
[ 1, 2, 3, 1, 2, 3, 0, 0, 0, H] //Copy the array 1 time
[ 1, 2, 3, 1, 2, 3, 4, 0, 0, H] //
[ 1, 2, 3, 1, 2, 3, 4, 5, 0, H] //
[ 1, 2, 3, 1, 2, 3, 4, 5, 6, H] //Copy the array 2 time
// If you try to add 7 to this array you will receive out of memory exception
(Math.pow(2,5)).toString(2) // 100000
(Math.pow(2,5)-1).toString(2) // 011111
//2^n = odd , 2^n - 1=even
function setBit3(x){ return x|4 }
function toggleBit3(x){ return x^4 }
/**
SUM:
Carry: 11
110
011
-----
1001
110
011
---
010
*/
function clearBit3(x){ return x&(~4) }
function isSetBit3(x){ return x&4==4 }
//https://en.wikipedia.org/wiki/Two's_complement
x=5
x.toString(2) // "101"
y=~5 //-6
(y>>>0).toString(2) // "11111111111111111111111111111010"
//101 to number
parseInt("101",2)
//two complemnts
x="11111111111111111111111111111010"
//convert to decimal, invert bits and include the - signal
-~parseInt(x,2) // == -5
var head = {
value: 4,
next: { value: 5,
next: {
value: 6,
next: null
}
}
};
var last = {
value: 6,
next: null
};
var middle = { value: 5,
next: last
};
var head = {
value: 4,
next: middle
};
//couting the size
var node = head;
function middleItem(head){
if(!head) return;
var count=0;
while(node.next){
node=node.next;
count++
}
//getting the middle item in list
node = head;
for(i=0;i<count/2;i++){
node=node.next;
}
console.log("Middle man ",node);
}
//Using recursive
function counter(head){
if(!head) return 0;
return counter(head.next)+1;
}
let reverse = function(head){
let node = tail = head;
let prev;
//4 -> 5 - >6
while(node){ //4
let next = node.next; //next=5
node.next = prev;
prev = node;
node = next;
}
console.log(prev);
return prev;
}
var head = { value: 4, next: { value: 5, next: {value: 6, next: null}}};
head.next.next.next = head;
let iscicle = function(head){
let node = head;
let i=0;
while(node){
let check=head;
for(k=0;k<i;k++){
if(check==node) return true;
check=check.next;
}
i++;
node = node.next;
}
return false;
}
'use strict';
const chai = require('chai');
const chaiHTTP = require('chai-http');
chai.use(chaiHTTP);
const promise1 = chai.request('https://api.github.com')
.get('/search/repositories?q=bookmark');
const promise2 = chai.request('https://api.github.com')
.get('/search/repositories?q=bookmark');
const promise3 = chai.request('https://api.github.com')
.get('/search/repositories?q=bookmark');
const promise4 = chai.request('https://api.github.com')
.get('/search/repositories?q=bookmark');
const promise5 = chai.request('https://api.github.com')
.get('/search/repositories?q=bookmark');
const promise6 = chai.request('https://api.github.com')
.get('/search/repositories?q=bookmark');
const all = Promise.all([promise4, promise5, promise6])
.then(responses => {
console.log(responses.length);
});
const chain = promise1
.then((response) => {
console.log(1);
return promise2;
})
.then((response) => {
console.log(2);
return promise3;
})
.then((response) => {
console.log(3);
});
//https://en.wikipedia.org/wiki/List_of_Dewey_Decimal_classes
/**
Contents [hide]
1 Class 000 – Computer science, information & general works
2 Class 100 – Philosophy & psychology
3 Class 200 – Religion
4 Class 300 – Social sciences
5 Class 400 – Language
6 Class 500 – Science
7 Class 600 – Technology
8 Class 700 – Arts & recreation
9 Class 800 – Literature
10 Class 900 – History & geography
11 */
// 0000001 = 000.001 example with 3 digis after class
var librarybook = [ 000001, 1000001, 2000001, 3000001,3000002,3000003, 400001,500001,600001,700001,800001,900001,900002];
var books = [ {id: 000001, value: {name: "book 1"} } , {id: 100001, value: {name: "book 2"} } ];
const library = [
{code: '005.133', title: 'Mike Cowlishaw: The REXX Language'},
{code: '005.133', title: 'Sams: Teach Yourself C++ In 21 Days'},
{code: '005.133', title: 'Bjarne Stroustrup: The C++ Programming Language'},
{code: '005.2762', title: 'Douglas Crockford: JavaScript: The Good Parts'},
{code: '005.2762', title: 'David Flanagan: JavaScript: The Definitive Guide'},
{code: '005.44684', title: 'Meinhard Schmidt: Windows Vista for Dummies'},
{code: '220.52081', title: 'Zondervan: NIV Study Bible'},
{code: '231.7652', title: 'Dr Russell Humphries: Starlight and Time'},
{code: '623.82509051', title: 'Frederick Thomas Jane: Jane\'s Fighting Ships'},
{code: '796.8092', title: 'Chuck Norris: The Official Chuck Norris Fact Book'},
];
const searchBook = (library, dewey, title) => {
let start = 0;
let end = library.length;
while('') {
if(start > end) {
return false;
}
const index = Math.floor((start + end) / 2);
const ptr = library[index];
if(ptr.code > dewey) {
end = index - 1;
continue;
}
if(ptr.code < dewey) {
start = index + 1;
continue;
}
if(ptr.title === title) {
return ptr;
}
for(let i = index + 1; i < end && library[i].code === dewey; i++) {
if(library[i].title === title) {
return library[i];
}
}
for(let i = index - 1; i >= start && library[i].code === dewey; i--) {
if(library[i].title === title) {
return library[i];
}
}
return false;
}
};
// 1 min to 10 max
//Loop over the array and make currentNode - minValue = newPosition , example node 1 - 1 =0 , 3 -1 = 2 position
var original = [ 3, 4, 8, 7, 2, 1, 5, 10 , 9, 6 ] ;
// [ , , 3 ,4 , , , , , , 10 ]
//swtich nodes in the same array
var i=2;
var j=3;
var temp=original[i];
original[i]=original[j];
original[j]=temp;
//recipes
// 1, "cake"
// 2, "bread"
//ingredientes
// 1, "milk"
// 2, "flour"
/*
select * from recipes JOIN ingredientes on recipes.id = ingredientes.id
select * from recipes, ingredientes where recipes.id = ingredientes.id
//JOIN
1, "cake" 1, "milk"
1, "cake" 2, "flour"
2, "bread" 1, "milk"
2, "bread" 2, "flour"
//JOIN with ON
1, "cake" 1, "milk"
2, "bread" 2, "flour"
//Country, State(country_id), City (state_id), User (city_id)
select * from user , city, state, country
where user.city = city.id and city.state = state.id and state.country = country.id
*/
var root={value: 8};
root.left={value: 3};
root.right={value: 10};
root.left.left={value: 1};
root.left.right={value: 6};
root.right.right={value: 14};
var total=0;
var elements=[];
let sortTree= function(node,depth){
if(!node) return depth;
depth++;
var right= sortTree(node.right,depth);
//elements.push(node);
var left = sortTree(node.left,depth); //return null
if(right>left) return right;
else return left;
}
sortTree(root,0);
console.log(elements[-3])
/*
Normal Binary Tree - BFS
1
2 3
4 5 6 7
Binary Search Tree - DFS - recursive to the left
4
3 6
1 2 5 7
*/
arr = [128, 97, 121, 123, 98, 97, 105]
var root={value: 128};
root.left={value: 97};
root.left.right={value: 121};
root.left.right.left={value: 98};
root.left.right.right={value: 123};
root.left.right.left.left={value: 97};
root.left.right.left.right={value: 105};
/*
128
97
121
98 123
97 105
123 - 97=26
105 - 98=7
123 - 121=2
*/
function getStockes(node){
if(node.right) {
maxValue= getStockes(node.right);
return maxValue;
}else if(node.left) {
maxValue= getStockes(node.left);
return maxValue;
}
else return node.value;
return node.value-maxValue;
}
getStockes(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment