From 0837be3772833d4d06803ae5326e15d8b2441497 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Tue, 24 Aug 2021 14:30:24 +0300 Subject: [PATCH 01/11] Fibonacci Calculator Memoized --- index.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 4ea6312..ce82368 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,12 @@ -function moveZeros(arr = '') { - function twistIt(str = '') { - return str.split('').map((a, i, arr) => arr[arr.length - i - 1]).join('') - } - return arr.split(' ').map(item => twistIt(item)).join(' ') +function feb(num, memo = {}) { + if (num <= 2) return 1; + if (num in memo) return memo[num]; + memo[num] = feb(num - 1, memo) + feb(num - 2, memo); + return memo[num]; } -console.log(moveZeros("double spaces"), "double spaces", '==>', "elbuod secaps") -console.log(moveZeros("double spaces"), "double spaces", '==>', "elbuod secaps") -console.log(moveZeros("double spaces"), "double spaces", '==>', "elbuod secaps") - -function manhattanDistance(arr0, arr1) { - function fn(params) { - - return Math.abs(params) - } - return (fn(arr0[0] - arr1[0]) + fn(arr0[1] - arr1[1])) -} - -console.log(manhattanDistance([1, 1], [1, 1]), '=> returns 0') -console.log(manhattanDistance([5, 4], [3, 2]), '=> returns 4') -console.log(manhattanDistance([1, 1], [0, 3]), '=> returns 3') +console.log(feb(1)); +console.log(feb(2)); +console.log(feb(3)); +console.log(feb(9)); +console.log(feb(39)); From 3cca900051bd16c756d6aa6361b205080dcc8f80 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Tue, 24 Aug 2021 14:55:04 +0300 Subject: [PATCH 02/11] Grid Traveler Memod --- index.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index ce82368..2333527 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,17 @@ -function feb(num, memo = {}) { - if (num <= 2) return 1; - if (num in memo) return memo[num]; - memo[num] = feb(num - 1, memo) + feb(num - 2, memo); - return memo[num]; +function gridTraveler(row, column, memo = {}) { + if (row === 1 && column === 1) return 1; + if (row === 0 || column === 0) return 0; + const key = row + "," + column; + if (key in memo) return memo[key]; + memo[key] = + gridTraveler(row - 1, column, memo) + gridTraveler(row, column - 1, memo); + return memo[key]; } -console.log(feb(1)); -console.log(feb(2)); -console.log(feb(3)); -console.log(feb(9)); -console.log(feb(39)); +console.log(gridTraveler(1, 0)); +console.log(gridTraveler(2, 1)); +console.log(gridTraveler(3, 2)); +console.log(gridTraveler(3, 3)); +console.log(gridTraveler(9, 3)); +console.log(gridTraveler(18, 18)); +console.log(gridTraveler(39, 14)); From 2d7c0f17b654c1605e755c0b4355adf86c483b7b Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Tue, 24 Aug 2021 15:57:09 +0300 Subject: [PATCH 03/11] Can Sum --- index.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 2333527..1896877 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,24 @@ -function gridTraveler(row, column, memo = {}) { - if (row === 1 && column === 1) return 1; - if (row === 0 || column === 0) return 0; - const key = row + "," + column; - if (key in memo) return memo[key]; - memo[key] = - gridTraveler(row - 1, column, memo) + gridTraveler(row, column - 1, memo); - return memo[key]; +function canSum(totalSum, numArr, memo = {}) { + if (totalSum in memo) return memo[totalSum]; + if (totalSum === 0) return true; + if (totalSum < 0) return false; + + for (let num of numArr) { + const remainder = totalSum - num; + if (canSum(remainder, numArr, memo)) { + memo[totalSum] = true; + return true; + } + } + + memo[totalSum] = false; + return false; } -console.log(gridTraveler(1, 0)); -console.log(gridTraveler(2, 1)); -console.log(gridTraveler(3, 2)); -console.log(gridTraveler(3, 3)); -console.log(gridTraveler(9, 3)); -console.log(gridTraveler(18, 18)); -console.log(gridTraveler(39, 14)); +console.log(canSum(1, [9, 2])); +console.log(canSum(2, [1, 4, 5, 9, 2])); +console.log(canSum(3, [2, 4, 5, 9, 2])); +console.log(canSum(3, [1, 4, 5, 9, 2])); +console.log(canSum(9, [3, 4, 5, 2])); +console.log(canSum(18, [4, 5, 9, 2])); +console.log(canSum(300, [7, 14])); From fa839a167202c058108aacb01b1837fedbe3a0d2 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Tue, 24 Aug 2021 19:40:12 +0300 Subject: [PATCH 04/11] How Some --- index.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 1896877..fb7249d 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,25 @@ -function canSum(totalSum, numArr, memo = {}) { +function howSum(totalSum, numArr, memo = {}) { if (totalSum in memo) return memo[totalSum]; - if (totalSum === 0) return true; - if (totalSum < 0) return false; + if (totalSum === 0) return []; + if (totalSum < 0) return null; for (let num of numArr) { const remainder = totalSum - num; - if (canSum(remainder, numArr, memo)) { - memo[totalSum] = true; - return true; + const returnedValue = howSum(remainder, numArr, memo); + if (returnedValue) { + memo[totalSum] = [...returnedValue, num]; + return memo[totalSum]; } } - memo[totalSum] = false; - return false; + memo[totalSum] = null; + return null; } -console.log(canSum(1, [9, 2])); -console.log(canSum(2, [1, 4, 5, 9, 2])); -console.log(canSum(3, [2, 4, 5, 9, 2])); -console.log(canSum(3, [1, 4, 5, 9, 2])); -console.log(canSum(9, [3, 4, 5, 2])); -console.log(canSum(18, [4, 5, 9, 2])); -console.log(canSum(300, [7, 14])); +console.log(howSum(1, [9, 2])); +console.log(howSum(2, [1, 4, 5, 9, 2])); +console.log(howSum(3, [2, 4, 5, 9, 2])); +console.log(howSum(3, [1, 4, 5, 9, 2])); +console.log(howSum(9, [3, 4, 5, 2])); +console.log(howSum(18, [4, 5, 9, 2])); +console.log(howSum(300, [7, 14])); From a66edd64c17fdfa0f82fa488edb173eb78ba16bc Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Tue, 24 Aug 2021 19:54:30 +0300 Subject: [PATCH 05/11] best some --- index.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index fb7249d..9a8596f 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,27 @@ -function howSum(totalSum, numArr, memo = {}) { - if (totalSum in memo) return memo[totalSum]; - if (totalSum === 0) return []; - if (totalSum < 0) return null; +function bestSum(targetSum, numArr, memo = {}) { + if (targetSum in memo) return memo[targetSum]; + if (targetSum === 0) return []; + if (targetSum < 0) return null; + let shortest = null; for (let num of numArr) { - const remainder = totalSum - num; - const returnedValue = howSum(remainder, numArr, memo); + const remainder = targetSum - num; + const returnedValue = bestSum(remainder, numArr, memo); if (returnedValue) { - memo[totalSum] = [...returnedValue, num]; - return memo[totalSum]; + memo[targetSum] = [...returnedValue, num]; + if (shortest === null || memo[targetSum].length < shortest.length) { + shortest = memo[targetSum]; + } } } - - memo[totalSum] = null; - return null; + memo[targetSum] = shortest; + return shortest; } -console.log(howSum(1, [9, 2])); -console.log(howSum(2, [1, 4, 5, 9, 2])); -console.log(howSum(3, [2, 4, 5, 9, 2])); -console.log(howSum(3, [1, 4, 5, 9, 2])); -console.log(howSum(9, [3, 4, 5, 2])); -console.log(howSum(18, [4, 5, 9, 2])); -console.log(howSum(300, [7, 14])); +console.log(bestSum(1, [9, 2])); +console.log(bestSum(2, [1, 4, 5, 9, 2])); +console.log(bestSum(3, [2, 4, 5, 9, 1])); +console.log(bestSum(3, [1, 4, 5, 9, 2])); +console.log(bestSum(9, [3, 4, 5, 2])); +console.log(bestSum(18, [4, 5, 9, 2])); +console.log(bestSum(300, [7, 14])); From 8aae0518a0da1412669f2353a52973a983ce372e Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Tue, 24 Aug 2021 21:25:50 +0300 Subject: [PATCH 06/11] can construct a word --- index.js | 53 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 9a8596f..a818067 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,38 @@ -function bestSum(targetSum, numArr, memo = {}) { - if (targetSum in memo) return memo[targetSum]; - if (targetSum === 0) return []; - if (targetSum < 0) return null; - let shortest = null; +function canConstruct(target, wordBank, memo = {}) { + if (target in memo) return memo[target]; + if (target === "") return true; - for (let num of numArr) { - const remainder = targetSum - num; - const returnedValue = bestSum(remainder, numArr, memo); - if (returnedValue) { - memo[targetSum] = [...returnedValue, num]; - if (shortest === null || memo[targetSum].length < shortest.length) { - shortest = memo[targetSum]; + for (const word of wordBank) { + if (target.indexOf(word) === 0) { + const suffix = target.slice(word.length); + // ! Don't Forget Passing Down The MEMO + if (canConstruct(suffix, wordBank, memo)) { + memo[target] = true; + return true; } } } - memo[targetSum] = shortest; - return shortest; + + memo[target] = false; + return false; } -console.log(bestSum(1, [9, 2])); -console.log(bestSum(2, [1, 4, 5, 9, 2])); -console.log(bestSum(3, [2, 4, 5, 9, 1])); -console.log(bestSum(3, [1, 4, 5, 9, 2])); -console.log(bestSum(9, [3, 4, 5, 2])); -console.log(bestSum(18, [4, 5, 9, 2])); -console.log(bestSum(300, [7, 14])); +console.log(canConstruct(`programming`, ["amming", "p", "rog"])); // false +console.log(canConstruct(`programming`, ["ramming", "p", "rog"])); // true +console.log(canConstruct("interview", ["ew", "vi", "in", "ter"])); // true +console.log( + canConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ + "o", + "oo", + "ooo", + "oooo", + "h", + "w", + ]) +); +console.log( + canConstruct( + "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", + ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"] + ) +); From 7b3f0dc82261ef13705687b6d2be280cfc01fc85 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Thu, 26 Aug 2021 18:46:46 +0300 Subject: [PATCH 07/11] count constructer for finding a targer in wordBank --- index.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index a818067..ae30c6e 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,27 @@ -function canConstruct(target, wordBank, memo = {}) { +function countConstruct(target, wordBank, memo = {}) { if (target in memo) return memo[target]; - if (target === "") return true; - + if (target === "") { + return 1; + } for (const word of wordBank) { if (target.indexOf(word) === 0) { const suffix = target.slice(word.length); // ! Don't Forget Passing Down The MEMO - if (canConstruct(suffix, wordBank, memo)) { - memo[target] = true; - return true; - } + if (!(target in memo)) memo[target] = 0; + memo[target] += countConstruct(suffix, wordBank, memo); } } - memo[target] = false; - return false; + return memo[target] || 0; } -console.log(canConstruct(`programming`, ["amming", "p", "rog"])); // false -console.log(canConstruct(`programming`, ["ramming", "p", "rog"])); // true -console.log(canConstruct("interview", ["ew", "vi", "in", "ter"])); // true +console.log(countConstruct(`programming`, ["amming", "p", "rog"])); // 0 +console.log( + countConstruct(`programming`, ["ramming", "ram", "ming", "p", "rog"]) +); // 2 +console.log(countConstruct("interview", ["ew", "vi", "in", "ter"])); // 1 console.log( - canConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ + countConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ "o", "oo", "ooo", @@ -31,7 +31,7 @@ console.log( ]) ); console.log( - canConstruct( + countConstruct( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"] ) From 442a3b95aded7b6b02448d692bededbcdcb63a52 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Thu, 26 Aug 2021 18:50:09 +0300 Subject: [PATCH 08/11] another solution for Count Word Constuct --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ae30c6e..269eb79 100644 --- a/index.js +++ b/index.js @@ -3,16 +3,18 @@ function countConstruct(target, wordBank, memo = {}) { if (target === "") { return 1; } + let totalCount = 0; for (const word of wordBank) { if (target.indexOf(word) === 0) { const suffix = target.slice(word.length); // ! Don't Forget Passing Down The MEMO if (!(target in memo)) memo[target] = 0; - memo[target] += countConstruct(suffix, wordBank, memo); + totalCount += countConstruct(suffix, wordBank, memo); } } - return memo[target] || 0; + memo[target] = totalCount; + return totalCount; } console.log(countConstruct(`programming`, ["amming", "p", "rog"])); // 0 @@ -29,10 +31,10 @@ console.log( "h", "w", ]) -); +); // 6376719104 console.log( countConstruct( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"] ) -); +); // 0 From f95588bc4d17824e623a58c87845f0e51cd27ac8 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Fri, 3 Sep 2021 18:19:14 +0300 Subject: [PATCH 09/11] initing TS --- index.js | 55 +++++++++++++++------------------- index.ts | 44 +++++++++++++++++++++++++++ tsconfig.json | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 index.ts create mode 100644 tsconfig.json diff --git a/index.js b/index.js index 269eb79..e25e467 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,33 @@ -function countConstruct(target, wordBank, memo = {}) { - if (target in memo) return memo[target]; - if (target === "") { - return 1; - } - let totalCount = 0; - for (const word of wordBank) { - if (target.indexOf(word) === 0) { - const suffix = target.slice(word.length); - // ! Don't Forget Passing Down The MEMO - if (!(target in memo)) memo[target] = 0; - totalCount += countConstruct(suffix, wordBank, memo); +function countConstruct(target, wordBank, memo) { + if (memo === void 0) { memo = {}; } + if (target in memo) + return memo[target]; + if (target === "") { + return 1; } - } - - memo[target] = totalCount; - return totalCount; + var totalCount = 0; + for (var _i = 0, wordBank_1 = wordBank; _i < wordBank_1.length; _i++) { + var word = wordBank_1[_i]; + if (target.indexOf(word) === 0) { + var suffix = target.slice(word.length); + // ! Don't Forget Passing Down The MEMO + if (!(target in memo)) + memo[target] = 0; + totalCount += countConstruct(suffix, wordBank, memo); + } + } + memo[target] = totalCount; + return totalCount; } - -console.log(countConstruct(`programming`, ["amming", "p", "rog"])); // 0 -console.log( - countConstruct(`programming`, ["ramming", "ram", "ming", "p", "rog"]) -); // 2 +console.log(countConstruct("programming", ["amming", "p", "rog"])); // 0 +console.log(countConstruct("programming", ["ramming", "ram", "ming", "p", "rog"])); // 2 console.log(countConstruct("interview", ["ew", "vi", "in", "ter"])); // 1 -console.log( - countConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ +console.log(countConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ "o", "oo", "ooo", "oooo", "h", "w", - ]) -); // 6376719104 -console.log( - countConstruct( - "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", - ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"] - ) -); // 0 +])); // 6376719104 +console.log(countConstruct("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"])); // 0 diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..a71240e --- /dev/null +++ b/index.ts @@ -0,0 +1,44 @@ +function countConstruct( + target: string, + wordBank: string[], + memo: { [key: string]: number } = {} +): number { + if (target in memo) return memo[target]; + if (target === "") { + return 1; + } + let totalCount = 0; + for (const word of wordBank) { + if (target.indexOf(word) === 0) { + const suffix = target.slice(word.length); + // ! Don't Forget Passing Down The MEMO + if (!(target in memo)) memo[target] = 0; + totalCount += countConstruct(suffix, wordBank, memo); + } + } + + memo[target] = totalCount; + return totalCount; +} + +console.log(countConstruct(`programming`, ["amming", "p", "rog"])); // 0 +console.log( + countConstruct(`programming`, ["ramming", "ram", "ming", "p", "rog"]) +); // 2 +console.log(countConstruct("interview", ["ew", "vi", "in", "ter"])); // 1 +console.log( + countConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ + "o", + "oo", + "ooo", + "oooo", + "h", + "w", + ]) +); // 6376719104 +console.log( + countConstruct( + "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", + ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"] + ) +); // 0 diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..05760f8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,83 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "ESNEXT" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */, + "module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, + "lib": [ + "DOM", + "ES5", + "ES2015", + "ES2016", + "ES2017", + "ES2018", + "ES2019", + "ES2020", + "ES2021", + "ESNEXT" + ] /* Specify library files to be included in the compilation. */, + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + // "outDir": "./", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + + /* Advanced Options */ + "skipLibCheck": true /* Skip type checking of declaration files. */, + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + } +} From a66cb561ac1ac61cce53556362e062ce2ad47725 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Fri, 3 Sep 2021 19:03:46 +0300 Subject: [PATCH 10/11] Multiplying --- index.js | 45 +++++++++++++++---------------------- index.ts | 57 ++++++++++++++++++----------------------------- package-lock.json | 13 +++++++++++ package.json | 1 + 4 files changed, 54 insertions(+), 62 deletions(-) diff --git a/index.js b/index.js index e25e467..5851d8c 100644 --- a/index.js +++ b/index.js @@ -1,33 +1,24 @@ -function countConstruct(target, wordBank, memo) { +function multiplication(n, m, memo) { if (memo === void 0) { memo = {}; } - if (target in memo) - return memo[target]; - if (target === "") { - return 1; + if (n === 0 || m === 0) { + return 0; } + if (m.toString().length < 2) + return n * m; + if (m in memo) + return memo[m]; var totalCount = 0; - for (var _i = 0, wordBank_1 = wordBank; _i < wordBank_1.length; _i++) { - var word = wordBank_1[_i]; - if (target.indexOf(word) === 0) { - var suffix = target.slice(word.length); - // ! Don't Forget Passing Down The MEMO - if (!(target in memo)) - memo[target] = 0; - totalCount += countConstruct(suffix, wordBank, memo); - } + var arrOfM = m.toString().split("").reverse(); + for (var i = 0; i < arrOfM.length; i++) { + var num = arrOfM[i]; + memo[num] = Number(multiplication(n, Number(num), memo) + Array(i).fill(0).join("")); + totalCount += memo[num]; } - memo[target] = totalCount; + memo[m] = totalCount; return totalCount; } -console.log(countConstruct("programming", ["amming", "p", "rog"])); // 0 -console.log(countConstruct("programming", ["ramming", "ram", "ming", "p", "rog"])); // 2 -console.log(countConstruct("interview", ["ew", "vi", "in", "ter"])); // 1 -console.log(countConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ - "o", - "oo", - "ooo", - "oooo", - "h", - "w", -])); // 6376719104 -console.log(countConstruct("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"])); // 0 +console.log(multiplication(2, 2)); // 4 +console.log(multiplication(22, 22)); // 484 +console.log(multiplication(22, 2253)); +console.log(multiplication(214532, 0)); // 0 +console.log(multiplication(214532, 145368722)); diff --git a/index.ts b/index.ts index a71240e..7b40584 100644 --- a/index.ts +++ b/index.ts @@ -1,44 +1,31 @@ -function countConstruct( - target: string, - wordBank: string[], +function multiplication( + n: number, + m: number, memo: { [key: string]: number } = {} ): number { - if (target in memo) return memo[target]; - if (target === "") { - return 1; + if (n === 0 || m === 0) { + return 0; } + if (m.toString().length < 2) return n * m; + if (m in memo) return memo[m]; + let totalCount = 0; - for (const word of wordBank) { - if (target.indexOf(word) === 0) { - const suffix = target.slice(word.length); - // ! Don't Forget Passing Down The MEMO - if (!(target in memo)) memo[target] = 0; - totalCount += countConstruct(suffix, wordBank, memo); - } + const arrOfM = m.toString().split("").reverse(); + + for (let i = 0; i < arrOfM.length; i++) { + let num = arrOfM[i]; + memo[num] = Number( + multiplication(n, Number(num), memo) + Array(i).fill(0).join("") + ); + totalCount += memo[num]; } - memo[target] = totalCount; + memo[m] = totalCount; return totalCount; } -console.log(countConstruct(`programming`, ["amming", "p", "rog"])); // 0 -console.log( - countConstruct(`programming`, ["ramming", "ram", "ming", "p", "rog"]) -); // 2 -console.log(countConstruct("interview", ["ew", "vi", "in", "ter"])); // 1 -console.log( - countConstruct("ooooooowoooooooooooooooohoooooooooooooo", [ - "o", - "oo", - "ooo", - "oooo", - "h", - "w", - ]) -); // 6376719104 -console.log( - countConstruct( - "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", - ["e", "ee", "eee", "eeee", "eeeee", "eeeeee"] - ) -); // 0 +console.log(multiplication(2, 2)); // 4 +console.log(multiplication(22, 22)); // 484 +console.log(multiplication(22, 2253)); // 49566 +console.log(multiplication(214532, 0)); // 0 +console.log(multiplication(214532, 145368722)); // 31186242668104 diff --git a/package-lock.json b/package-lock.json index f61b636..9b3ddcd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "packages": { "": { "devDependencies": { + "@types/node": "^16.7.10", "@typescript-eslint/eslint-plugin": "^4.14.1", "@typescript-eslint/parser": "^4.14.1", "eslint": "^7.19.0", @@ -199,6 +200,12 @@ "integrity": "sha512-YSBPTLTVm2e2OoQIDYx8HaeWJ5tTToLH67kXR7zYNGupXMEHa2++G8k+DczX2cFVgalypqtyZIcU19AFcmOpmg==", "dev": true }, + "node_modules/@types/node": { + "version": "16.7.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.7.10.tgz", + "integrity": "sha512-S63Dlv4zIPb8x6MMTgDq5WWRJQe56iBEY0O3SOFA9JrRienkOVDXSXBjjJw6HTNQYSE2JI6GMCR6LVbIMHJVvA==", + "dev": true + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "4.28.5", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.28.5.tgz", @@ -2850,6 +2857,12 @@ "integrity": "sha512-YSBPTLTVm2e2OoQIDYx8HaeWJ5tTToLH67kXR7zYNGupXMEHa2++G8k+DczX2cFVgalypqtyZIcU19AFcmOpmg==", "dev": true }, + "@types/node": { + "version": "16.7.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.7.10.tgz", + "integrity": "sha512-S63Dlv4zIPb8x6MMTgDq5WWRJQe56iBEY0O3SOFA9JrRienkOVDXSXBjjJw6HTNQYSE2JI6GMCR6LVbIMHJVvA==", + "dev": true + }, "@typescript-eslint/eslint-plugin": { "version": "4.28.5", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.28.5.tgz", diff --git a/package.json b/package.json index 0551e5a..ff9cf67 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "devDependencies": { + "@types/node": "^16.7.10", "@typescript-eslint/eslint-plugin": "^4.14.1", "@typescript-eslint/parser": "^4.14.1", "eslint": "^7.19.0", From 2705d4731b7aa801f17dc96cd1cf7042b0ecb622 Mon Sep 17 00:00:00 2001 From: Ahmed eldessouki Date: Fri, 3 Sep 2021 22:57:22 +0300 Subject: [PATCH 11/11] the output is different from what's on codewars --- index.js | 69 ++++++++++++++++++++++++++++++++++++++------------------ index.ts | 53 +++++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index 5851d8c..5226bc0 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,49 @@ -function multiplication(n, m, memo) { - if (memo === void 0) { memo = {}; } - if (n === 0 || m === 0) { - return 0; +function multiply(value, times) { + if (value === null) return null; + if (typeof times !== "number" || isNaN(times)) throw new Error(""); + + function checkTimes(n) { + if (n < 0 || times % 1 !== 0) { + throw new Error(""); } - if (m.toString().length < 2) - return n * m; - if (m in memo) - return memo[m]; - var totalCount = 0; - var arrOfM = m.toString().split("").reverse(); - for (var i = 0; i < arrOfM.length; i++) { - var num = arrOfM[i]; - memo[num] = Number(multiplication(n, Number(num), memo) + Array(i).fill(0).join("")); - totalCount += memo[num]; - } - memo[m] = totalCount; - return totalCount; + } + + switch (typeof value) { + case "number": + if (times > Number.MAX_VALUE) { + throw new Error(""); + } + return value * times; + case "string": + checkTimes(times); + return times > 0 ? value.repeat(times) : ``; + case "object": + checkTimes(times); + return times > 0 ? Array(times).fill(value) : []; + case "function": + if (times === 0) return; + checkTimes(times); + return function () { + for (var i = 0; i < times; i++) { + value.apply(this, arguments); + } + }; + default: + return value; + } } -console.log(multiplication(2, 2)); // 4 -console.log(multiplication(22, 22)); // 484 -console.log(multiplication(22, 2253)); -console.log(multiplication(214532, 0)); // 0 -console.log(multiplication(214532, 145368722)); +console.log(function func() { + if (valid) { + console.log("still valid, original called"); + hits++; + if (this !== context) { + Test.expect(false, "Incorrect context."); + valid = false; + } else if ( + Test.inspect(Array.prototype.slice.call(arguments, 0)) === args + ) { + Test.expect(false, "Incorrect arguments."); + valid = false; + } + } +}, 222); // 4 diff --git a/index.ts b/index.ts index 7b40584..e6082d9 100644 --- a/index.ts +++ b/index.ts @@ -1,31 +1,34 @@ -function multiplication( - n: number, - m: number, - memo: { [key: string]: number } = {} -): number { - if (n === 0 || m === 0) { - return 0; +function multiply(value: any, times: number): any { + function checkTimes(n: number) { + if (n < 0) { + throw new Error(""); + } } - if (m.toString().length < 2) return n * m; - if (m in memo) return memo[m]; - let totalCount = 0; - const arrOfM = m.toString().split("").reverse(); - - for (let i = 0; i < arrOfM.length; i++) { - let num = arrOfM[i]; - memo[num] = Number( - multiplication(n, Number(num), memo) + Array(i).fill(0).join("") - ); - totalCount += memo[num]; + switch (typeof value) { + case "number": + if (times > Number.MAX_VALUE) { + throw new Error(""); + } + return value * times; + case "string": + checkTimes(times); + return times > 0 ? value.repeat(times) : ``; + case "object": + checkTimes(times); + return times > 0 ? Array(times).fill(value) : []; + default: + return value; } - memo[m] = totalCount; - return totalCount; + if (times === 0) { + return ""; + } + return value.repeat(times); } -console.log(multiplication(2, 2)); // 4 -console.log(multiplication(22, 22)); // 484 -console.log(multiplication(22, 2253)); // 49566 -console.log(multiplication(214532, 0)); // 0 -console.log(multiplication(214532, 145368722)); // 31186242668104 +console.log(multiply("asd-", 2)); // 4 +console.log(multiply("asd-", 22)); // 484 +console.log(multiply("asd-", 2253)); // 49566 +console.log(multiply("asd-", 0)); // 0 +console.log(multiply("asd-", 145368722)); // 31186242668104