feat: Test running overhaul, switch to Prettier & reformat everything (#1407)

* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
Roland Hummel
2023-10-03 23:08:19 +02:00
committed by GitHub
parent 0ca18c2b2c
commit 86d333ee94
392 changed files with 5849 additions and 16622 deletions

View File

@@ -14,11 +14,11 @@ Link for the Problem: https://leetcode.com/problems/add-two-numbers/
*/
class AddTwoNumbers {
constructor () {
constructor() {
this.dummyNode = new Node(0)
}
solution (firstList, secondList) {
solution(firstList, secondList) {
let firstRunner = firstList
let secondRunner = secondList
let tail = this.dummyNode
@@ -44,7 +44,7 @@ class AddTwoNumbers {
return this.dummyNode.next
}
solutionToArray () {
solutionToArray() {
const list = []
let currentNode = this.dummyNode.next
while (currentNode) {

View File

@@ -3,18 +3,22 @@
* https://en.wikipedia.org/wiki/Cycle_detection
*/
function detectCycle (head) {
function detectCycle(head) {
/*
Problem Statement:
Given head, the head of a linked list, determine if the linked list has a cycle in it.
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
*/
if (!head) { return false }
if (!head) {
return false
}
let slow = head
let fast = head.next
while (fast && fast.next) {
if (fast === slow) { return true }
if (fast === slow) {
return true
}
fast = fast.next.next
slow = slow.next
}

View File

@@ -1,5 +1,5 @@
class Node {
constructor (element) {
constructor(element) {
this.element = element
this.next = null
this.prev = null
@@ -7,14 +7,14 @@ class Node {
}
class DoubleLinkedList {
constructor () {
constructor() {
this.length = 0
this.head = null
this.tail = null
}
// Add new element
append (element) {
append(element) {
const node = new Node(element)
if (!this.head) {
@@ -30,7 +30,7 @@ class DoubleLinkedList {
}
// Add element
insert (position, element) {
insert(position, element) {
// Check of out-of-bound values
if (position >= 0 && position <= this.length) {
const node = new Node(element)
@@ -74,7 +74,7 @@ class DoubleLinkedList {
}
// Remove element at any position
removeAt (position) {
removeAt(position) {
// look for out-of-bounds value
if (position > -1 && position < this.length) {
let current = this.head
@@ -114,7 +114,7 @@ class DoubleLinkedList {
}
// Get the indexOf item
indexOf (elm) {
indexOf(elm) {
let current = this.head
let index = -1
@@ -133,27 +133,27 @@ class DoubleLinkedList {
}
// Find the item in the list
isPresent (elm) {
isPresent(elm) {
return this.indexOf(elm) !== -1
}
// Delete an item from the list
delete (elm) {
delete(elm) {
return this.removeAt(this.indexOf(elm))
}
// Delete first item from the list
deleteHead () {
deleteHead() {
this.removeAt(0)
}
// Delete last item from the list
deleteTail () {
deleteTail() {
this.removeAt(this.length - 1)
}
// Print item of the string
toString () {
toString() {
let current = this.head
let string = ''
@@ -166,7 +166,7 @@ class DoubleLinkedList {
}
// Convert list to array
toArray () {
toArray() {
const arr = []
let current = this.head
@@ -179,31 +179,31 @@ class DoubleLinkedList {
}
// Check if list is empty
isEmpty () {
isEmpty() {
return this.length === 0
}
// Get the size of the list
size () {
size() {
return this.length
}
// Get the this.head
getHead () {
getHead() {
return this.head
}
// Get the this.tail
getTail () {
getTail() {
return this.tail
}
// Method to iterate over the LinkedList
iterator () {
iterator() {
let currentNode = this.getHead()
if (currentNode === null) return -1
const iterate = function * () {
const iterate = function* () {
while (currentNode) {
yield currentNode.element
currentNode = currentNode.next
@@ -214,7 +214,7 @@ class DoubleLinkedList {
// Method to log the LinkedList, for debugging
// it' a circular structure, so can't use stringify to debug the whole structure
log () {
log() {
let currentNode = this.getHead()
while (currentNode) {
console.log(currentNode.element)

View File

@@ -1,7 +1,7 @@
/** A LinkedList based solution to reverse a number
Problem Statement: Given a number such that each of its digit is stored in a singly linked list. Reverse the linked list and return the head of the linked list Link for the Problem: https://leetcode.com/problems/reverse-linked-list/ */
class ReverseSinglyLinkedList {
solution (head) {
solution(head) {
let prev = null
let next = null
while (head) {
@@ -11,6 +11,6 @@ class ReverseSinglyLinkedList {
head = next
}
return prev
};
}
}
export { ReverseSinglyLinkedList }

View File

@@ -2,7 +2,7 @@
import { Node } from './SinglyLinkedList.js'
class SinglyCircularLinkedList {
constructor () {
constructor() {
this.headNode = null
this.length = 0
}
@@ -15,12 +15,12 @@ class SinglyCircularLinkedList {
isEmpty = () => this.length === 0
// initiate the node and index
initiateNodeAndIndex () {
initiateNodeAndIndex() {
return { currentNode: this.headNode, currentIndex: 0 }
}
// get the data specific to an index
getElementAt (index) {
getElementAt(index) {
if (this.length !== 0 && index >= 0 && index <= this.length) {
let { currentNode } = this.initiateNodeAndIndex()
for (let i = 0; i < index && currentNode !== null; i++) {
@@ -32,7 +32,7 @@ class SinglyCircularLinkedList {
}
// Add the element in the first position
addAtFirst (data) {
addAtFirst(data) {
const node = new Node(data)
node.next = this.headNode
this.headNode = node
@@ -41,8 +41,10 @@ class SinglyCircularLinkedList {
}
// Add any data to the end of the linkedList
add (data) {
if (!this.headNode) { return this.addAtFirst(data) }
add(data) {
if (!this.headNode) {
return this.addAtFirst(data)
}
const node = new Node(data)
// Getting the last node
const currentNode = this.getElementAt(this.length - 1)
@@ -53,10 +55,11 @@ class SinglyCircularLinkedList {
}
// insert data at a specific position
insertAt (index, data) {
insertAt(index, data) {
if (index === 0) return this.addAtFirst(data)
if (index === this.length) return this.add(data)
if (index < 0 || index > this.length) throw new RangeError(`Index is out of range max ${this.length}`)
if (index < 0 || index > this.length)
throw new RangeError(`Index is out of range max ${this.length}`)
const node = new Node(data)
const previousNode = this.getElementAt(index - 1)
node.next = previousNode.next
@@ -66,7 +69,7 @@ class SinglyCircularLinkedList {
}
// find the first index of the data
indexOf (data) {
indexOf(data) {
let { currentNode } = this.initiateNodeAndIndex()
// initializing currentIndex as -1
let currentIndex = -1
@@ -81,7 +84,7 @@ class SinglyCircularLinkedList {
}
// remove the data from the end of the list
remove () {
remove() {
if (this.isEmpty()) return null
const secondLastNode = this.getElementAt(this.length - 2)
const removedNode = secondLastNode.next
@@ -91,7 +94,7 @@ class SinglyCircularLinkedList {
}
// remove the data from the first of the list
removeFirst () {
removeFirst() {
if (this.isEmpty()) return null
const removedNode = this.headNode
if (this.length === 1) {
@@ -106,7 +109,7 @@ class SinglyCircularLinkedList {
}
// remove the data from the index
removeAt (index) {
removeAt(index) {
if (this.isEmpty()) return null
if (index === 0) return this.removeFirst()
if (index === this.length) return this.remove()
@@ -119,14 +122,14 @@ class SinglyCircularLinkedList {
}
// remove if the data is present
removeData (data) {
removeData(data) {
if (this.isEmpty()) return null
const index = this.indexOf(data)
return this.removeAt(index)
}
// logs the data
printData (output = value => console.log(value)) {
printData(output = (value) => console.log(value)) {
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
while (currentNode !== null && currentIndex < this.length) {
@@ -137,7 +140,7 @@ class SinglyCircularLinkedList {
}
// get the data from the linkedList
get () {
get() {
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
const list = []
while (currentNode !== null && currentIndex < this.length) {
@@ -148,7 +151,7 @@ class SinglyCircularLinkedList {
return list
}
clear () {
clear() {
this.headNode = null
this.length = 0
}

View File

@@ -9,14 +9,14 @@
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean, rotateListRight
class Node {
constructor (data) {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor (listOfValues) {
constructor(listOfValues) {
this.headNode = null
this.tailNode = null
this.length = 0
@@ -29,32 +29,32 @@ class LinkedList {
}
// initiates the currentNode and currentIndex and return as an object
initiateNodeAndIndex () {
initiateNodeAndIndex() {
return { currentNode: this.headNode, currentIndex: 0 }
}
// Returns length
size () {
size() {
return this.length
}
// Returns the head
head () {
head() {
return this.headNode?.data ?? null
}
// Returns the tail
tail () {
tail() {
return this.tailNode?.data ?? null
}
// Return if the list is empty
isEmpty () {
isEmpty() {
return this.length === 0
}
// add a node at last it to linklist
addLast (element) {
addLast(element) {
// Check if its the first element
if (this.headNode === null) {
return this.addFirst(element)
@@ -68,7 +68,7 @@ class LinkedList {
}
// add a node at first it to linklist
addFirst (element) {
addFirst(element) {
const node = new Node(element)
// Check if its the first element
if (this.headNode === null) {
@@ -82,7 +82,7 @@ class LinkedList {
}
// remove the first from the linklist
removeFirst () {
removeFirst() {
// Check if head is null
if (this.headNode === null) {
return null
@@ -99,7 +99,7 @@ class LinkedList {
}
// remove the last from the linklist
removeLast () {
removeLast() {
if (this.isEmpty()) return null
// Check if there is only one element
if (this.length === 1) {
@@ -118,7 +118,7 @@ class LinkedList {
}
// Removes the node with the value as param
remove (element) {
remove(element) {
if (this.isEmpty()) return null
let { currentNode } = this.initiateNodeAndIndex()
let removedNode = null
@@ -144,7 +144,7 @@ class LinkedList {
}
// Returns the index of the element passed as param otherwise -1
indexOf (element) {
indexOf(element) {
if (this.isEmpty()) return -1
let { currentNode, currentIndex } = this.initiateNodeAndIndex()
while (currentNode) {
@@ -158,7 +158,7 @@ class LinkedList {
}
// Returns the element at an index
elementAt (index) {
elementAt(index) {
if (index >= this.length || index < 0) {
throw new RangeError('Out of Range index')
}
@@ -171,7 +171,7 @@ class LinkedList {
}
// Adds the element at specified index
addAt (index, element) {
addAt(index, element) {
// Check if index is out of bounds of list
if (index > this.length || index < 0) {
throw new RangeError('Out of Range index')
@@ -196,7 +196,7 @@ class LinkedList {
}
// Removes the node at specified index
removeAt (index) {
removeAt(index) {
// Check if index is present in list
if (index < 0 || index >= this.length) {
throw new RangeError('Out of Range index')
@@ -216,7 +216,7 @@ class LinkedList {
}
// Returns a reference to middle node of linked list
findMiddle () {
findMiddle() {
// If there are two middle nodes, return the second middle node.
let fast = this.headNode
let slow = this.headNode
@@ -229,14 +229,14 @@ class LinkedList {
}
// make the linkedList Empty
clean () {
clean() {
this.headNode = null
this.tailNode = null
this.length = 0
}
// Method to get the LinkedList
get () {
get() {
const list = []
let { currentNode } = this.initiateNodeAndIndex()
while (currentNode) {
@@ -247,7 +247,7 @@ class LinkedList {
}
// Method for Rotating a List to the right by k places
rotateListRight (k) {
rotateListRight(k) {
if (!this.headNode) return
let current = this.headNode
let tail = this.tailNode
@@ -268,11 +268,11 @@ class LinkedList {
}
// Method to iterate over the LinkedList
iterator () {
iterator() {
let { currentNode } = this.initiateNodeAndIndex()
if (currentNode === null) return -1
const iterate = function * () {
const iterate = function* () {
while (currentNode) {
yield currentNode.data
currentNode = currentNode.next
@@ -282,12 +282,12 @@ class LinkedList {
}
// Method to log the LinkedList
log () {
log() {
console.log(JSON.stringify(this.headNode, null, 2))
}
// Method to reverse the LinkedList
reverse () {
reverse() {
let head = this.headNode
let prev = null
let next = null
@@ -299,7 +299,7 @@ class LinkedList {
}
this.tailNode = this.headNode
this.headNode = prev
};
}
}
export { Node, LinkedList }

View File

@@ -1,147 +1,147 @@
import { SinglyCircularLinkedList } from '../SinglyCircularLinkedList'
describe('SinglyCircularLinkedList', () => {
let list
beforeEach(() => {
list = new SinglyCircularLinkedList()
})
it('Check get', () => {
expect(list.get()).toEqual([])
expect(list.add(1)).toEqual(1)
expect(list.get()).toEqual([1])
expect(list.add(5)).toEqual(2)
expect(list.get()).toEqual([1, 5])
})
it('Check size', () => {
expect(list.size()).toEqual(0)
expect(list.add(1)).toEqual(1)
expect(list.add(1)).toEqual(2)
expect(list.size()).toEqual(2)
})
it('Check head', () => {
expect(list.head()).toEqual(null)
expect(list.add(1)).toEqual(1)
expect(list.head()).toEqual(1)
expect(list.add(1)).toEqual(2)
expect(list.head()).toEqual(1)
expect(list.addAtFirst(100)).toEqual(3)
expect(list.head()).toEqual(100)
expect(list.insertAt(0, 500)).toEqual(4)
expect(list.head()).toEqual(500)
list.clear()
expect(list.head()).toEqual(null)
})
it('Check isEmpty', () => {
expect(list.isEmpty()).toEqual(true)
expect(list.add(1)).toEqual(1)
expect(list.add(1)).toEqual(2)
expect(list.isEmpty()).toEqual(false)
})
it('Check getElementAt', () => {
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.getElementAt(1).data).toEqual(200)
expect(list.getElementAt(3).data).toEqual(500)
})
it('Check addAtFirst', () => {
list.add(1)
list.add(5)
list.add(7)
list.add(9)
list.add(0)
expect(list.get()).toEqual([1, 5, 7, 9, 0])
list.addAtFirst(100)
expect(list.get()).toEqual([100, 1, 5, 7, 9, 0])
})
it('Check add', () => {
list.add(1)
list.add(5)
list.add(7)
list.add(9)
list.add(0)
expect(list.get()).toEqual([1, 5, 7, 9, 0])
list.add(100)
expect(list.get()).toEqual([1, 5, 7, 9, 0, 100])
})
it('Check insertAt', () => {
expect(list.insertAt(0, 100)).toEqual(1)
expect(list.get()).toEqual([100])
expect(list.insertAt(0, 200)).toEqual(2)
expect(list.get()).toEqual([200, 100])
expect(list.insertAt(2, 300)).toEqual(3)
expect(list.get()).toEqual([200, 100, 300])
})
it('Checks indexOf', () => {
expect(list.indexOf(200)).toEqual(-1)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.indexOf(200)).toEqual(1)
})
it('Check remove', () => {
expect(list.remove()).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.remove()
expect(removedData).toEqual(900)
expect(list.get()).toEqual([100, 200, 300, 500])
})
it('Check removeFirst', () => {
expect(list.removeFirst()).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.removeFirst()
expect(removedData).toEqual(100)
expect(list.get()).toEqual([200, 300, 500, 900])
})
it('Check removeAt', () => {
expect(list.removeAt(1)).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.removeAt(2)
expect(removedData).toEqual(300)
expect(list.get()).toEqual([100, 200, 500, 900])
})
it('Check removeData', () => {
expect(list.removeData(100)).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.removeData(200)
expect(removedData).toEqual(200)
expect(list.get()).toEqual([100, 300, 500, 900])
})
})
import { SinglyCircularLinkedList } from '../SinglyCircularLinkedList'
describe('SinglyCircularLinkedList', () => {
let list
beforeEach(() => {
list = new SinglyCircularLinkedList()
})
it('Check get', () => {
expect(list.get()).toEqual([])
expect(list.add(1)).toEqual(1)
expect(list.get()).toEqual([1])
expect(list.add(5)).toEqual(2)
expect(list.get()).toEqual([1, 5])
})
it('Check size', () => {
expect(list.size()).toEqual(0)
expect(list.add(1)).toEqual(1)
expect(list.add(1)).toEqual(2)
expect(list.size()).toEqual(2)
})
it('Check head', () => {
expect(list.head()).toEqual(null)
expect(list.add(1)).toEqual(1)
expect(list.head()).toEqual(1)
expect(list.add(1)).toEqual(2)
expect(list.head()).toEqual(1)
expect(list.addAtFirst(100)).toEqual(3)
expect(list.head()).toEqual(100)
expect(list.insertAt(0, 500)).toEqual(4)
expect(list.head()).toEqual(500)
list.clear()
expect(list.head()).toEqual(null)
})
it('Check isEmpty', () => {
expect(list.isEmpty()).toEqual(true)
expect(list.add(1)).toEqual(1)
expect(list.add(1)).toEqual(2)
expect(list.isEmpty()).toEqual(false)
})
it('Check getElementAt', () => {
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.getElementAt(1).data).toEqual(200)
expect(list.getElementAt(3).data).toEqual(500)
})
it('Check addAtFirst', () => {
list.add(1)
list.add(5)
list.add(7)
list.add(9)
list.add(0)
expect(list.get()).toEqual([1, 5, 7, 9, 0])
list.addAtFirst(100)
expect(list.get()).toEqual([100, 1, 5, 7, 9, 0])
})
it('Check add', () => {
list.add(1)
list.add(5)
list.add(7)
list.add(9)
list.add(0)
expect(list.get()).toEqual([1, 5, 7, 9, 0])
list.add(100)
expect(list.get()).toEqual([1, 5, 7, 9, 0, 100])
})
it('Check insertAt', () => {
expect(list.insertAt(0, 100)).toEqual(1)
expect(list.get()).toEqual([100])
expect(list.insertAt(0, 200)).toEqual(2)
expect(list.get()).toEqual([200, 100])
expect(list.insertAt(2, 300)).toEqual(3)
expect(list.get()).toEqual([200, 100, 300])
})
it('Checks indexOf', () => {
expect(list.indexOf(200)).toEqual(-1)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.indexOf(200)).toEqual(1)
})
it('Check remove', () => {
expect(list.remove()).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.remove()
expect(removedData).toEqual(900)
expect(list.get()).toEqual([100, 200, 300, 500])
})
it('Check removeFirst', () => {
expect(list.removeFirst()).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.removeFirst()
expect(removedData).toEqual(100)
expect(list.get()).toEqual([200, 300, 500, 900])
})
it('Check removeAt', () => {
expect(list.removeAt(1)).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.removeAt(2)
expect(removedData).toEqual(300)
expect(list.get()).toEqual([100, 200, 500, 900])
})
it('Check removeData', () => {
expect(list.removeData(100)).toEqual(null)
list.add(100)
list.add(200)
list.add(300)
list.add(500)
list.add(900)
expect(list.get()).toEqual([100, 200, 300, 500, 900])
const removedData = list.removeData(200)
expect(removedData).toEqual(200)
expect(list.get()).toEqual([100, 300, 500, 900])
})
})