Skip to content

Commit e9e9585

Browse files
committed
Merge branch 'TheAlgorithms:master' into fix-maths-is-divisible
2 parents cf5ff92 + abb7bb4 commit e9e9585

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+883
-213
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ jobs:
1515
- name: npm install, build, and test
1616
run: |
1717
npm install doctest standard --save-dev
18-
npx doctest **/*.js || true # TODO: error: Line 1: Unexpected token >>
18+
npx doctest **/*.js || true # TODO: Add all doctests
1919
npx standard
2020
npm ci
2121
npm run build --if-present
22-
# TODO: Remove the next line when #539 is fixed.
23-
rm Linear-Algebra/test/test.js String/LevenshteinDistance.test.js
2422
npm test
2523
env:
2624
CI: true

Ciphers/KeywordShiftedAlphabet.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Keyword shifted alphabet is a simple cipher using a translation table created with a help of a keyword.
3+
* Keyword must be a word where each character can occur only once.
4+
* To create the translation table, we write all the alphabet characters to the first.
5+
* Second row start with the keyword, then we continue with the rest of the characters that are missing in alphabetical order.
6+
*
7+
* |A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|
8+
* |K|E|Y|W|O|R|D|A|B|C|F|G|H|I|J|L|M|N|P|Q|S|T|U|V|W|Z|
9+
*
10+
* Encryption is then just a matter of writing the matching (same index) letter from the second row instead of the first row:
11+
* 'Hello world' -> 'Aoggj ujngw'
12+
*
13+
* Decryption is then just the reverse process of writing the matching (same index) letter from the first row instead of the second row
14+
* 'Aogg ujngw' -> 'Hello world'
15+
*
16+
* Non alphabetical characters (space, exclamation mark, ...) are kept as they are
17+
*/
18+
19+
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
20+
21+
function checkKeywordValidity (keyword) {
22+
keyword.split('').forEach((char, index) => {
23+
const rest = keyword.slice(0, index) + keyword.slice(index + 1)
24+
if (rest.indexOf(char) !== -1) {
25+
return false
26+
}
27+
})
28+
return true
29+
}
30+
31+
function getEncryptedAlphabet (keyword) {
32+
const encryptedAlphabet = keyword.split('')
33+
alphabet.forEach((char) => {
34+
if (encryptedAlphabet.indexOf(char) === -1) {
35+
encryptedAlphabet.push(char)
36+
}
37+
})
38+
return encryptedAlphabet
39+
}
40+
41+
function translate (sourceAlphabet, targetAlphabet, message) {
42+
return message.split('').reduce((encryptedMessage, char) => {
43+
const isUpperCase = char === char.toUpperCase()
44+
const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase())
45+
const encryptedChar = encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char
46+
encryptedMessage += isUpperCase ? encryptedChar.toUpperCase() : encryptedChar
47+
return encryptedMessage
48+
}, '')
49+
}
50+
51+
function checkInputs (keyword, message) {
52+
if (!keyword || !message) {
53+
throw new Error('Both keyword and message must be specified')
54+
}
55+
56+
if (!checkKeywordValidity(keyword)) {
57+
throw new Error('Invalid keyword!')
58+
}
59+
}
60+
61+
function encrypt (keyword, message) {
62+
checkInputs(keyword, message)
63+
return translate(alphabet, getEncryptedAlphabet(keyword.toLowerCase()), message)
64+
}
65+
66+
function decrypt (keyword, message) {
67+
checkInputs(keyword, message)
68+
return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message)
69+
}
70+
71+
console.log(encrypt('keyword', 'Hello world!')) // Prints 'Aoggj ujngw!'
72+
console.log(decrypt('keyword', 'Aoggj ujngw!')) // Prints 'Hello world!

Conversions/DecimalToBinary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function decimalToBinary (num) {
2-
var bin = []
2+
const bin = []
33
while (num > 0) {
44
bin.unshift(num % 2)
55
num >>= 1 // basically /= 2 without remainder if any

Conversions/HexToRGB.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function hexStringToRGB (hexString) {
2-
var r = hexString.substring(0, 2)
3-
var g = hexString.substring(2, 4)
4-
var b = hexString.substring(4, 6)
2+
let r = hexString.substring(0, 2)
3+
let g = hexString.substring(2, 4)
4+
let b = hexString.substring(4, 6)
55

66
r = parseInt(r, 16)
77
g = parseInt(g, 16)
88
b = parseInt(b, 16)
9-
var obj = { r, g, b }
9+
const obj = { r, g, b }
1010

1111
return obj
1212
}

Conversions/RomanToDecimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var values = {
1+
const values = {
22
I: 1,
33
V: 5,
44
X: 10,

DIRECTORY.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
## Ciphers
1717
* [CaesarsCipher](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/CaesarsCipher.js)
1818
* [KeyFinder](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/KeyFinder.js)
19+
* [KeywordShiftedAlphabet](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/KeywordShiftedAlphabet.js)
1920
* [ROT13](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/ROT13.js)
2021
* [VigenereCipher](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/VigenereCipher.js)
2122
* [XORCipher](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/XORCipher.js)
@@ -47,6 +48,7 @@
4748
* [SingleCircularLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SingleCircularLinkedList.js.js)
4849
* [SinglyLinkList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SinglyLinkList.js)
4950
* Queue
51+
* [CircularQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Queue/CircularQueue.js)
5052
* [Queue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Queue/Queue.js)
5153
* [QueueUsing2Stacks](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Queue/QueueUsing2Stacks.js)
5254
* Stack
@@ -74,11 +76,15 @@
7476
* [MaxNonAdjacentSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/MaxNonAdjacentSum.js)
7577
* [MinimumCostPath](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/MinimumCostPath.js)
7678
* [NumberOfSubsetEqualToGivenSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js)
79+
* [Shuf](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/Shuf.js)
7780
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/SieveOfEratosthenes.js)
7881
* [SudokuSolver](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/SudokuSolver.js)
7982
* [TrappingRainWater](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/TrappingRainWater.js)
8083
* [ZeroOneKnapsack](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ZeroOneKnapsack.js)
8184

85+
## Geometry
86+
* [ConvexHullGraham](https://github.com/TheAlgorithms/Javascript/blob/master/Geometry/ConvexHullGraham.js)
87+
8288
## Graphs
8389
* [BreadthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/BreadthFirstSearch.js)
8490
* [BreadthFirstShortestPath](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/BreadthFirstShortestPath.js)
@@ -88,6 +94,7 @@
8894
* [DepthFirstSearchRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/DepthFirstSearchRecursive.js)
8995
* [Dijkstra](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/Dijkstra.js)
9096
* [DijkstraSmallestPath](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/DijkstraSmallestPath.js)
97+
* [FloydWarshall](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/FloydWarshall.js)
9198
* [KruskalMST](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/KruskalMST.js)
9299
* [NodeNeighbors](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/NodeNeighbors.js)
93100
* [NumberOfIslands](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/NumberOfIslands.js)
@@ -113,6 +120,7 @@
113120
* [decimalIsolate](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/decimalIsolate.js)
114121
* [DigitSum](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/DigitSum.js)
115122
* [EulerMethod](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/EulerMethod.js)
123+
* [EulersTotient](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/EulersTotient.js)
116124
* [EulersTotientFunction](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/EulersTotientFunction.js)
117125
* [Factorial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Factorial.js)
118126
* [Factors](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Factors.js)
@@ -123,6 +131,7 @@
123131
* [isDivisible](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isDivisible.js)
124132
* [isOdd](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isOdd.js)
125133
* [Mandelbrot](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Mandelbrot.js)
134+
* [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js)
126135
* [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js)
127136
* [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js)
128137
* [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js)
@@ -137,9 +146,11 @@
137146
* [Polynomial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Polynomial.js)
138147
* [Pow](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Pow.js)
139148
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js)
149+
* [PrimeFactors](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeFactors.js)
140150
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js)
141151
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js)
142152
* [Softmax](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Softmax.js)
153+
* [SquareRoot](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SquareRoot.js)
143154
* test
144155
* [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Abs.test.js)
145156
* [Area](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Area.test.js)
@@ -181,13 +192,16 @@
181192
* [Problem014](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem014.js)
182193
* [Problem020](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem020.js)
183194
* [Problem1](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem1.js)
195+
* [Problem10](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem10.js)
184196
* [Problem2](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem2.js)
185197
* [Problem3](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem3.js)
186198
* [Problem4](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem4.js)
187199
* [Problem5](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem5.js)
188200
* [Problem6](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem6.js)
189201
* [Problem7](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem7.js)
190202
* [Problem9](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem9.js)
203+
* test
204+
* [Problem10](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/test/Problem10.test.js)
191205

192206
## Recursive
193207
* [BinarySearch](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/BinarySearch.js)
@@ -244,7 +258,7 @@
244258
* [CheckVowels](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckVowels.test.js)
245259
* [CheckWordOccurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOccurrence.js)
246260
* [CheckWordOcurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOcurrence.test.js)
247-
* [createPurmutations](https://github.com/TheAlgorithms/Javascript/blob/master/String/createPurmutations.js)
261+
* [CreatePermutations](https://github.com/TheAlgorithms/Javascript/blob/master/String/CreatePermutations.js)
248262
* [FormatPhoneNumber](https://github.com/TheAlgorithms/Javascript/blob/master/String/FormatPhoneNumber.js)
249263
* [FormatPhoneNumber](https://github.com/TheAlgorithms/Javascript/blob/master/String/FormatPhoneNumber.test.js)
250264
* [GenerateGUID](https://github.com/TheAlgorithms/Javascript/blob/master/String/GenerateGUID.js)
@@ -262,6 +276,7 @@
262276
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CheckAnagram.test.js)
263277
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CheckPalindrome.test.js)
264278
* [CheckPangram](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CheckPangram.test.js)
279+
* [CreatePermutations](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CreatePermutations.test.js)
265280
* [KMPPatternSearching](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/KMPPatternSearching.test.js)
266281
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/PatternMatching.test.js)
267282
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/ReverseString.test.js)

Data-Structures/Linked-List/SinglyLinkList.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// class LinkedList and constructor
1212
// Creates a LinkedList
13-
var LinkedList = (function () {
13+
const LinkedList = (function () {
1414
function LinkedList () {
1515
// Length of linklist and head is null at start
1616
this.length = 0
@@ -19,7 +19,7 @@ var LinkedList = (function () {
1919

2020
// class node (constructor)
2121
// Creating Node with element's value
22-
var Node = (function () {
22+
const Node = (function () {
2323
function Node (element) {
2424
this.element = element
2525
this.next = null
@@ -39,12 +39,12 @@ var LinkedList = (function () {
3939

4040
// Creates a node and adds it to linklist
4141
LinkedList.prototype.add = function (element) {
42-
var node = new Node(element)
42+
const node = new Node(element)
4343
// Check if its the first element
4444
if (this.head === null) {
4545
this.head = node
4646
} else {
47-
var currentNode = this.head
47+
let currentNode = this.head
4848

4949
// Loop till there is node present in the list
5050
while (currentNode.next) {
@@ -60,8 +60,8 @@ var LinkedList = (function () {
6060

6161
// Removes the node with the value as param
6262
LinkedList.prototype.remove = function (element) {
63-
var currentNode = this.head
64-
var previousNode
63+
let currentNode = this.head
64+
let previousNode
6565

6666
// Check if the head node is the element to remove
6767
if (currentNode.element === element) {
@@ -88,8 +88,8 @@ var LinkedList = (function () {
8888

8989
// Returns the index of the element passed as param otherwise -1
9090
LinkedList.prototype.indexOf = function (element) {
91-
var currentNode = this.head
92-
var index = -1
91+
let currentNode = this.head
92+
let index = -1
9393

9494
while (currentNode) {
9595
index++
@@ -106,8 +106,8 @@ var LinkedList = (function () {
106106

107107
// Returns the element at an index
108108
LinkedList.prototype.elementAt = function (index) {
109-
var currentNode = this.head
110-
var count = 0
109+
let currentNode = this.head
110+
let count = 0
111111
while (count < index) {
112112
count++
113113
currentNode = currentNode.next
@@ -118,11 +118,11 @@ var LinkedList = (function () {
118118
// Adds the element at specified index
119119
LinkedList.prototype.addAt = function (index, element) {
120120
index--
121-
var node = new Node(element)
121+
const node = new Node(element)
122122

123-
var currentNode = this.head
124-
var previousNode
125-
var currentIndex = 0
123+
let currentNode = this.head
124+
let previousNode
125+
let currentIndex = 0
126126

127127
// Check if index is out of bounds of list
128128
if (index > this.length) {
@@ -153,9 +153,9 @@ var LinkedList = (function () {
153153
// Removes the node at specified index
154154
LinkedList.prototype.removeAt = function (index) {
155155
index--
156-
var currentNode = this.head
157-
var previousNode
158-
var currentIndex = 0
156+
let currentNode = this.head
157+
let previousNode
158+
let currentIndex = 0
159159

160160
// Check if index is present in list
161161
if (index < 0 || index >= this.length) {
@@ -181,8 +181,8 @@ var LinkedList = (function () {
181181

182182
// Function to view the LinkedList
183183
LinkedList.prototype.view = function () {
184-
var currentNode = this.head
185-
var count = 0
184+
let currentNode = this.head
185+
let count = 0
186186
while (count < this.length) {
187187
count++
188188
console.log(currentNode.element)
@@ -195,7 +195,7 @@ var LinkedList = (function () {
195195
}())
196196

197197
// Implementation of LinkedList
198-
var linklist = new LinkedList()
198+
const linklist = new LinkedList()
199199
linklist.add(2)
200200
linklist.add(5)
201201
linklist.add(8)

0 commit comments

Comments
 (0)