Skip to content

Instantly share code, notes, and snippets.

@likev
likev / longest-common-substring.js
Last active October 22, 2019 09:42
Print the longest common substring in JavaScript
var strA = 'abcdeaa';
var strB = '002bcdekk';
//approach from https://www.geeksforgeeks.org/print-longest-common-substring/
var maxCommonString = (strA, strB)=>{
var trail = [[],[]];
var cur = 0;
var maxLength = 0;
var posA = 0;
@likev
likev / getIndexofMaxOfArray.js
Created October 22, 2019 07:12
get index of max element of array use user defined compare function
var a = [1, 2, 3,4,5]
function getIndexofMaxOfArray(array, compareFunction) {
if(! Array.isArray(array)) return -1;
if(array.length === 0) return -1;
var pos = 0;
for(var i = 1; i<array.length; i++ ){
if(typeof compareFunction === 'function'){
@likev
likev / longestCommonSubsequenceLength.js
Created October 22, 2019 14:02
Given two string sequences, write an algorithm to find the length of longest subsequence present in both of them.
var strA = 'ACBDEA';
var strB = 'ABCDA';
//approach from https://algorithms.tutorialhorizon.com/dynamic-programming-longest-common-subsequence/
var longestCommonSubsequenceLength = (strA, strB)=>{
var LCS = [[],[]];
var cur = 0;
var maxLength = 0;
for(var i=0; i <= strA.length; i++){
@likev
likev / big-multiply.js
Last active November 1, 2019 14:51
big number multiply in JS
var big_multiply = function(a,b){
a += '', b += ''; //int to string
var c = [];
c.length = a.length + b.length;
for(var index= 0; index<c.length; index++){
c[index] = 0;
}
for(var index_b = 1; index_b <= b.length; index_b++){
for(var n=4;n<10000;n++){
var b = n*n + '';
for(var i=1;i<b.length;i++){
var part1 = + b.substring(0,i), part2 = + b.substring(i);
if(part1 + part2 === n){
console.log(`${n}^2 = ${b} ${part1} + ${part2} = ${n}`);
}
}
}
@likev
likev / V8-leak-test.js
Last active February 13, 2020 01:02
A V8 bug that cause Node.js(and also V8-related browsers and APPs) memory leak
let create_leak_class = () => {
function Leak() {
//call an unused prototype method
this.unused();
}
Leak.prototype.unused = () => {
@likev
likev / load-next-pages.js
Last active December 23, 2019 07:30
Load next pages
var loadPage = async (n) => {
var response = await fetch(`https://www.tecmint.com/free-open-source-cloud-storage-tools-for-linux/${n}/`);
var html = await response.text();
var $ = jQuery;
var entry = $('article .entry-inner', html);
entry.find('.social-sharing').remove();
entry.find('img[data-lazy-src]').each(function () {
@likev
likev / Two-lines-output.js
Last active November 27, 2022 06:34
output content in many Two-lines
var content = '社会主义核心价值观的基本内容是:富强、民主、文明、和谐,自由、平等、公正、法治,爱国、敬业、诚信、友善。';
var sizeOfLine = 20;
var count = 0;
var isOdd = true;
var output = '';
var oddLine = [], evenLine = [];
var Harray = [];
var isVertical = false;
@likev
likev / FUN_00401420.asm
Created March 26, 2020 08:01
FUN_00401420
**************************************************************
* FUNCTION *
**************************************************************
undefined __fastcall FUN_00401420(CWnd * param_1)
undefined AL:1 <RETURN>
CWnd * ECX:4 param_1
undefined4 Stack[-0x4]:4 local_4 XREF[2]: 0040146e(W),
004017fc(W)
undefined1 Stack[-0x8]:1 local_8 XREF[8]: 004014c8(W),
004014d6(W),
@likev
likev / server.js
Created March 27, 2020 23:32
Create desktop App using ( Node.js + HTML + CSS ) without Electron
"use strict";
const http = require('http');
const url = require('url');
const { exec } = require('child_process');
let listenPort = 8081;
let startHttpServer = function(){
http.createServer(function(req, res) {