Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cf00041
docs: add codecov badge (#1600)
vil02 Jan 29, 2024
6aaa376
tests: add tests for `SHA1` (#1602)
vil02 Jan 31, 2024
f313498
tests: add tests for `SHA256` (#1604)
vil02 Feb 9, 2024
10febce
style: cleanup `PascalTriangle` (#1606)
vil02 Feb 9, 2024
1ea7a5c
test: add missing test for `SumOfGeometricProgression` (#1607)
vil02 Feb 14, 2024
fb0a99c
fix: throw error and add tests for `ReverseNumber` (#1608)
vil02 Feb 19, 2024
0e0cf98
fix: cleanup `CoPrimeCheck` (#1609)
vil02 Feb 27, 2024
c067a34
fix: `GetEuclidGCD(0, 0)` is `0` (#1621)
vil02 Feb 28, 2024
a5945e3
fix: throw error instead of returning it (#1624)
vil02 Feb 29, 2024
8734dfc
fix: handle zeros in `CoPrimeCheck` (#1622)
vil02 Feb 29, 2024
894a46c
fix: throw error instead of returning it `RailwayTimeConversion` (#1625)
vil02 Mar 2, 2024
83b4dd8
fix: cleanup `CheckKishnamurthyNumber` (#1626)
vil02 Mar 2, 2024
2fe0dfd
fix: `throw` form `DateToDay` (#1628)
vil02 Mar 4, 2024
f13eec1
fix: properly floor the partial results (#1629)
vil02 Mar 4, 2024
d8cfdcd
chore: use `check-style` in (#1630)
vil02 Mar 4, 2024
4a4ed57
refactor: use `isLeapYear` (#1638)
vil02 Mar 7, 2024
0204198
feat: remove `twinPrime` (#1641)
vil02 Mar 11, 2024
bd34e9f
feat: remove duplicated `gcd`-like functions (#1642)
vil02 Mar 16, 2024
702840b
style: improve test names of `GetEuclidGCD'` (#1646)
vil02 Mar 28, 2024
34a663a
fix: hadnle zeros at the endpoints in `BisectionMethod` (#1640)
vil02 Apr 3, 2024
9c622dd
refactor: add and use `parseDate` (#1643)
vil02 Apr 3, 2024
d02e402
removed code already present in test cases related to DFT in Trees fo…
SourabhHere Apr 3, 2024
d920e7f
refactor: reduce code duplication in `FloodFill` (#1645)
vil02 Apr 3, 2024
314144f
Update CircularQueue.js for zero-length case (#1655)
MartinLBeacham Apr 3, 2024
6fe21d2
chore: convert functions to an ES2015 classes (#1656)
hasanalkaf3 Apr 13, 2024
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
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run style
npm run check-style
npm run test
56 changes: 15 additions & 41 deletions Conversions/DateDayDifference.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,54 +6,28 @@
Algorithm & Explanation : https://ncalculators.com/time-date/date-difference-calculator.htm
*/

// Internal method for make calculations easier
const isLeap = (year) =>{
if (year % 400 === 0) return true
else if (year % 100 === 0) return false
else if (year % 4 === 0) return true
else return false
}
import{isLeapYear } from '../Maths/LeapYear'
import{parseDate } from '../Timing-Functions/ParseDate'

const DateToDay = (dd, mm, yyyy) =>{
return Math.floor(
return (
365 * (yyyy - 1) +
(yyyy - 1) / 4 -
(yyyy - 1) / 100 +
(yyyy - 1) / 400 +
dd +
(367 * mm - 362) / 12 +
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
Math.floor((yyyy - 1) / 4) -
Math.floor((yyyy - 1) / 100) +
Math.floor((yyyy - 1) / 400) +
dd +
Math.floor((367 * mm - 362) / 12) +
(mm <= 2 ? 0 : isLeapYear(yyyy) ? -1 : -2)
)
}

const DateDayDifference = (date1, date2) =>{
// firstly, check that both input are string or not.
if (typeof date1 !== 'string' || typeof date2 !== 'string'){
return new TypeError('Argument is not a string.')
}
// extract the first date
const [firstDateDay, firstDateMonth, firstDateYear] = date1
.split('/')
.map((ele) => Number(ele))
// extract the second date
const [secondDateDay, secondDateMonth, secondDateYear] = date2
.split('/')
.map((ele) => Number(ele))
// check the both data are valid or not.
if (
firstDateDay < 0 ||
firstDateDay > 31 ||
firstDateMonth > 12 ||
firstDateMonth < 0 ||
secondDateDay < 0 ||
secondDateDay > 31 ||
secondDateMonth > 12 ||
secondDateMonth < 0
){
return new TypeError('Date is not valid.')
}
const firstDate = parseDate(date1)
const secondDate = parseDate(date2)

return Math.abs(
DateToDay(secondDateDay, secondDateMonth, secondDateYear) -
DateToDay(firstDateDay, firstDateMonth, firstDateYear)
DateToDay(secondDate.day, secondDate.month, secondDate.year) -
DateToDay(firstDate.day, firstDate.month, firstDate.year)
)
}

Expand Down
15 changes: 6 additions & 9 deletions Conversions/DateToDay.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,8 @@
Algorithm & Explanation : https://en.wikipedia.org/wiki/Zeller%27s_congruence
*/

import{parseDate } from '../Timing-Functions/ParseDate'

// Array holding name of the day: Saturday - Sunday - Friday => 0 - 1 - 6
const daysNameArr = [
'Saturday',
Expand All@@ -25,15 +27,10 @@ const daysNameArr = [

const DateToDay = (date) =>{
// firstly, check that input is a string or not.
if (typeof date !== 'string'){
return new TypeError('Argument is not a string.')
}
// extract the date
let [day, month, year] = date.split('/').map((x) => Number(x))
// check the data are valid or not.
if (day < 1 || day > 31 || month > 12 || month < 1){
return new TypeError('Date is not valid.')
}
const dateStruct = parseDate(date)
let year = dateStruct.year
let month = dateStruct.month
let day = dateStruct.day

// In case of Jan and Feb:
// Year: we consider it as previous year
Expand Down
6 changes: 3 additions & 3 deletions Conversions/RailwayTimeConversion.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,14 +18,14 @@
const RailwayTimeConversion = (timeString) =>{
// firstly, check that input is a string or not.
if (typeof timeString !== 'string'){
return new TypeError('Argument is not a string.')
throw new TypeError('Argument is not a string.')
}
// split the string by ':' character.
const [hour, minute, secondWithShift] = timeString.split(':')
// split second and shift value.
const [second, shift] = [
secondWithShift.substr(0, 2),
secondWithShift.substr(2)
secondWithShift.substring(0, 2),
secondWithShift.substring(2)
]
// convert shifted time to not-shift time(Railway time) by using the above explanation.
if (shift === 'PM'){
Expand Down
47 changes: 31 additions & 16 deletions Conversions/test/DateDayDiffernce.test.js
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
import{DateDayDifference } from '../DateDayDifference'

test('The difference between 17/08/2002 & 10/10/2020 is 6630', () =>{
const res = DateDayDifference('17/08/2002', '10/10/2020')
expect(res).toBe(6630)
})

test('The difference between 18/02/2001 & 16/03/2022 is 7696', () =>{
const res = DateDayDifference('18/02/2001', '16/03/2022')
expect(res).toBe(7696)
})
describe('DateDayDifference', () =>{
it.each([
['17/08/2002', '10/10/2020', 6629],
['18/02/2001', '16/03/2022', 7696],
['11/11/2011', '12/12/2012', 397],
['01/01/2001', '16/03/2011', 3726],
['04/03/2024', '04/03/2024', 0],
['03/03/2024', '04/03/2024', 1],
['02/03/2024', '04/03/2024', 2],
['01/03/2024', '04/03/2024', 3],
['29/02/2024', '04/03/2024', 4],
['04/03/2024', '04/03/2025', 365],
['04/03/2023', '04/03/2024', 366]
])(
'The difference between %s and %s is %i',
(firstDate, secondDate, expected) =>{
expect(DateDayDifference(firstDate, secondDate)).toBe(expected)
expect(DateDayDifference(secondDate, firstDate)).toBe(expected)
}
)

test('The difference between 11/11/2011 & 12/12/2012 is 398', () =>{
const res = DateDayDifference('11/11/2011', '12/12/2012')
expect(res).toBe(398)
})
it('should throw when any input is not a string', () =>{
expect(() => DateDayDifference(10102024, '11/10/2024')).toThrowError()
expect(() => DateDayDifference('11/10/2024', 10102024)).toThrowError()
})

test('The difference between 01/01/2001 & 16/03/2011 is 3727', () =>{
const res = DateDayDifference('01/01/2001', '16/03/2011')
expect(res).toBe(3727)
it.each(['32/01/2000', '00/01/2000', '15/00/2000', '15/13/2000'])(
'should throw when input is not a correct date %s',
(wrongDate) =>{
expect(() => DateDayDifference(wrongDate, '04/03/2024')).toThrowError()
expect(() => DateDayDifference('04/03/2024', wrongDate)).toThrowError()
}
)
})
53 changes: 23 additions & 30 deletions Conversions/test/DateToDay.test.js
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
import{DateToDay } from '../DateToDay'

test('The date 18/02/2001 is Sunday', () =>{
const res = DateToDay('18/02/2001')
expect(res).toBe('Sunday')
})

test('The date 18/12/2020 is Friday', () =>{
const res = DateToDay('18/12/2020')
expect(res).toBe('Friday')
})
describe('DateToDay', () =>{
it.each([
['18/02/2001', 'Sunday'],
['18/12/2020', 'Friday'],
['12/12/2012', 'Wednesday'],
['01/01/2001', 'Monday'],
['1/1/2020', 'Wednesday'],
['2/3/2014', 'Sunday'],
['28/2/2017', 'Tuesday'],
['02/03/2024', 'Saturday'],
['29/02/2024', 'Thursday']
])('%s is %s', (date, day) =>{
expect(DateToDay(date)).toBe(day)
})

test('The date 12/12/2012 is Wednesday', () =>{
const res = DateToDay('12/12/2012')
expect(res).toBe('Wednesday')
})
test('The date 01/01/2001 is Monday', () =>{
const res = DateToDay('01/01/2001')
expect(res).toBe('Monday')
})

test('The date 1/1/2020 is Wednesday', () =>{
const res = DateToDay('1/1/2020')
expect(res).toBe('Wednesday')
})

test('The date 2/3/2014 is Sunday', () =>{
const res = DateToDay('2/3/2014')
expect(res).toBe('Sunday')
})
it('should throw when input is not a string', () =>{
expect(() => DateToDay(100)).toThrowError()
})

test('The date 28/2/2017 is Tuesday', () =>{
const res = DateToDay('28/2/2017')
expect(res).toBe('Tuesday')
it.each(['32/01/2000', '00/01/2000', '15/00/2000', '15/13/2000'])(
'should throw when input is not a correct date %s',
(wrongDate) =>{
expect(() => DateToDay(wrongDate)).toThrowError()
}
)
})
4 changes: 4 additions & 0 deletions Conversions/test/RailwayTimeConversion.test.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,3 +19,7 @@ test('The RailwayTimeConversion of 11:20:00PM is 23:20:00', () =>{
const res = RailwayTimeConversion('11:20:00PM')
expect(res).toEqual('23:20:00')
})

test('The RailwayTimeConversion throws when input is not a string', () =>{
expect(() => RailwayTimeConversion(1120)).toThrowError()
})
2 changes: 0 additions & 2 deletions DIRECTORY.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -260,7 +260,6 @@
* [SquareRootLogarithmic](Maths/SquareRootLogarithmic.js)
* [SumOfDigits](Maths/SumOfDigits.js)
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
* [TwinPrime](Maths/TwinPrime.js)
* [TwoSum](Maths/TwoSum.js)
* [Volume](Maths/Volume.js)
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
Expand DownExpand Up@@ -296,7 +295,6 @@
* **Recursive**
* [BinaryEquivalent](Recursive/BinaryEquivalent.js)
* [BinarySearch](Recursive/BinarySearch.js)
* [EucledianGCD](Recursive/EucledianGCD.js)
* [Factorial](Recursive/Factorial.js)
* [FibonacciNumberRecursive](Recursive/FibonacciNumberRecursive.js)
* [FloodFill](Recursive/FloodFill.js)
Expand Down
8 changes: 3 additions & 5 deletions Data-Structures/Array/Reverse.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,9 @@

const Reverse = (arr) =>{
// limit specifies the amount of Reverse actions
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--){
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--)
[arr[i], arr[j]] = [arr[j], arr[i]]

return arr
}
export{Reverse }
2 changes: 1 addition & 1 deletion Data-Structures/Queue/CircularQueue.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,7 +77,7 @@ class CircularQueue{

// Displays the length of queue
length(){
return this.queue.length - 1
return this.checkEmpty() ? 0 : this.queue.length - 1
}

// Display the top most value of queue
Expand Down
18 changes: 8 additions & 10 deletions Data-Structures/Stack/Stack.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,22 +8,22 @@
// Functions: push, pop, peek, view, length

// Creates a stack constructor
const Stack = (function (){
function Stack(){
class Stack{
constructor(){
// The top of the Stack
this.top = 0
// The array representation of the stack
this.stack = []
}

// Adds a value onto the end of the stack
Stack.prototype.push = function (value){
push(value){
this.stack[this.top] = value
this.top++
}

// Removes and returns the value at the end of the stack
Stack.prototype.pop = function (){
pop(){
if (this.top === 0){
return 'Stack is Empty'
}
Expand All@@ -35,23 +35,21 @@ const Stack = (function (){
}

// Returns the size of the stack
Stack.prototype.size = function (){
size(){
return this.top
}

// Returns the value at the end of the stack
Stack.prototype.peek = function (){
peek(){
return this.stack[this.top - 1]
}

// To see all the elements in the stack
Stack.prototype.view = function (output = (value) => console.log(value)){
view(output = (value) => console.log(value)){
for (let i = 0; i < this.top; i++){
output(this.stack[i])
}
}

return Stack
})()
}

export{Stack }
Loading