Skip to content

Changes for consolidation #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cbe7e0c
Comply with ESM syntax.
lvlte Oct 9, 2021
3f32320
Comply with ESM. Convert live code example to Jest test.
lvlte Oct 9, 2021
2edbc23
Comply with ESM syntax. Remove unnecessary import.
lvlte Oct 9, 2021
4f4deac
Fix #748 : Revise algorithm implementation so that it actually return…
lvlte Oct 9, 2021
51897ca
Auto-update DIRECTORY.md
Oct 9, 2021
09da5ee
Fix GeneratePermutations so that it actually returns the permutations…
lvlte Oct 9, 2021
5624fa6
Merge branch 'issues/720' of https://github.com/lvlte/Javascript into…
lvlte Oct 9, 2021
97724cc
Merge branch 'TheAlgorithms:master' into issues/720
lvlte Oct 9, 2021
7f6a53a
KnightTour : convert live code example to Jest test (log remaining is…
lvlte Oct 9, 2021
5c4be76
Remove live code & console.log (Backtracking, Bit-manipulation, Ciphe…
lvlte Oct 9, 2021
ab7e519
LFUCache/LRUCache : convert live code example to Jest test.
lvlte Oct 9, 2021
3077968
Conversions algorithms : remove live code & console.log.
lvlte Oct 9, 2021
a3d44ad
Data Structure : remove live code & console.log
lvlte Oct 10, 2021
449ef45
Data Structure : Convert live test to Jest test. Remove live code & c…
lvlte Oct 10, 2021
29d2a84
Dynamic-Programming : remove live code & console.log, leave examples …
lvlte Oct 10, 2021
9212e6d
Remove live code & console.log, leave examples as comments (Geometry,…
lvlte Oct 10, 2021
5f45b54
Remove live code & console.log, leave examples as comments (ProjectEu…
lvlte Oct 10, 2021
8a7be96
Fix non-standard syntax
lvlte Oct 10, 2021
74f2965
Search/Sorts algoruthms : remove live code & console.log, leave examp…
lvlte Oct 11, 2021
90356f3
Exponential Search : Fix `'value' is not defined` error .
lvlte Oct 11, 2021
e18718b
Remove live code & console.log, leave examples as comments.
lvlte Oct 11, 2021
036ac90
Graph2.js : Convert live test into Jest test.
lvlte Oct 11, 2021
e3d605c
Breadth FIrst Tree Traversal : Convert live test into Jest test.
lvlte Oct 11, 2021
9218a5c
RomanToDecimal : live test and example were removed, adding Jest test…
lvlte Oct 11, 2021
87a3da7
Remove console.log
lvlte Oct 11, 2021
df4a783
Complying with JavaScript Standard Style (npx standard --fix).
lvlte Oct 11, 2021
cb6201f
Project Euler problems (numbering) : follow naming convention.
lvlte Oct 11, 2021
88ee132
Auto-update DIRECTORY.md
Oct 11, 2021
8c64f99
Officially switching to ES Module system.
lvlte Oct 11, 2021
e3a10ee
Pull/merge updated directory.md
lvlte Oct 11, 2021
274686a
RomanToDecimal : Fix import (typo).
lvlte Oct 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Backtracking/AllCombinationsOfSizeK.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ class Combinations {
constructor (n, k) {
this.n = n
this.k = k
this.combinationArray = [] // will be used for storing current combination
this.current = [] // will be used for storing current combination
this.combinations = []
}

findCombinations (high = this.n, total = this.k, low = 1) {
if (total === 0) {
console.log(this.combinationArray)
return
this.combinations.push([...this.current])
return this.combinations
}
for (let i = low; i <= high; i++) {
this.combinationArray.push(i)
this.current.push(i)
this.findCombinations(high, total - 1, i + 1)
this.combinationArray.pop()
this.current.pop()
}
return this.combinations
}
}

Expand Down
29 changes: 16 additions & 13 deletions Backtracking/GeneratePermutations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Problem Statement: Generate all distinct permutations of a string/array (all permutations should be in sorted order);
* Problem Statement: Generate all distinct permutations of a an array (all permutations should be in sorted order);
*
* What is permutations?
* - Permutation means possible arrangements in a set (here it is string/array);
* - Permutation means possible arrangements in a set (here it is an array);
*
* Reference to know more about permutations:
* - https://www.britannica.com/science/permutation
Expand All @@ -17,17 +17,20 @@ const swap = (arr, i, j) => {
return newArray
}

const permutations = (arr, low, high) => {
if (low === high) {
console.log(arr.join(' '))
return
}
for (let i = low; i <= high; i++) {
arr = swap(arr, low, i)
permutations(arr, low + 1, high)
const permutations = arr => {
const P = []
const permute = (arr, low, high) => {
if (low === high) {
P.push([...arr])
return P
}
for (let i = low; i <= high; i++) {
arr = swap(arr, low, i)
permute(arr, low + 1, high)
}
return P
}
return permute(arr, 0, arr.length - 1)
}

// Driver Code
const input = [1, 2, 3]
permutations(input, 0, input.length - 1)
export { permutations }
17 changes: 3 additions & 14 deletions Backtracking/KnightTour.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,16 @@ class OpenKnightTour {
return false
}

printBoard () {
printBoard (output = value => console.log(value)) {
// utility function to display the board
for (const row of this.board) {
let string = ''
for (const elem of row) {
string += elem + '\t'
}
console.log(string)
output(string)
}
}
}

function main () {
const board = new OpenKnightTour(5)

board.printBoard()
console.log('\n')

board.solve()

board.printBoard()
}

main()
export { OpenKnightTour }
9 changes: 5 additions & 4 deletions Backtracking/NQueen.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class NQueen {

solve (col = 0) {
if (col >= this.size) {
this.printBoard()
this.solutionCount++
return true
}
Expand All @@ -52,10 +51,12 @@ class NQueen {
return false
}

printBoard () {
console.log('\n')
printBoard (output = value => console.log(value)) {
if (!output._isMockFunction) {
output('\n')
}
for (const row of this.board) {
console.log(...row)
output(row)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions Backtracking/Sudoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ class Sudoku {
return this.board[row].slice(start, end)
}

printBoard () {
printBoard (output = (...v) => console.log(...v)) {
// helper function to display board
for (let i = 0; i < 9; i++) {
if (i % 3 === 0 && i !== 0) console.log('- - - - - - - - - - - -')
console.log(
if (i % 3 === 0 && i !== 0) {
output('- - - - - - - - - - - -')
}
output(
...this.getSection(i, [0, 3]), ' | ',
...this.getSection(i, [3, 6]), ' | ',
...this.getSection(i, [6, 9]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Combinations } from '../AllCombinationsOfSizeK'
describe('AllCombinationsOfSizeK', () => {
it('should return 3x2 matrix solution for n = 3 and k = 2', () => {
const test1 = new Combinations(3, 2)
expect(test1.findCombinations).toEqual([[1, 2], [1, 3], [2, 3]])
expect(test1.findCombinations()).toEqual([[1, 2], [1, 3], [2, 3]])
})

it('should return 6x2 matrix solution for n = 3 and k = 2', () => {
it('should return 6x2 matrix solution for n = 4 and k = 2', () => {
const test2 = new Combinations(4, 2)
expect(test2.findCombinations).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
expect(test2.findCombinations()).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
})
})
14 changes: 14 additions & 0 deletions Backtracking/tests/GeneratePermutations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { permutations } from '../GeneratePermutations'

describe('Permutations', () => {
it('Permutations of [1, 2, 3]', () => {
expect(permutations([1, 2, 3])).toEqual([
[1, 2, 3],
[1, 3, 2],
[2, 1, 3],
[2, 3, 1],
[3, 1, 2],
[3, 2, 1]
])
})
})
23 changes: 23 additions & 0 deletions Backtracking/tests/KnightTour.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OpenKnightTour } from '../KnightTour'

describe('OpenKnightTour', () => {
it('OpenKnightTour(5)', () => {
const KT = new OpenKnightTour(5)
expect(KT.board).toEqual([
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
])

KT.solve()
expect(KT.board).toEqual([
[19, 4, 15, 10, 25],
[14, 9, 18, 5, 16],
[1, 20, 3, 24, 11],
[8, 13, 22, 17, 6],
[21, 2, 7, 12, 23]
])
})
})
3 changes: 1 addition & 2 deletions Bit-Manipulation/BinaryCountSetBits.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ function BinaryCountSetBits (a) {
return a.toString(2).split('1').length - 1
}

// Run `binary_and` Function to find the binary and operation
console.log(BinaryCountSetBits(251))
export { BinaryCountSetBits }
2 changes: 1 addition & 1 deletion Bit-Manipulation/test/IsPowerOfTwo.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IsPowerOfTwo} from '../IsPowerOfTwo'
import { IsPowerOfTwo } from '../IsPowerOfTwo'

test('Check if 0 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(0)
Expand Down
8 changes: 7 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ should add unique value.
* **Example:**`UserProfile.js` is allowed but `userprofile.js`,`Userprofile.js`,`user-Profile.js`,`userProfile.js` are
not.

#### Module System

We use the [ES Module](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) system, which bring an official, standardized module system to JavaScript.

It roughly means you will need to use `export` and `import` statements instead of `module.exports` and `require()`.

#### Testing

Be confident that your code works. When was the last time you committed a code change, your build failed, and half of
Expand Down Expand Up @@ -125,7 +131,7 @@ function sumOfArray (arrayOfNumbers) {
return (sum)
}
```
*
*
* Avoid using global variables and avoid `==`
* Please use `let` over `var`
* Please refrain from using `console.log` or any other console methods
Expand Down
39 changes: 1 addition & 38 deletions Cache/LFUCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,41 +103,4 @@ class LFUCache {
}
}

function main () {
// Example 1 (Small Cache)
const cache = new LFUCache(2)
cache.set(1, 1)
cache.set(2, 2)

console.log(cache.get(1))

cache.set(3, 3)

console.log(cache.get(2)) // cache miss

cache.set(4, 4)

console.log(cache.get(1)) // cache miss
console.log(cache.get(3))
console.log(cache.get(4))

console.log('Example Cache: ', cache.cacheInfo(), '\n')

// Example 2 (Computing Fibonacci Series - 100 terms)
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
}

const fibCache = new LFUCache(100)
for (let i = 1; i <= 100; i++) { fib(i, fibCache) }
console.log('Fibonacci Series Cache: ', fibCache.cacheInfo(), '\n')
}

main()
export { LFUCache }
39 changes: 1 addition & 38 deletions Cache/LRUCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,41 +86,4 @@ class LRUCache {
}
}

function main () {
// Example 1 (Small Cache)
const cache = new LRUCache(2)
cache.set(1, 1)
cache.set(2, 2)

console.log(cache.get(1))

cache.set(3, 3)

console.log(cache.get(2)) // cache miss

cache.set(4, 4)

console.log(cache.get(1)) // cache miss
console.log(cache.get(3))
console.log(cache.get(4))

console.log('Example Cache: ', cache.cacheInfo(), '\n')

// Example 2 (Computing Fibonacci Series - 100 terms)
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
}

const fibCache = new LRUCache(100)
for (let i = 1; i <= 100; i++) { fib(i, fibCache) }
console.log('Fibonacci Series Cache: ', fibCache.cacheInfo(), '\n')
}

main()
export { LRUCache }
54 changes: 54 additions & 0 deletions Cache/test/LFUCache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { LFUCache } from '../LFUCache'

describe('LFUCache', () => {
it('Example 1 (Small Cache, size=2)', () => {
const cache = new LFUCache(2)
cache.set(1, 1)
cache.set(2, 2)

expect(cache.get(1)).toBe(1)
expect(cache.get(2)).toBe(2)

// Additional entries triggers cache rotate
cache.set(3, 3)

// Then we should have a cache miss for the first entry added
expect(cache.get(1)).toBe(null)
expect(cache.get(2)).toBe(2)
expect(cache.get(3)).toBe(3)

cache.set(4, 4)
expect(cache.get(1)).toBe(null) // cache miss
expect(cache.get(2)).toBe(null) // cache miss
expect(cache.get(3)).toBe(3)
expect(cache.get(4)).toBe(4)

expect(cache.cacheInfo()).toBe('CacheInfo(hits=6, misses=3, capacity=2, current size=2)')
})

it('Example 2 (Computing Fibonacci Series, size=100)', () => {
const cache = new LFUCache(100)
for (let i = 1; i <= 100; i++) {
fib(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
}
Loading