Skip to content

Commit 7dfcfef

Browse files
Merge pull request #1 from TheAlgorithms/master
update my fork
2 parents 0dfb200 + e92b5d2 commit 7dfcfef

File tree

13 files changed

+315
-23
lines changed

13 files changed

+315
-23
lines changed

Conversions/BinaryToDecimal.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
function binaryToDeicmal (binaryNumber) {
1+
const binaryToDecimal = (binaryString) => {
22
let decimalNumber = 0
3-
const binaryDigits = binaryNumber.split('').reverse() // Splits the binary number into reversed single digits
3+
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
44
binaryDigits.forEach((binaryDigit, index) => {
55
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
66
})
7-
console.log(`Decimal of ${binaryNumber} is ${decimalNumber}`)
7+
console.log(`Decimal of ${binaryString} is ${decimalNumber}`)
8+
return decimalNumber
89
}
910

10-
binaryToDeicmal('111001')
11-
binaryToDeicmal('101')
11+
(() => {
12+
binaryToDecimal('111001')
13+
binaryToDecimal('101')
14+
})()

DIRECTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
* [MaxHeap](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MaxHeap.js)
3535
* [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MinPriorityQueue.js)
3636
* Linked-List
37+
* [CycleDetection](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleDetection.js)
3738
* [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/DoublyLinkedList.js)
39+
* [SingleCircularLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SingleCircularLinkedList.js.js)
3840
* [SinglyLinkList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SinglyLinkList.js)
3941
* Queue
4042
* [Queue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Queue/Queue.js)
@@ -50,6 +52,7 @@
5052
* [ClimbingStairs](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ClimbingStairs.js)
5153
* [CoinChange](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/CoinChange.js)
5254
* [EditDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/EditDistance.js)
55+
* [FibonacciNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/FibonacciNumber.js)
5356
* [KadaneAlgo](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/KadaneAlgo.js)
5457
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LevenshteinDistance.js)
5558
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestCommonSubsequence.js)
@@ -87,6 +90,7 @@
8790
* [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js)
8891
* [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js)
8992
* [GridGet](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGet.js)
93+
* [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js)
9094
* [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js)
9195
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js)
9296
* [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js)
@@ -101,6 +105,7 @@
101105

102106
## Recursive
103107
* [EucledianGCD](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/EucledianGCD.js)
108+
* [FibonacciNumberRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/FibonacciNumberRecursive.js)
104109
* [TowerOfHanoi](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/TowerOfHanoi.js)
105110

106111
## Search
@@ -130,6 +135,7 @@
130135
* [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js)
131136
* [RadixSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/RadixSort.js)
132137
* [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.js)
138+
* [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.test.js)
133139
* [ShellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/ShellSort.js)
134140
* [TimSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TimSort.js)
135141
* [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js)
@@ -141,6 +147,8 @@
141147
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.js)
142148
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.test.js)
143149
* [CheckRearrangePalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckRearrangePalindrome.js)
150+
* [CheckWordOccurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOccurrence.js)
151+
* [CheckWordOcurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOcurrence.test.js)
144152
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.js)
145153
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.test.js)
146154
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* A LinkedList based solution for Detect a Cycle in a list
3+
* https://en.wikipedia.org/wiki/Cycle_detection
4+
*/
5+
6+
function main () {
7+
/*
8+
Problem Statement:
9+
Given head, the head of a linked list, determine if the linked list has a cycle in it.
10+
11+
Note:
12+
* While Solving the problem in given link below, don't use main() function.
13+
* Just use only the code inside main() function.
14+
* The purpose of using main() function here is to aviod global variables.
15+
16+
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
17+
*/
18+
const head = '' // Reference to head is given in the problem. So please ignore this line
19+
let fast = head
20+
let slow = head
21+
22+
while (fast != null && fast.next != null && slow != null) {
23+
fast = fast.next.next
24+
slow = slow.next
25+
if (fast === slow) {
26+
return true
27+
}
28+
}
29+
return false
30+
}
31+
32+
main()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class Node {
2+
constructor (data, next = null) {
3+
this.data = data
4+
this.next = next
5+
}
6+
}
7+
8+
class SinglyCircularLinkedList {
9+
constructor () {
10+
this.head = null
11+
this.size = 0
12+
}
13+
14+
insert (data) {
15+
const node = new Node(data)
16+
17+
if (!this.head) {
18+
node.next = node
19+
this.head = node
20+
this.size++
21+
} else {
22+
node.next = this.head
23+
24+
let current = this.head
25+
26+
while (current.next.data !== this.head.data) {
27+
current = current.next
28+
}
29+
30+
current.next = node
31+
this.size++
32+
}
33+
}
34+
35+
insertAt (index, data) {
36+
const node = new Node(data)
37+
38+
if (index < 0 || index > this.size) return
39+
40+
if (index === 0) {
41+
this.head = node
42+
this.size = 1
43+
return
44+
}
45+
46+
let previous
47+
let count = 0
48+
let current = this.head
49+
50+
while (count < index) {
51+
previous = current
52+
current = current.next
53+
count++
54+
}
55+
56+
node.next = current
57+
previous.next = node
58+
this.size++
59+
}
60+
61+
remove () {
62+
if (!this.head) return
63+
64+
let prev
65+
let current = this.head
66+
67+
while (current.next !== this.head) {
68+
prev = current
69+
current = current.next
70+
}
71+
72+
prev.next = this.head
73+
this.size--
74+
}
75+
76+
printData () {
77+
let count = 0
78+
let current = this.head
79+
80+
while (current !== null && count !== this.size) {
81+
console.log(current.data + '\n')
82+
current = current.next
83+
count++
84+
}
85+
}
86+
}
87+
88+
const ll = new SinglyCircularLinkedList()
89+
90+
ll.insert(10)
91+
ll.insert(20)
92+
ll.insert(30)
93+
ll.insert(40)
94+
ll.insert(50)
95+
ll.insertAt(5, 60)
96+
ll.remove(5)
97+
ll.printData()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://en.wikipedia.org/wiki/Fibonacci_number
2+
3+
const fibonacci = (N) => {
4+
// creating array to store values
5+
const memo = new Array(N + 1)
6+
memo[0] = 0
7+
memo[1] = 1
8+
for (let i = 2; i <= N; i++) {
9+
memo[i] = memo[i - 1] + memo[i - 2]
10+
}
11+
return memo[N]
12+
}
13+
14+
// testing
15+
(() => {
16+
const number = 5
17+
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
18+
})()

Maths/MeanSquareError.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Wikipedia: https://en.wikipedia.org/wiki/Mean_squared_error
2+
3+
const meanSquaredError = (predicted, expected) => {
4+
if (!Array.isArray(predicted) || !Array.isArray(expected)) {
5+
throw new TypeError('Argument must be an Array')
6+
}
7+
8+
if (predicted.length !== expected.length) {
9+
throw new TypeError('The two lists must be of equal length')
10+
}
11+
12+
let err = 0
13+
14+
for (let i = 0; i < expected.length; i++) {
15+
err += (expected[i] - predicted[i]) ** 2
16+
}
17+
18+
return err / expected.length
19+
}
20+
21+
// testing
22+
(() => {
23+
console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0)
24+
console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5)
25+
console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3)
26+
})()

Recursive/FibonacciNumberRecursive.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://en.wikipedia.org/wiki/Fibonacci_number
2+
3+
const fibonacci = (N) => {
4+
if (N === 0 || N === 1) return N
5+
6+
return fibonacci(N - 2) + fibonacci(N - 1)
7+
}
8+
9+
// testing
10+
(() => {
11+
const number = 5
12+
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
13+
})()

Sorts/SelectionSort.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,43 @@
88
*from the unsorted subarray is picked and moved to the sorted subarray.
99
*/
1010

11-
function selectionSort (items) {
12-
var length = items.length
13-
for (var i = 0; i < length - 1; i++) {
11+
const selectionSort = (list) => {
12+
if (!Array.isArray(list)) {
13+
throw new TypeError('Given input is not an array')
14+
}
15+
const items = [...list] // We don't want to modify the original array
16+
const length = items.length
17+
for (let i = 0; i < length - 1; i++) {
18+
if (typeof items[i] !== 'number') {
19+
throw new TypeError('One of the items in your array is not a number')
20+
}
1421
// Number of passes
15-
var min = i // min holds the current minimum number position for each pass; i holds the Initial min number
16-
for (var j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array
22+
let min = i // min holds the current minimum number position for each pass; i holds the Initial min number
23+
for (let j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array
1724
if (items[j] < items[min]) { // Compare the numbers
1825
min = j // Change the current min number position if a smaller num is found
1926
}
2027
}
2128
if (min !== i) {
2229
// After each pass, if the current min num != initial min num, exchange the position.
2330
// Swap the numbers
24-
[items[i], items[min]] = [items[min], [items[i]]]
31+
[items[i], items[min]] = [items[min], items[i]]
2532
}
2633
}
34+
return items
2735
}
2836

29-
// Implementation of Selection Sort
37+
/* Implementation of Selection Sort
38+
39+
(() => {
40+
let array = [5, 6, 7, 8, 1, 2, 12, 14]
41+
// Array before Sort
42+
console.log(array)
43+
array = selectionSort(array)
44+
// Array after sort
45+
console.log(array)
46+
})()
47+
48+
*/
3049

31-
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
32-
// Array before Sort
33-
console.log(ar)
34-
selectionSort(ar)
35-
// Array after sort
36-
console.log(ar)
50+
export { selectionSort }

Sorts/SelectionSort.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { selectionSort } from './SelectionSort'
2+
3+
describe('selectionSort', () => {
4+
it('expects to return the array sorted in ascending order', () => {
5+
var toSort = [5, 6, 7, 8, 1, 2, 12, 14]
6+
const expected = [1, 2, 5, 6, 7, 8, 12, 14]
7+
8+
expect(selectionSort(toSort)).toEqual(expected)
9+
})
10+
11+
it('expects to throw if it is not a valid array', () => {
12+
expect(() => selectionSort('abc')).toThrow('Given input is not an array')
13+
expect(() => selectionSort(123)).toThrow('Given input is not an array')
14+
expect(() => selectionSort({})).toThrow('Given input is not an array')
15+
expect(() => selectionSort(null)).toThrow('Given input is not an array')
16+
expect(() => selectionSort()).toThrow('Given input is not an array')
17+
})
18+
19+
it('expects to throw if one of the elements in the array is not a number', () => {
20+
expect(() => selectionSort([1, 'x', 2])).toThrow('One of the items in your array is not a number')
21+
})
22+
})

String/CheckWordOccurrence.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Check and count occurrence of each word in a string
3+
* Inputs a String eg. Madonna and Boolean
4+
**/
5+
6+
const checkWordOccurrence = (str, isCaseSensitive = false) => {
7+
if (typeof str !== 'string') {
8+
throw new TypeError('The first param should be a string')
9+
}
10+
if (typeof isCaseSensitive !== 'boolean') {
11+
throw new TypeError('The second param should be a boolean')
12+
}
13+
14+
const result = {}
15+
if (str.length > 0) {
16+
for (let i = 0; i < str.length; i++) {
17+
const word = isCaseSensitive ? str[i] : str[i].toUpperCase()
18+
if (/\s/.test(word)) continue
19+
result[word] = (!result[word]) ? 1 : result[word] + 1
20+
}
21+
}
22+
23+
return result
24+
}
25+
export { checkWordOccurrence }

0 commit comments

Comments
 (0)
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