Skip to content

Instantly share code, notes, and snippets.

View HaiBV's full-sized avatar
🔥
Journeyman in craftsmanship

Harvey Bui HaiBV

🔥
Journeyman in craftsmanship
View GitHub Profile
@HaiBV
HaiBV / adjacentElementsProduct.js
Last active October 23, 2019 10:41
adjacentElementsProduct.js
function adjacentElementsProduct(inputArray) {
var arrayLength = inputArray.length;
var largestProduct = inputArray[0] * inputArray[1];
for (var i = 1; i < arrayLength - 1; i++) {
largestProduct = Math.max(largestProduct, inputArray[i] * inputArray[i + 1]);
}
return largestProduct;
}
@HaiBV
HaiBV / reverseInParentheses.js
Created October 24, 2019 17:14
reverseInParentheses.js
function reverseInParentheses(s) {
while (true) {
let c = s.indexOf(")");
if (c === -1) {
break;
}
let o = s.substring(0, c).lastIndexOf("(");
@HaiBV
HaiBV / typeOfTriangle.sql
Last active October 26, 2019 09:22
typeOfTriangle.sql
select
case
when a + b <= c or a + c <= b or b + c <=a then 'Not A Triangle'
when a = b and b = c then 'Equilateral'
when a = b or b = c or a = c then 'Isosceles'
else 'Scalene'
end as triangle_type
from TRIANGLES;
@HaiBV
HaiBV / occupations.sql
Created October 28, 2019 04:00
occupations.sql
set @r1=0, @r2=0, @r3=0, @r4=0;
select min(Doctor), min(Professor), min(Singer), min(Actor)
from(
select case when Occupation='Doctor' then (@r1:=@r1+1)
when Occupation='Professor' then (@r2:=@r2+1)
when Occupation='Singer' then (@r3:=@r3+1)
when Occupation='Actor' then (@r4:=@r4+1) end as RowNumber,
case when Occupation='Doctor' then Name end as Doctor,
case when Occupation='Professor' then Name end as Professor,
case when Occupation='Singer' then Name end as Singer,
@HaiBV
HaiBV / removeKFromList.js
Created November 7, 2019 17:27
removeKFromList
// Singly-linked lists are already defined with this interface:
// function ListNode(x) {
// this.value = x;
// this.next = null;
// }
//
function removeKFromList(l, k) {
var curr;
// remove leading k values with changing l
@HaiBV
HaiBV / addTwoHugeNumbers.js
Created November 7, 2019 18:15
addTwoHugeNumbers
// Singly-linked lists are already defined with this interface:
// function ListNode(x) {
// this.value = x;
// this.next = null;
// }
//
function addTwoHugeNumbers(a, b) {
var sum = null,
tmp,
carry = 0,
@HaiBV
HaiBV / messageFromBinaryCode.js
Created November 13, 2019 10:37
messageFromBinaryCode
function messageFromBinaryCode(code) {
return code.match(/.{8}/g).reduce((a,b)=>a+String.fromCharCode(parseInt(b,2)),"")
}
@HaiBV
HaiBV / Benchmarking.php
Last active November 22, 2019 09:40
Benchmarking PHP code block
$n = 1000000;
$time1 = microtime(true);
$mem1 = memory_get_usage(true);
for ($i = 0; $i < $n; $i++)
{
}
@HaiBV
HaiBV / splitToNCharLong.js
Last active December 12, 2019 15:02
split string in to n-char long tokens
function split(input, len) {
return input.match(new RegExp('.{1,' + len + '}(?=(.{' + len + '})+(?!.))|.{1,' + len + '}$', 'g'))
}
console.log(split('11010101101', 4)) // ['110', '1010', '1101']
console.log(split('ab22883b0ada0', 2)) // ['a', 'b2', '28', '83', 'b0', 'ad', 'a0']
@HaiBV
HaiBV / OptionalChaining.js
Last active February 4, 2020 04:48
Optional Chaining
// without Optional Chaining
function sayHi(user) {
let name = (user && user.name && user.name.toUpperCase()) || "Unknown";
console.log(`Hi Mr. ${name}`);
}
sayHi({}); // Hi Mr. Unknown
sayHi(); // Hi Mr. Unknown
// with Optional Chaining
function sayHi(user) {