From 28502696746fbc61c5d6a095f5f8f554f7a327f5 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:49:18 +0530 Subject: [PATCH 01/33] Added Longest Common Subsequence --- .../longestCommonSubsequence.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Dynamic-Programming/longestCommonSubsequence.js diff --git a/Dynamic-Programming/longestCommonSubsequence.js b/Dynamic-Programming/longestCommonSubsequence.js new file mode 100644 index 000000000..f4e0c7fb2 --- /dev/null +++ b/Dynamic-Programming/longestCommonSubsequence.js @@ -0,0 +1,31 @@ +/* + * Given two sequences, find the length of longest subsequence present in both of them. + * A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. + * For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg” +*/ + +function longestCommonSubsequence(x, y, str1, str2, dp) { + if (x == -1 || y == -1) return 0; + else { + if (dp[x][y] != 0) return dp[x][y]; + else { + if (str1[x] == str2[y]) { + return dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + } + else { + return dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) + } + } + } + +} + +function main() { + const str1 = "ABCDGH" + const str2 = "AEDFHR" + let dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) + const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp) + console.log(res); +} + +main() From 595319b65160ee93de83ba5fa7b58439661d3d45 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:53:31 +0530 Subject: [PATCH 02/33] Renamed the File --- .../{longestCommonSubsequence.js => LongestCommonSubsequence.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Dynamic-Programming/{longestCommonSubsequence.js => LongestCommonSubsequence.js} (100%) diff --git a/Dynamic-Programming/longestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js similarity index 100% rename from Dynamic-Programming/longestCommonSubsequence.js rename to Dynamic-Programming/LongestCommonSubsequence.js From b8a519399a8a3ba71aec47c36892865afbeb9e8f Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:59:07 +0530 Subject: [PATCH 03/33] Optimized the code --- Dynamic-Programming/LongestCommonSubsequence.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index f4e0c7fb2..ac5e61e3a 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -23,7 +23,7 @@ function longestCommonSubsequence(x, y, str1, str2, dp) { function main() { const str1 = "ABCDGH" const str2 = "AEDFHR" - let dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) + const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp) console.log(res); } From a56758580db98fb3564846bbd61f611d07525239 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 01:06:25 +0530 Subject: [PATCH 04/33] Optimized the code --- .../LongestCommonSubsequence.js | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index ac5e61e3a..9ba2edbbb 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -1,31 +1,36 @@ /* - * Given two sequences, find the length of longest subsequence present in both of them. - * A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. + * Given two sequences, find the length of longest subsequence present in both of them. + * A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. * For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg” */ -function longestCommonSubsequence(x, y, str1, str2, dp) { - if (x == -1 || y == -1) return 0; +function longestCommonSubsequence (x, y, str1, str2, dp) { + if (x === -1 || y === -1) { + return 0 + } else { - if (dp[x][y] != 0) return dp[x][y]; + if (dp[x][y] !== 0){ + return dp[x][y] + } else { - if (str1[x] == str2[y]) { - return dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + if (str1[x] === str2[y]) { + dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + return dp[x][y] } else { - return dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) + dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) + return dp[x][y] } } } - } -function main() { - const str1 = "ABCDGH" - const str2 = "AEDFHR" +function main () { + const str1 = 'ABCDGH' + const str2 = 'AEDFHR' const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp) - console.log(res); + console.log(res) } main() From fdc34c32dca3fe4381876693c448e0a599bf6dd8 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 01:13:17 +0530 Subject: [PATCH 05/33] Changed some styles as per the rule --- Dynamic-Programming/LongestCommonSubsequence.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index 9ba2edbbb..b5f028769 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -11,13 +11,12 @@ function longestCommonSubsequence (x, y, str1, str2, dp) { else { if (dp[x][y] !== 0){ return dp[x][y] - } + } else { if (str1[x] === str2[y]) { - dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp) return dp[x][y] - } - else { + } else { dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) return dp[x][y] } From e9fe0ce598fe81939cda5583a9523b07c8999d0c Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 01:16:33 +0530 Subject: [PATCH 06/33] Again some style fixed --- Dynamic-Programming/LongestCommonSubsequence.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index b5f028769..f4724ccb0 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -7,12 +7,10 @@ function longestCommonSubsequence (x, y, str1, str2, dp) { if (x === -1 || y === -1) { return 0 - } - else { - if (dp[x][y] !== 0){ + } else { + if (dp[x][y] !== 0) { return dp[x][y] - } - else { + } else { if (str1[x] === str2[y]) { dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp) return dp[x][y] From 838abade98e4ebbd79482ee3ca2c29abbe3eb2c5 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 11:47:48 +0530 Subject: [PATCH 07/33] Added Longest Increasing Subsequence program to the list --- .../LongestIncreasingSubsequence.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Dynamic-Programming/LongestIncreasingSubsequence.js diff --git a/Dynamic-Programming/LongestIncreasingSubsequence.js b/Dynamic-Programming/LongestIncreasingSubsequence.js new file mode 100644 index 000000000..1ea8dde65 --- /dev/null +++ b/Dynamic-Programming/LongestIncreasingSubsequence.js @@ -0,0 +1,26 @@ +/** + * A Dynamic Programming based solution for calculating Longest Increasing Subsequence + * https://en.wikipedia.org/wiki/Longest_increasing_subsequence + */ + +function main () { + const x = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] + const length = x.length + const dp = Array(length).fill(1) + + let res = 1 + + for (let i = 0; i < length; i++) { + for (let j = 0; j < i; j++) { + if (x[i] > x[j]) { + dp[i] = Math.max(dp[i], 1 + dp[j]) + if (dp[i] > res) + res = dp[i] + } + } + } + + console.log('Length of Longest Increasing Subsequence is:', res) +} + +main() From 87b2eb02923c3e6d154331ec85b56cded052f5da Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 11:51:10 +0530 Subject: [PATCH 08/33] Style changed --- Dynamic-Programming/LongestIncreasingSubsequence.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/LongestIncreasingSubsequence.js b/Dynamic-Programming/LongestIncreasingSubsequence.js index 1ea8dde65..43cadecce 100644 --- a/Dynamic-Programming/LongestIncreasingSubsequence.js +++ b/Dynamic-Programming/LongestIncreasingSubsequence.js @@ -14,12 +14,13 @@ function main () { for (let j = 0; j < i; j++) { if (x[i] > x[j]) { dp[i] = Math.max(dp[i], 1 + dp[j]) - if (dp[i] > res) + if (dp[i] > res) { res = dp[i] + } } } } - + console.log('Length of Longest Increasing Subsequence is:', res) } From 8a40a08f3e65da652e011db4232ddcccd8859316 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 22:22:14 +0530 Subject: [PATCH 09/33] Added 0-1-Knapsack Problem --- Dynamic-Programming/ZeroOneKnapsack.js | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Dynamic-Programming/ZeroOneKnapsack.js diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js new file mode 100644 index 000000000..f9cc2d450 --- /dev/null +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -0,0 +1,71 @@ +/** + * A Dynamic Programming based solution for calculating Zero One Knapsack + * https://en.wikipedia.org/wiki/Knapsack_problem + */ + +function zeroOneKnapsack (arr, n, cap, cache) { + if (cap == 0 || n == 0) { + cache[n][cap] = 0 + return cache[n][cap] + } + if (cache[n][cap] != -1) { + return cache[n][cap] + } + if (arr[n - 1][0] <= cap) { + cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack (arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack (arr, n - 1, cap, cache)) + return cache[n][cap] + } else { + cache[n][cap] = zeroOneKnapsack (arr, n - 1, cap, cache) + return cache[n][cap] + } +} + +function main () { + /* + Problem Statement: + You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity. + You have to cherry pick the artifact in order to maximize the total value of the artifacts you stole. + + Link for the Problem: https://www.hackerrank.com/contests/srin-aadc03/challenges/classic-01-knapsack + */ + let input = `1 + 4 5 + 1 8 + 2 4 + 3 0 + 2 5 + 2 3` + + input = input.trim().split('\n') + input.shift() + const length = input.length + + let i = 0 + while (i < length) { + const cap = Number(input[i].trim().split(' ')[0]) + const currlen = Number(input[i].trim().split(' ')[1]) + let j = i + 1; + const arr = [] + while (j <= i + currlen) { + arr.push(input[j]) + j++ + } + const newArr = [] + arr.map(e => { + newArr.push(e.trim().split(' ').map(Number)) + }) + const cache = [] + for (let i = 0; i <= currlen; i++) { + const temp = [] + for (let j = 0; j <= cap; j++) { + temp.push(-1) + } + cache.push(temp) + } + const result = zeroOneKnapsack(newArr, currlen, cap, cache) + console.log(result) + i += currlen + 1 + } +} + +main() \ No newline at end of file From 7a65d8929fdecb79979b5a3ef6698d7573dfcc42 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 22:27:53 +0530 Subject: [PATCH 10/33] Style Changed as per guidelines --- Dynamic-Programming/ZeroOneKnapsack.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js index f9cc2d450..e418db1ae 100644 --- a/Dynamic-Programming/ZeroOneKnapsack.js +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -4,18 +4,18 @@ */ function zeroOneKnapsack (arr, n, cap, cache) { - if (cap == 0 || n == 0) { + if (cap === 0 || n === 0) { cache[n][cap] = 0 return cache[n][cap] } - if (cache[n][cap] != -1) { + if (cache[n][cap] !== -1) { return cache[n][cap] } if (arr[n - 1][0] <= cap) { - cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack (arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack (arr, n - 1, cap, cache)) + cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack(arr, n - 1, cap, cache)) return cache[n][cap] } else { - cache[n][cap] = zeroOneKnapsack (arr, n - 1, cap, cache) + cache[n][cap] = zeroOneKnapsack(arr, n - 1, cap, cache) return cache[n][cap] } } @@ -44,7 +44,7 @@ function main () { while (i < length) { const cap = Number(input[i].trim().split(' ')[0]) const currlen = Number(input[i].trim().split(' ')[1]) - let j = i + 1; + let j = i + 1 const arr = [] while (j <= i + currlen) { arr.push(input[j]) @@ -68,4 +68,4 @@ function main () { } } -main() \ No newline at end of file +main() From 48c7bab8327f6aa1e92bd36f736679bd2ff57d48 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 23:44:17 +0530 Subject: [PATCH 11/33] Added Cycle Detection Problem in Linkedlist --- Data-Structures/Linked-List/CycleDetection.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Data-Structures/Linked-List/CycleDetection.js diff --git a/Data-Structures/Linked-List/CycleDetection.js b/Data-Structures/Linked-List/CycleDetection.js new file mode 100644 index 000000000..ff07bca60 --- /dev/null +++ b/Data-Structures/Linked-List/CycleDetection.js @@ -0,0 +1,31 @@ +/** + * A LinkedList based solution for Detect a Cycle in a list + * https://en.wikipedia.org/wiki/Cycle_detection + */ + +function main () { + /* + Problem Statement: + Given head, the head of a linked list, determine if the linked list has a cycle in it. + + Note: + * While Solving the problem in given link below, don't use main() function. + * Just use only the code inside main() function. + * The purpose of using main() function here is to aviod global variables. + + Link for the Problem: https://leetcode.com/problems/linked-list-cycle/ + */ + let fast = head + let slow = head + + while (fast != null && fast.next != null && slow != null) { + fast = fast.next.next + slow = slow.next + if (fast == slow) { + return true + } + } + return false +} + +main() From e31390f0a0f5f75ef31968020b62d37fe85d0c5e Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 23:55:40 +0530 Subject: [PATCH 12/33] Added Dummy head to avoid Undefined Error --- Data-Structures/Linked-List/CycleDetection.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Data-Structures/Linked-List/CycleDetection.js b/Data-Structures/Linked-List/CycleDetection.js index ff07bca60..b8e16fe71 100644 --- a/Data-Structures/Linked-List/CycleDetection.js +++ b/Data-Structures/Linked-List/CycleDetection.js @@ -15,6 +15,7 @@ function main () { Link for the Problem: https://leetcode.com/problems/linked-list-cycle/ */ + const head = '' // Reference to head is given in the problem. So please ignore this line let fast = head let slow = head From 3ab272da4b2f2185197a78dbd1d8b99073202ac3 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 5 Oct 2020 00:00:45 +0530 Subject: [PATCH 13/33] Style Changed --- Data-Structures/Linked-List/CycleDetection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/CycleDetection.js b/Data-Structures/Linked-List/CycleDetection.js index b8e16fe71..910fe2214 100644 --- a/Data-Structures/Linked-List/CycleDetection.js +++ b/Data-Structures/Linked-List/CycleDetection.js @@ -22,7 +22,7 @@ function main () { while (fast != null && fast.next != null && slow != null) { fast = fast.next.next slow = slow.next - if (fast == slow) { + if (fast === slow) { return true } } From 05d48b8ff9c7386a34781bf1d99dd726e74d95de Mon Sep 17 00:00:00 2001 From: Bogdan Lazar Date: Mon, 5 Oct 2020 18:03:52 +0000 Subject: [PATCH 14/33] Fix selection sort and add tests; fixes #414 (#418) * Fix selection sort and add tests; fixes #414 Co-authored-by: vinayak --- Sorts/SelectionSort.js | 40 +++++++++++++++++++++++++------------ Sorts/SelectionSort.test.js | 22 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 Sorts/SelectionSort.test.js diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index 38a4bfff2..c4a11e354 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -8,12 +8,19 @@ *from the unsorted subarray is picked and moved to the sorted subarray. */ -function selectionSort (items) { - var length = items.length - for (var i = 0; i < length - 1; i++) { +const selectionSort = (list) => { + if (!Array.isArray(list)) { + throw new TypeError('Given input is not an array') + } + const items = [...list] // We don't want to modify the original array + const length = items.length + for (let i = 0; i < length - 1; i++) { + if (typeof items[i] !== 'number') { + throw new TypeError('One of the items in your array is not a number') + } // Number of passes - var min = i // min holds the current minimum number position for each pass; i holds the Initial min number - for (var j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array + let min = i // min holds the current minimum number position for each pass; i holds the Initial min number + for (let j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array if (items[j] < items[min]) { // Compare the numbers min = j // Change the current min number position if a smaller num is found } @@ -21,16 +28,23 @@ function selectionSort (items) { if (min !== i) { // After each pass, if the current min num != initial min num, exchange the position. // Swap the numbers - [items[i], items[min]] = [items[min], [items[i]]] + [items[i], items[min]] = [items[min], items[i]] } } + return items } -// Implementation of Selection Sort +/* Implementation of Selection Sort -var ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -selectionSort(ar) -// Array after sort -console.log(ar) +(() => { + let array = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before Sort + console.log(array) + array = selectionSort(array) + // Array after sort + console.log(array) +})() + +*/ + +export { selectionSort } diff --git a/Sorts/SelectionSort.test.js b/Sorts/SelectionSort.test.js new file mode 100644 index 000000000..57ad1e8ec --- /dev/null +++ b/Sorts/SelectionSort.test.js @@ -0,0 +1,22 @@ +import { selectionSort } from './SelectionSort' + +describe('selectionSort', () => { + it('expects to return the array sorted in ascending order', () => { + var toSort = [5, 6, 7, 8, 1, 2, 12, 14] + const expected = [1, 2, 5, 6, 7, 8, 12, 14] + + expect(selectionSort(toSort)).toEqual(expected) + }) + + it('expects to throw if it is not a valid array', () => { + expect(() => selectionSort('abc')).toThrow('Given input is not an array') + expect(() => selectionSort(123)).toThrow('Given input is not an array') + expect(() => selectionSort({})).toThrow('Given input is not an array') + expect(() => selectionSort(null)).toThrow('Given input is not an array') + expect(() => selectionSort()).toThrow('Given input is not an array') + }) + + it('expects to throw if one of the elements in the array is not a number', () => { + expect(() => selectionSort([1, 'x', 2])).toThrow('One of the items in your array is not a number') + }) +}) From 0e5eb4edbe4ca35125e63ca940d184fa09fd4bb4 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 5 Oct 2020 18:04:14 +0000 Subject: [PATCH 15/33] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e447facda..f2de4c097 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -130,6 +130,7 @@ * [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js) * [RadixSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/RadixSort.js) * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.js) + * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.test.js) * [ShellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/ShellSort.js) * [TimSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TimSort.js) * [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js) From d87233cd1e4cd2b8409cd2ff9faa132d7c7b5647 Mon Sep 17 00:00:00 2001 From: illegalcall <44542765+illegalcall@users.noreply.github.com> Date: Mon, 5 Oct 2020 23:43:03 +0530 Subject: [PATCH 16/33] Create FibonacciNumber.js (#378) * Create FibonacciNumber.js * Update FibonacciNumber.js Co-authored-by: vinayak --- Dynamic-Programming/FibonacciNumber.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Dynamic-Programming/FibonacciNumber.js diff --git a/Dynamic-Programming/FibonacciNumber.js b/Dynamic-Programming/FibonacciNumber.js new file mode 100644 index 000000000..a39912f18 --- /dev/null +++ b/Dynamic-Programming/FibonacciNumber.js @@ -0,0 +1,18 @@ +// https://en.wikipedia.org/wiki/Fibonacci_number + +const fibonacci = (N) => { + // creating array to store values + const memo = new Array(N + 1) + memo[0] = 0 + memo[1] = 1 + for (let i = 2; i <= N; i++) { + memo[i] = memo[i - 1] + memo[i - 2] + } + return memo[N] +} + +// testing +(() => { + const number = 5 + console.log(number + 'th Fibonacci number is ' + fibonacci(number)) +})() From a8fe5e79a84141450d5b7ca83e2222a0a50a0b94 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 5 Oct 2020 18:13:20 +0000 Subject: [PATCH 17/33] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f2de4c097..29f0f4d73 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -50,6 +50,7 @@ * [ClimbingStairs](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ClimbingStairs.js) * [CoinChange](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/CoinChange.js) * [EditDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/EditDistance.js) + * [FibonacciNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/FibonacciNumber.js) * [KadaneAlgo](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/KadaneAlgo.js) * [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LevenshteinDistance.js) * [LongestCommonSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestCommonSubsequence.js) From c02fc1f6f6cf7d265804d97753345acf9ad60749 Mon Sep 17 00:00:00 2001 From: Stas Date: Mon, 5 Oct 2020 21:18:59 +0300 Subject: [PATCH 18/33] Added Mean Square Error (#417) * Added Mean Square Error * Update MeanSquareError.js Co-authored-by: vinayak --- Maths/MeanSquareError.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Maths/MeanSquareError.js diff --git a/Maths/MeanSquareError.js b/Maths/MeanSquareError.js new file mode 100644 index 000000000..de9dcfd7f --- /dev/null +++ b/Maths/MeanSquareError.js @@ -0,0 +1,26 @@ +// Wikipedia: https://en.wikipedia.org/wiki/Mean_squared_error + +const meanSquaredError = (predicted, expected) => { + if (!Array.isArray(predicted) || !Array.isArray(expected)) { + throw new TypeError('Argument must be an Array') + } + + if (predicted.length !== expected.length) { + throw new TypeError('The two lists must be of equal length') + } + + let err = 0 + + for (let i = 0; i < expected.length; i++) { + err += (expected[i] - predicted[i]) ** 2 + } + + return err / expected.length +} + +// testing +(() => { + console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0) + console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5) + console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3) +})() From 7a7cdd63597d3025861e83456e29fb07ecb0cdb6 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 5 Oct 2020 18:19:16 +0000 Subject: [PATCH 19/33] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 29f0f4d73..523148866 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -88,6 +88,7 @@ * [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js) * [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js) * [GridGet](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGet.js) + * [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js) * [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js) * [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js) * [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js) From 2bb330e68bb8cc519d1b5c7de51f1751064a9331 Mon Sep 17 00:00:00 2001 From: illegalcall <44542765+illegalcall@users.noreply.github.com> Date: Mon, 5 Oct 2020 23:52:52 +0530 Subject: [PATCH 20/33] Create FibonacciNumberRecursive.js (#380) * Create FibonacciNumberRecursive.js * Update FibonacciNumberRecursive.js Co-authored-by: vinayak --- Recursive/FibonacciNumberRecursive.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Recursive/FibonacciNumberRecursive.js diff --git a/Recursive/FibonacciNumberRecursive.js b/Recursive/FibonacciNumberRecursive.js new file mode 100644 index 000000000..406c86684 --- /dev/null +++ b/Recursive/FibonacciNumberRecursive.js @@ -0,0 +1,14 @@ +// https://en.wikipedia.org/wiki/Fibonacci_number + +const fibonacci = (N) => { + if (N === 0 || N === 1) return N + + return fibonacci(N - 2) + fibonacci(N - 1) +} + + +// testing +(() => { + const number = 5 + console.log(number + 'th Fibonacci number is ' + fibonacci(number)) +})() From 2c5e1e77ad62d65756a2e1e793488fe466a13c1f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 5 Oct 2020 18:23:08 +0000 Subject: [PATCH 21/33] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 523148866..9fd2d090c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -103,6 +103,7 @@ ## Recursive * [EucledianGCD](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/EucledianGCD.js) + * [FibonacciNumberRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/FibonacciNumberRecursive.js) * [TowerOfHanoi](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/TowerOfHanoi.js) ## Search From fd1025e3ec42ee727561b9ae3b86bb2b99f8ef28 Mon Sep 17 00:00:00 2001 From: Kaustubh Badrike Date: Tue, 6 Oct 2020 00:13:44 +0530 Subject: [PATCH 22/33] Fix methodname and parameter type in BinaryToDecimal.js (#392) * Update BinaryToDecimal.js Fix typo in name * Update BinaryToDecimal.js Co-authored-by: vinayak --- Conversions/BinaryToDecimal.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Conversions/BinaryToDecimal.js b/Conversions/BinaryToDecimal.js index 907468470..4a391cf84 100644 --- a/Conversions/BinaryToDecimal.js +++ b/Conversions/BinaryToDecimal.js @@ -1,11 +1,14 @@ -function binaryToDeicmal (binaryNumber) { +const binaryToDecimal = (binaryString) => { let decimalNumber = 0 - const binaryDigits = binaryNumber.split('').reverse() // Splits the binary number into reversed single digits + const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits binaryDigits.forEach((binaryDigit, index) => { decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits }) - console.log(`Decimal of ${binaryNumber} is ${decimalNumber}`) + console.log(`Decimal of ${binaryString} is ${decimalNumber}`) + return decimalNumber } -binaryToDeicmal('111001') -binaryToDeicmal('101') +(() => { + binaryToDecimal('111001') + binaryToDecimal('101') +})() From c419ff5a1f0de2349e1c6878ee8af447058e19d1 Mon Sep 17 00:00:00 2001 From: vinayak Date: Tue, 6 Oct 2020 00:23:53 +0530 Subject: [PATCH 23/33] npx fixx (#421) --- Recursive/FibonacciNumberRecursive.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Recursive/FibonacciNumberRecursive.js b/Recursive/FibonacciNumberRecursive.js index 406c86684..e5c5bb95b 100644 --- a/Recursive/FibonacciNumberRecursive.js +++ b/Recursive/FibonacciNumberRecursive.js @@ -6,7 +6,6 @@ const fibonacci = (N) => { return fibonacci(N - 2) + fibonacci(N - 1) } - // testing (() => { const number = 5 From bd70fd2ada9cf3c27864f40de1952df9c62e47cd Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 17:03:56 -0300 Subject: [PATCH 24/33] Added new algoritm --- Linear-Algebra/test/test.js | 2 +- String/CheckWordOccurrence.js | 25 +++++++++++++++++++++++ String/CheckWordOcurrence.test.js | 34 +++++++++++++++++++++++++++++++ package.json | 4 +++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 String/CheckWordOccurrence.js create mode 100644 String/CheckWordOcurrence.test.js diff --git a/Linear-Algebra/test/test.js b/Linear-Algebra/test/test.js index 5f1892db8..18cd00500 100644 --- a/Linear-Algebra/test/test.js +++ b/Linear-Algebra/test/test.js @@ -11,7 +11,7 @@ var assert = require('assert') var fs = require('fs') // file is included here -eval(fs.readFileSync('src/la_lib.js') + '') +eval(fs.readFileSync('../src/la_lib.js') + '') // Tests goes here // creating some vectors diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js new file mode 100644 index 000000000..d17b2a0cf --- /dev/null +++ b/String/CheckWordOccurrence.js @@ -0,0 +1,25 @@ +/** + * Check and count occurrence of each word in a string + * Inputs a String eg. Madonna and Boolean + */ + +const checkWordOccurrence = (str, isCaseSensitive = false) => { + if (typeof str != 'string') { + throw new TypeError('The first param should be a string'); + } + if (typeof isCaseSensitive != 'boolean') { + throw new TypeError('The second param should be a boolean') + } + + let result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if(/\s/.test(word)) continue; + result[word] = (!result[word]) ? 1 : result[word] + 1 + } + + } + return result; +} +export { checkWordOccurrence } \ No newline at end of file diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js new file mode 100644 index 000000000..0e57ad655 --- /dev/null +++ b/String/CheckWordOcurrence.test.js @@ -0,0 +1,34 @@ +import {checkWordOccurrence} from './CheckWordOccurrence'; +describe('checkWordOccurrence', () => { + it('expects throw on insert wrong string', () => { + const value = 123; + expect(() => checkWordOccurrence(value)).toThrow(); + }); + it('expect throw on insert wrong param for case sensitive', () => { + const value = 'hello'; + expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case sensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, true); + const expectResult = {A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1}; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); + }); + }); + it('check occurrence with case insensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, false); + const expectResult = {A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1}; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); + }); + + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index eea349d1b..450a58349 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ "jest" ] + "env": [ + "jest" + ] }, "devDependencies": { "babel-jest": "^26.3.0", From 5ba5a603bb4fdde6294d4153c706124350769827 Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 17:06:45 -0300 Subject: [PATCH 25/33] remove wrong edited file package.json --- package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index 450a58349..eea349d1b 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,7 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ - "jest" - ] + "env": [ "jest" ] }, "devDependencies": { "babel-jest": "^26.3.0", From dd40c139d5e048a03b7f3a0b28bb1185bf02f913 Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 23:44:20 -0300 Subject: [PATCH 26/33] remove wrong edited file --- Linear-Algebra/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Linear-Algebra/test/test.js b/Linear-Algebra/test/test.js index 18cd00500..5f1892db8 100644 --- a/Linear-Algebra/test/test.js +++ b/Linear-Algebra/test/test.js @@ -11,7 +11,7 @@ var assert = require('assert') var fs = require('fs') // file is included here -eval(fs.readFileSync('../src/la_lib.js') + '') +eval(fs.readFileSync('src/la_lib.js') + '') // Tests goes here // creating some vectors From fbd3442cf0c5cc69f259b99cea4397721fdf6d2e Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Mon, 5 Oct 2020 23:52:21 -0300 Subject: [PATCH 27/33] fix ident file --- String/CheckWordOccurrence.js | 30 ++++++++-------- String/CheckWordOcurrence.test.js | 58 +++++++++++++++---------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js index d17b2a0cf..f31df400b 100644 --- a/String/CheckWordOccurrence.js +++ b/String/CheckWordOccurrence.js @@ -4,22 +4,22 @@ */ const checkWordOccurrence = (str, isCaseSensitive = false) => { - if (typeof str != 'string') { - throw new TypeError('The first param should be a string'); - } - if (typeof isCaseSensitive != 'boolean') { - throw new TypeError('The second param should be a boolean') - } + if (typeof str != 'string') { + throw new TypeError('The first param should be a string'); + } + if (typeof isCaseSensitive != 'boolean') { + throw new TypeError('The second param should be a boolean') + } - let result = {} - if (str.length > 0) { - for (let i = 0; i < str.length; i++) { - const word = isCaseSensitive ? str[i] : str[i].toUpperCase() - if(/\s/.test(word)) continue; - result[word] = (!result[word]) ? 1 : result[word] + 1 - } + let result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if (/\s/.test(word)) continue; + result[word] = (!result[word]) ? 1 : result[word] + 1 + } - } - return result; + } + return result; } export { checkWordOccurrence } \ No newline at end of file diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js index 0e57ad655..cd5673885 100644 --- a/String/CheckWordOcurrence.test.js +++ b/String/CheckWordOcurrence.test.js @@ -1,34 +1,34 @@ -import {checkWordOccurrence} from './CheckWordOccurrence'; +import { checkWordOccurrence } from './CheckWordOccurrence'; describe('checkWordOccurrence', () => { - it('expects throw on insert wrong string', () => { - const value = 123; - expect(() => checkWordOccurrence(value)).toThrow(); + it('expects throw on insert wrong string', () => { + const value = 123; + expect(() => checkWordOccurrence(value)).toThrow(); + }); + it('expect throw on insert wrong param for case sensitive', () => { + const value = 'hello'; + expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case sensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, true); + const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 }; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); }); - it('expect throw on insert wrong param for case sensitive', () => { - const value = 'hello'; - expect(() => checkWordOccurrence(value, value)).toThrow(); + }); + it('check occurrence with case insensitive', () => { + const stringToTest = "A Mad World"; + const charsOccurrences = checkWordOccurrence(stringToTest, false); + const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 }; + const occurrencesObjectKeys = Object.keys(charsOccurrences); + const expectObjectKeys = Object.keys(expectResult); + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + expectObjectKeys.forEach(key => { + expect(expectResult[key]).toBe(charsOccurrences[key]); }); - it('check occurrence with case sensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, true); - const expectResult = {A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1}; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); - expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - }); - it('check occurrence with case insensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, false); - const expectResult = {A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1}; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); - expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - }); + }); }); \ No newline at end of file From 86ed0d882949c9ed0144dd53b71c85daf03b4a2c Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Tue, 6 Oct 2020 00:08:52 -0300 Subject: [PATCH 28/33] fix code style using standardjs --- String/CheckWordOccurrence.js | 38 +++++++++++----------- String/CheckWordOcurrence.test.js | 53 +++++++++++++++---------------- package-lock.json | 7 ++-- package.json | 4 ++- 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/String/CheckWordOccurrence.js b/String/CheckWordOccurrence.js index f31df400b..b86150c0b 100644 --- a/String/CheckWordOccurrence.js +++ b/String/CheckWordOccurrence.js @@ -1,25 +1,25 @@ /** - * Check and count occurrence of each word in a string - * Inputs a String eg. Madonna and Boolean - */ + * Check and count occurrence of each word in a string + * Inputs a String eg. Madonna and Boolean + **/ const checkWordOccurrence = (str, isCaseSensitive = false) => { - if (typeof str != 'string') { - throw new TypeError('The first param should be a string'); - } - if (typeof isCaseSensitive != 'boolean') { - throw new TypeError('The second param should be a boolean') - } + if (typeof str !== 'string') { + throw new TypeError('The first param should be a string') + } + if (typeof isCaseSensitive !== 'boolean') { + throw new TypeError('The second param should be a boolean') + } - let result = {} - if (str.length > 0) { - for (let i = 0; i < str.length; i++) { - const word = isCaseSensitive ? str[i] : str[i].toUpperCase() - if (/\s/.test(word)) continue; - result[word] = (!result[word]) ? 1 : result[word] + 1 - } + const result = {} + if (str.length > 0) { + for (let i = 0; i < str.length; i++) { + const word = isCaseSensitive ? str[i] : str[i].toUpperCase() + if (/\s/.test(word)) continue + result[word] = (!result[word]) ? 1 : result[word] + 1 + } + } - } - return result; + return result } -export { checkWordOccurrence } \ No newline at end of file +export { checkWordOccurrence } diff --git a/String/CheckWordOcurrence.test.js b/String/CheckWordOcurrence.test.js index cd5673885..2b1a88782 100644 --- a/String/CheckWordOcurrence.test.js +++ b/String/CheckWordOcurrence.test.js @@ -1,34 +1,33 @@ -import { checkWordOccurrence } from './CheckWordOccurrence'; +import { checkWordOccurrence } from './CheckWordOccurrence' describe('checkWordOccurrence', () => { it('expects throw on insert wrong string', () => { - const value = 123; - expect(() => checkWordOccurrence(value)).toThrow(); - }); + const value = 123 + expect(() => checkWordOccurrence(value)).toThrow() + }) it('expect throw on insert wrong param for case sensitive', () => { - const value = 'hello'; - expect(() => checkWordOccurrence(value, value)).toThrow(); - }); + const value = 'hello' + expect(() => checkWordOccurrence(value, value)).toThrow() + }) it('check occurrence with case sensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, true); - const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 }; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + const stringToTest = 'A Mad World' + const charsOccurrences = checkWordOccurrence(stringToTest, true) + const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 } + const occurrencesObjectKeys = Object.keys(charsOccurrences) + const expectObjectKeys = Object.keys(expectResult) + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length) expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - }); + expect(expectResult[key]).toBe(charsOccurrences[key]) + }) + }) it('check occurrence with case insensitive', () => { - const stringToTest = "A Mad World"; - const charsOccurrences = checkWordOccurrence(stringToTest, false); - const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 }; - const occurrencesObjectKeys = Object.keys(charsOccurrences); - const expectObjectKeys = Object.keys(expectResult); - expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length); + const stringToTest = 'A Mad World' + const charsOccurrences = checkWordOccurrence(stringToTest, false) + const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 } + const occurrencesObjectKeys = Object.keys(charsOccurrences) + const expectObjectKeys = Object.keys(expectResult) + expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length) expectObjectKeys.forEach(key => { - expect(expectResult[key]).toBe(charsOccurrences[key]); - }); - - }); -}); \ No newline at end of file + expect(expectResult[key]).toBe(charsOccurrences[key]) + }) + }) +}) diff --git a/package-lock.json b/package-lock.json index 9bcf2855d..20ddcf663 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3734,12 +3734,11 @@ }, "dependencies": { "ansi-styles": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", - "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", "dev": true, "requires": { - "@types/color-name": "^1.1.1", "color-convert": "^2.0.1" } }, diff --git a/package.json b/package.json index eea349d1b..450a58349 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ "jest" ] + "env": [ + "jest" + ] }, "devDependencies": { "babel-jest": "^26.3.0", From 41f4e2cbd443d53e65dbfe378260bc7b639f7a2d Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 6 Oct 2020 05:38:44 +0000 Subject: [PATCH 29/33] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9fd2d090c..10b347519 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -145,6 +145,8 @@ * [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.js) * [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.test.js) * [CheckRearrangePalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckRearrangePalindrome.js) + * [CheckWordOccurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOccurrence.js) + * [CheckWordOcurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOcurrence.test.js) * [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.js) * [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.test.js) * [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js) From 3f755cfbdaee9a4a4c6591d63c2f66edd1fc9f09 Mon Sep 17 00:00:00 2001 From: Anup Kumar Panwar <1anuppanwar@gmail.com> Date: Tue, 6 Oct 2020 11:11:31 +0530 Subject: [PATCH 30/33] Update ZeroOneKnapsack.js --- Dynamic-Programming/ZeroOneKnapsack.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js index e418db1ae..0274fdefd 100644 --- a/Dynamic-Programming/ZeroOneKnapsack.js +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -3,7 +3,7 @@ * https://en.wikipedia.org/wiki/Knapsack_problem */ -function zeroOneKnapsack (arr, n, cap, cache) { +const zeroOneKnapsack = (arr, n, cap, cache) => { if (cap === 0 || n === 0) { cache[n][cap] = 0 return cache[n][cap] @@ -20,7 +20,7 @@ function zeroOneKnapsack (arr, n, cap, cache) { } } -function main () { +const main = () => { /* Problem Statement: You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity. From 3974bfb1ecbcffe7189651b313e762f8bd1bdc3f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 6 Oct 2020 05:53:41 +0000 Subject: [PATCH 31/33] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 10b347519..52b6ad823 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -34,6 +34,7 @@ * [MaxHeap](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MaxHeap.js) * [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MinPriorityQueue.js) * Linked-List + * [CycleDetection](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleDetection.js) * [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/DoublyLinkedList.js) * [SinglyLinkList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SinglyLinkList.js) * Queue From b6710baccc63b1a4f6e635893a73f7f4d1d72906 Mon Sep 17 00:00:00 2001 From: parhan <57286339+apriliandi246@users.noreply.github.com> Date: Tue, 6 Oct 2020 13:49:50 +0700 Subject: [PATCH 32/33] add new data structures (Single Circular Linked List) (#400) * add new data structures (Single Circular Linked List) * fix standard * fix standard js * fix code standard --- .../SingleCircularLinkedList.js.js | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Data-Structures/Linked-List/SingleCircularLinkedList.js.js diff --git a/Data-Structures/Linked-List/SingleCircularLinkedList.js.js b/Data-Structures/Linked-List/SingleCircularLinkedList.js.js new file mode 100644 index 000000000..fd50253ad --- /dev/null +++ b/Data-Structures/Linked-List/SingleCircularLinkedList.js.js @@ -0,0 +1,97 @@ +class Node { + constructor (data, next = null) { + this.data = data + this.next = next + } +} + +class SinglyCircularLinkedList { + constructor () { + this.head = null + this.size = 0 + } + + insert (data) { + const node = new Node(data) + + if (!this.head) { + node.next = node + this.head = node + this.size++ + } else { + node.next = this.head + + let current = this.head + + while (current.next.data !== this.head.data) { + current = current.next + } + + current.next = node + this.size++ + } + } + + insertAt (index, data) { + const node = new Node(data) + + if (index < 0 || index > this.size) return + + if (index === 0) { + this.head = node + this.size = 1 + return + } + + let previous + let count = 0 + let current = this.head + + while (count < index) { + previous = current + current = current.next + count++ + } + + node.next = current + previous.next = node + this.size++ + } + + remove () { + if (!this.head) return + + let prev + let current = this.head + + while (current.next !== this.head) { + prev = current + current = current.next + } + + prev.next = this.head + this.size-- + } + + printData () { + let count = 0 + let current = this.head + + while (current !== null && count !== this.size) { + console.log(current.data + '\n') + current = current.next + count++ + } + } +} + +const ll = new SinglyCircularLinkedList() + +ll.insert(10) +ll.insert(20) +ll.insert(30) +ll.insert(40) +ll.insert(50) +ll.insertAt(5, 60) +ll.remove(5) +ll.printData() From e92b5d2a49c3dcab076a1e0e8660cb9e0dbccc10 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 6 Oct 2020 06:50:07 +0000 Subject: [PATCH 33/33] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 52b6ad823..475f2bac3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -36,6 +36,7 @@ * Linked-List * [CycleDetection](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleDetection.js) * [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/DoublyLinkedList.js) + * [SingleCircularLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SingleCircularLinkedList.js.js) * [SinglyLinkList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SinglyLinkList.js) * Queue * [Queue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Queue/Queue.js)