From f3b17ada9bcbf112cc35848ad81db814e96b07ae Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Fri, 18 Mar 2022 16:21:59 +0600 Subject: [PATCH 1/3] refactor: added one implementation of fibonacciCahce --- Cache/test/LFUCache.test.js | 23 ++++------------------- Cache/test/LRUCache.test.js | 21 ++------------------- Cache/test/TesterFunction.js | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 38 deletions(-) create mode 100644 Cache/test/TesterFunction.js diff --git a/Cache/test/LFUCache.test.js b/Cache/test/LFUCache.test.js index 5e4bd67ac0..4c39a38298 100644 --- a/Cache/test/LFUCache.test.js +++ b/Cache/test/LFUCache.test.js @@ -1,4 +1,5 @@ import { LFUCache } from '../LFUCache' +import { fibonacciCache } from './TesterFunction' describe('LFUCache', () => { it('Example 1 (Small Cache, size=2)', () => { @@ -28,27 +29,11 @@ describe('LFUCache', () => { it('Example 2 (Computing Fibonacci Series, size=100)', () => { const cache = new LFUCache(100) + for (let i = 1; i <= 100; i++) { - fib(i, cache) + fibonacciCache(i, cache) } + expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)') }) }) - -// Helper for building and caching Fibonacci series -function fib (num, cache = null) { - if (cache) { - const value = cache.get(num) - if (value) { - return value - } - } - if (num === 1 || num === 2) { - return 1 - } - const result = fib(num - 1, cache) + fib(num - 2, cache) - if (cache) { - cache.set(num, result) - } - return result -} diff --git a/Cache/test/LRUCache.test.js b/Cache/test/LRUCache.test.js index 6ebeb1a01f..07ed9adf6a 100644 --- a/Cache/test/LRUCache.test.js +++ b/Cache/test/LRUCache.test.js @@ -1,4 +1,5 @@ import { LRUCache } from '../LRUCache' +import { fibonacciCache } from './TesterFunction' describe('LRUCache', () => { it('Example 1 (Small Cache, size=2)', () => { @@ -29,26 +30,8 @@ describe('LRUCache', () => { it('Example 2 (Computing Fibonacci Series, size=100)', () => { const cache = new LRUCache(100) for (let i = 1; i <= 100; i++) { - fib(i, cache) + fibonacciCache(i, cache) } expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)') }) }) - -// Helper for building and caching Fibonacci series -function fib (num, cache = null) { - if (cache) { - const value = cache.get(num) - if (value) { - return value - } - } - if (num === 1 || num === 2) { - return 1 - } - const result = fib(num - 1, cache) + fib(num - 2, cache) - if (cache) { - cache.set(num, result) - } - return result -} diff --git a/Cache/test/TesterFunction.js b/Cache/test/TesterFunction.js new file mode 100644 index 0000000000..f00365d8a4 --- /dev/null +++ b/Cache/test/TesterFunction.js @@ -0,0 +1,19 @@ +export const fibonacciCache = (num, cache = null) => { + if (cache) { + const value = cache.get(num) + + if (value !== null) { + return value + } + } + + if (num === 1 || num === 2) { + return 1 + } + + const result = fibonacciCache(num - 1, cache) + fibonacciCache(num - 2, cache) + + cache && cache.set(num, result) + + return result +} From 5136f7dbb4b65de6913af7d838401b6acf053911 Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Fri, 18 Mar 2022 16:58:22 +0600 Subject: [PATCH 2/3] chore: move union function --- Cache/test/Memoize.test.js | 12 +----------- Cache/test/TesterFunction.js | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Cache/test/Memoize.test.js b/Cache/test/Memoize.test.js index f6ecee7129..1ebad7075c 100644 --- a/Cache/test/Memoize.test.js +++ b/Cache/test/Memoize.test.js @@ -1,20 +1,10 @@ import { memoize } from '../Memoize' +import { union } from './TesterFunction' import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber' import { factorial } from '../../Recursive/Factorial' const multipleFactorials = (arr) => arr.map(factorial) -/** - * @title implementation of union function - * @param {Set} sets - * @return {new Set} - */ -function union (...sets) { - return new Set( - sets.reduce((flatArray, set) => [...flatArray, ...set], []) - ) -} - describe('Testing Memoize', () => { it('expects the fibonacci function to use the cache on the second call', () => { const memoFibonacci = memoize(fibonacci) diff --git a/Cache/test/TesterFunction.js b/Cache/test/TesterFunction.js index f00365d8a4..aecdcc6193 100644 --- a/Cache/test/TesterFunction.js +++ b/Cache/test/TesterFunction.js @@ -1,19 +1,37 @@ -export const fibonacciCache = (num, cache = null) => { +/** + * @function fibonacciCache + * @description - this is a cached variant of fib number + * @param {number} n - Real number (n > -1) + * @param {Object} cache + * @returns {number} + */ +export const fibonacciCache = (n, cache = null) => { if (cache) { - const value = cache.get(num) + const value = cache.get(n) if (value !== null) { return value } } - if (num === 1 || num === 2) { + if (n === 1 || n === 2) { return 1 } - const result = fibonacciCache(num - 1, cache) + fibonacciCache(num - 2, cache) + const result = fibonacciCache(n - 1, cache) + fibonacciCache(n - 2, cache) - cache && cache.set(num, result) + cache && cache.set(n, result) return result } + +/** + * @title implementation of union function + * @param {Set} sets + * @return {new Set} + */ +export const union = (...sets) => { + return new Set( + sets.reduce((flatArray, set) => [...flatArray, ...set], []) + ) +} From 95411262cc2c5812946df5a764d4a25101dd4bc2 Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Sun, 20 Mar 2022 11:25:07 +0600 Subject: [PATCH 3/3] chore: renamed the cacheTest file --- Cache/test/LFUCache.test.js | 2 +- Cache/test/LRUCache.test.js | 2 +- Cache/test/Memoize.test.js | 2 +- Cache/test/{TesterFunction.js => cacheTest.js} | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename Cache/test/{TesterFunction.js => cacheTest.js} (100%) diff --git a/Cache/test/LFUCache.test.js b/Cache/test/LFUCache.test.js index 4c39a38298..94ac0165e2 100644 --- a/Cache/test/LFUCache.test.js +++ b/Cache/test/LFUCache.test.js @@ -1,5 +1,5 @@ import { LFUCache } from '../LFUCache' -import { fibonacciCache } from './TesterFunction' +import { fibonacciCache } from './cacheTest' describe('LFUCache', () => { it('Example 1 (Small Cache, size=2)', () => { diff --git a/Cache/test/LRUCache.test.js b/Cache/test/LRUCache.test.js index 07ed9adf6a..b03d609d66 100644 --- a/Cache/test/LRUCache.test.js +++ b/Cache/test/LRUCache.test.js @@ -1,5 +1,5 @@ import { LRUCache } from '../LRUCache' -import { fibonacciCache } from './TesterFunction' +import { fibonacciCache } from './cacheTest' describe('LRUCache', () => { it('Example 1 (Small Cache, size=2)', () => { diff --git a/Cache/test/Memoize.test.js b/Cache/test/Memoize.test.js index 1ebad7075c..5d5948c489 100644 --- a/Cache/test/Memoize.test.js +++ b/Cache/test/Memoize.test.js @@ -1,5 +1,5 @@ import { memoize } from '../Memoize' -import { union } from './TesterFunction' +import { union } from './cacheTest' import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber' import { factorial } from '../../Recursive/Factorial' diff --git a/Cache/test/TesterFunction.js b/Cache/test/cacheTest.js similarity index 100% rename from Cache/test/TesterFunction.js rename to Cache/test/cacheTest.js pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy