• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const gcd = (...arr) => { const _gcd = (x, y) => (!y ? x : gcd(y, x % y)); return [...arr].reduce((a, b) => _gcd(a, b)); }; gcd(8, 36); // 4 gcd(...[12, 8, 32]); // 4
• Mar 11, 2021 •C S
0 likes • 2 views
import { configureStore } from "@reduxjs/toolkit"; import { combineReducers } from "redux"; import profile from "./profile"; import auth from "./auth"; import alert from "./alert"; const reducer = combineReducers({ profile, auth, alert, }); const store = configureStore({ reducer }); export default store;
• Sep 13, 2023 •C S
0 likes • 11 views
/** * @param {number[]} nums * @return {number[]} */ var productExceptSelf = function(nums) { var output = []; var leftMult = 1; var rightMult = 1; for (var i=nums.length - 1; i >= 0; i--) { output[i] = rightMult; rightMult *= nums[i]; console.log({output: JSON.stringify(output), i}) } for (var j=0; j < nums.length; j++) { output[j] *= leftMult; leftMult *= nums[j]; console.log({output: JSON.stringify(output), j}) } return output; }; console.log(productExceptSelf([1, 2, 3, 4]))
• Feb 5, 2021 •LeifMessinger
class Timer{ start(){ this.startTime = Date.now(); } stop(){ const dt = Date.now() - this.startTime; console.log(dt); return dt; } getExecTime(fun, asynchronous){ return asynchronous ? this.getAsyncExecTime(fun) : this.getSyncExecTime(fun); } getSyncExecTime(fun){ this.start(); fun(); return this.stop(); } async getAsyncExecTime(fun){ this.start(); await fun(); return this.stop(); } static average(fun, numTimes, asynchronous = false){ const times = []; const promises = []; let timer = new Timer(); if(asynchronous){ for(let i = 0; i < numTimes; i++){ timer.getAsyncExecTime(fun); } }else{ for(let i = 0; i < numTimes; i++){ timer.getSyncExecTime(fun); } } return (times => times.reduce(((sum, acc) => sume + acc), 0) / times.length); } static async timeAsync(fun, numTimes){ if(numTimes <= 1) return new Timer().getAsyncExecTime(fun); const promises = []; let timer = new Timer(true); for(let i = 0; i < numTimes; i++){ promises.push(fun().catch((e)=>{})); } try{ await Promise.all(promises).catch((e)=>{}); }catch(e){ //do absolutely nothing } return timer.stop(); } constructor(start = false){ if(start) this.start(); } }
const permutations = arr => { if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr; return arr.reduce( (acc, item, i) => acc.concat( permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [ item, ...val, ]) ), [] ); }; permutations([1, 33, 5]); // [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]
0 likes • 4 views
// Time Complexity : O(N) // Space Complexity : O(1) var isMonotonic = function(nums) { let isMono = null; for(let i = 1; i < nums.length; i++) { if(isMono === null) { if(nums[i - 1] < nums[i]) isMono = 0; else if(nums[i - 1] > nums[i]) isMono = 1; continue; } if(nums[i - 1] < nums[i] && isMono !== 0) { return false; } else if(nums[i - 1] > nums[i] && isMono !== 1) { return false; } } return true; }; let nums1 = [1,2,2,3] let nums2 = [6,5,4,4] let nums3 = [1,3,2] console.log(isMonotonic(nums1)); console.log(isMonotonic(nums2)); console.log(isMonotonic(nums3));