8000 style: fix basic eslint warnings (#230) · Algorithms-Learn/TypeScript@23ba61b · GitHub
[go: up one dir, main page]

Skip to content

Commit 23ba61b

Browse files
authored
style: fix basic eslint warnings (TheAlgorithms#230)
1 parent 24cbb3b commit 23ba61b

30 files changed

+108
-108
lines changed

backtracking/all_combinations_of_size_k.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* and repeat the same process for the next number.
1111
*/
1212
export function generateCombinations(n: number, k: number): number[][] {
13-
let combinationsAcc: number[][] = [];
14-
let currentCombination: number[] = [];
13+
const combinationsAcc: number[][] = [];
14+
const currentCombination: number[] = [];
1515

1616
function generateAllCombos(
1717
n: number,

bit_manipulation/add_binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export function addBinary(firstBinaryNo: string, secondBinaryNo: string): string {
99
let lengthOfFirstNumber: number = firstBinaryNo.length - 1;
1010
let lengthOfSecondNumber: number = secondBinaryNo.length - 1;
11-
let solution: string[] = [];
11+
const solution: string[] = [];
1212
let carry: number = 0;
1313

1414
while ( lengthOfFirstNumber >= 0 || lengthOfSecondNumber >= 0) {

data_structures/heap/heap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export abstract class Heap<T> {
5050
}
5151

5252
public extract(): T {
53-
let maxElement = this.heap[0];
53+
const maxElement = this.heap[0];
5454
this.heap[0] = this.heap[this.size() - 1];
5555
this.heap.pop();
5656
this.sinkDown();
@@ -162,8 +162,8 @@ export class PriorityQueue<T> extends MinHeap<T> {
162162
}
163163

164164
protected swap(a: number, b: number) {
165-
let akey = this.keys_index(this.heap[a]);
166-
let bkey = this.keys_index(this.heap[b]);
165+
const akey = this.keys_index(this.heap[a]);
166+
const bkey = this.keys_index(this.heap[b]);
167167
[this.keys[akey], this.keys[bkey]] = [this.keys[bkey], this.keys[akey]];
168168
super.swap(a, b);
169169
}
@@ -188,7 +188,7 @@ export class PriorityQueue<T> extends MinHeap<T> {
188188
this.insert(value);
189189
return;
190190
}
191-
let key = this.keys[idx];
191+
const key = this.keys[idx];
192192
if (this.compare(this.heap[key], value)) {
193193
// Do not do anything if the value in the heap already has a higher priority.
194194
return;

data_structures/heap/test/heap.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("MaxHeap", () => {
88

99
beforeEach(() => {
1010
heap = new MaxHeap();
11-
for (let element of elements) {
11+
for (const element of elements) {
1212
heap.insert(element);
1313
}
1414
});
@@ -61,7 +61,7 @@ describe("MinHeap", () => {
6161

6262
beforeEach(() => {
6363
heap = new MinHeap();
64-
for (let element of elements) {
64+
for (const element of elements) {
6565
heap.insert(element);
6666
}
6767
});
@@ -106,7 +106,7 @@ describe("MinHeap", () => {
106106
});
107107

108108
it("should increase priority", () => {
109-
let heap = new PriorityQueue((a: number) => { return a; }, elements.length);
109+
const heap = new PriorityQueue((a: number) => { return a; }, elements.length);
110110
elements.forEach((element: number) => {
111111
heap.insert(element);
112112
});

data_structures/queue/linked_queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class LinkedQueue<T> implements Queue<T> {
5555
}
5656

5757
this.size--;
58-
let head = this.head; // We store the head in order not to lose track of it
58+
const head = this.head; // We store the head in order not to lose track of it
5959
this.head = this.head.next; // Update the the head to the next node
6060
return head.value; // Return the value of the head
6161
}

data_structures/stack/test/linked_list_stack.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LinkedListStack } from "../linked_list_stack";
22

33
describe("Linked List Stack", () => {
4-
let stack: LinkedListStack<number> = new LinkedListStack<number>(4);
4+
const stack: LinkedListStack<number> = new LinkedListStack<number>(4);
55

66
stack.push(1);
77
stack.push(2);

dynamic_programming/knapsack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const knapsack = (
2323
const numberOfItems = weights.length;
2424

2525
// Declaring a data structure to store calculated states/values
26-
let dp: number[][] = new Array(numberOfItems + 1);
26+
const dp: number[][] = new Array(numberOfItems + 1);
2727

2828
for (let i = 0; i < dp.length; i++) {
2929
// Placing an array at each index of dp to make it a 2d matrix

graph/bellman_ford.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
export const bellmanFord = (graph: [number, number][][], start: number): number[] | undefined => {
1313
// We save the shortest distance to each node in `distances`. If a node is
1414
// unreachable from the start node, its distance is Infinity.
15-
let distances = Array(graph.length).fill(Infinity);
15+
const distances = Array(graph.length).fill(Infinity);
1616
distances[start] = 0;
1717

1818
// On the i'th iteration, we compute all shortest paths that consists of i+1

graph/dijkstra.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import { MinHeap, PriorityQueue } from '../data_structures/heap/heap';
1313
export const dijkstra = (graph: [number, number][][], start: number): number[] => {
1414
// We use a priority queue to make sure we always visit the closest node. The
1515
// queue makes comparisons based on path weights.
16-
let priorityQueue = new PriorityQueue((a: [number, number]) => { return a[0] }, graph.length, (a: [number, number], b: [number, number]) => { return a[1] < b[1] });
16+
const priorityQueue = new PriorityQueue((a: [number, number]) => { return a[0] }, graph.length, (a: [number, number], b: [number, number]) => { return a[1] < b[1] });
1717
priorityQueue.insert([start, 0]);
1818
// We save the shortest distance to each node in `distances`. If a node is
1919
// unreachable from the start node, its distance is Infinity.
20-
let distances = Array(graph.length).fill(Infinity);
20+
const distances = Array(graph.length).fill(Infinity);
2121
distances[start] = 0;
2222

2323
while (priorityQueue.size() > 0) {
2424
const [node, _] = priorityQueue.extract();
2525
graph[node].forEach(([child, weight]) => {
26-
let new_distance = distances[node] + weight;
26+
const new_distance = distances[node] + weight;
2727
if (new_distance < distances[child]) {
2828
// Found a new shortest path to child node. Record its distance and add child to the queue.
2929
// If the child already exists in the queue, the priority will be updated. This will make sure the queue will be at most size V (number of vertices).

graph/floyd_warshall.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
*/
1111
export const floydWarshall = (graph: number[][]): number[][] => {
1212
let distances = structuredClone(graph);
13-
let N = graph.length;
13+
const N = graph.length;
1414

1515
// We begin by setting the weighted adjacency matrix as the shortest paths.
1616
// For the k'th iteration, we try to relax the shortest paths by including node k in the path.
1717
for (let k = 0; k < N; ++k) {
18-
let newDistances = [];
18+
const newDistances = [];
1919
for (let i = 0; i < N; ++i) {
2020
newDistances.push(Array(N).fill(Infinity));
2121
}

graph/johnson.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,36 @@ import { dijkstra } from './dijkstra'
1212
* @see https://en.wikipedia.org/wiki/Johnson%27s_algorithm
1313
*/
1414
export const johnson = (graph: [number, number][][]): number[][] | undefined => {
15-
let N = graph.length;
15+
const N = graph.length;
1616

1717
// Add a new node and 0 weighted edges from the new node to all existing nodes.
18-
let newNodeGraph = structuredClone(graph);
19-
let newNode: [number, number][] = [];
18+
const newNodeGraph = structuredClone(graph);
19+
const newNode: [number, number][] = [];
2020
for (let i = 0; i < N; ++i) {
2121
newNode.push([i, 0]);
2222
}
2323
newNodeGraph.push(newNode);
2424

2525
// Compute distances from the new node to existing nodes using the Bellman-Ford algorithm.
26-
let adjustedGraph = bellmanFord(newNodeGraph, N);
26+
const adjustedGraph = bellmanFord(newNodeGraph, N);
2727
if (adjustedGraph === undefined) {
2828
// Found a negative weight cycle.
2929
return undefined;
3030
}
3131

3232
for (let i = 0; i < N; ++i) {
33-
for (let edge of graph[i]) {
33+
for (const edge of graph[i]) {
3434
// Adjust edge weights using the Bellman Ford output weights. This ensure that:
3535
// 1. Each weight is non-negative. This is required for the Dijkstra algorithm.
3636
// 2. The shortest path from node i to node j consists of the same nodes with or without adjustment.
3737
edge[1] += adjustedGraph[i] - adjustedGraph[edge[0]];
3838
}
3939
}
4040

41-
let shortestPaths: number[][] = [];
41+
const shortestPaths: number[][] = [];
4242
for (let i = 0; i < N; ++i) {
4343
// Compute Dijkstra weights for each node and re-adjust weights to their original values.
44-
let dijkstraShorestPaths = dijkstra(graph, i);
44+
const dijkstraShorestPaths = dijkstra(graph, i);
4545
for (let j = 0; j < N; ++j) {
4646
dijkstraShorestPaths[j] += adjustedGraph[j] - adjustedGraph[i];
4747
}

graph/kosajaru.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const getNodePriorities = (graph: number[][], visited: boolean[], stack: number[
1414

1515
// Return the transpose of graph. The tranpose of a directed graph is a graph where each of the edges are flipped.
1616
const transpose = (graph: number[][]): number[][] => {
17-
let transposedGraph = Array(graph.length);
17+
const transposedGraph = Array(graph.length);
1818
for (let i = 0; i < graph.length; ++i) {
1919
transposedGraph[i] = [];
2020
}
@@ -52,20 +52,20 @@ const gatherScc = (graph: number[][], visited: boolean[], node: number, scc: num
5252
* @see https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
5353
*/
5454
export const kosajaru = (graph: number[][]): number[][] => {
55-
let visited = Array(graph.length).fill(false);
55+
const visited = Array(graph.length).fill(false);
5656

57-
let stack: number[] = [];
57+
const stack: number[] = [];
5858
for (let i = 0; i < graph.length; ++i) {
5959
getNodePriorities(graph, visited, stack, i);
6060
}
6161

6262
const transposedGraph = transpose(graph);
6363

64-
let sccs = [];
64+
const sccs = [];
6565
visited.fill(false);
6666
for (let i = stack.length - 1; i >= 0; --i) {
6767
if (!visited[stack[i]]) {
68-
let scc: number[] = [];
68+
const scc: number[] = [];
6969
gatherScc(transposedGraph, visited, stack[i], scc);
7070
sccs.push(scc);
7171
}

graph/kruskal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import { DisjointSet } from '../data_structures/disjoint_set/disjoint_set';
1313
*/
1414
export const kruskal = (edges: Edge[], num_vertices: number): [Edge[], number] => {
1515
let cost = 0;
16-
let minimum_spanning_tree = [];
16+
const minimum_spanning_tree = [];
1717

1818
// Use a disjoint set to quickly join sets and find if vertices live in different sets
19-
let sets = new DisjointSet(num_vertices);
19+
const sets = new DisjointSet(num_vertices);
2020

2121
// Sort the edges in ascending order by weight so that we can greedily add cheaper edges to the tree
2222
edges.sort((a, b) => a.weight - b.weight);
2323

24-
for (let edge of edges) {
24+
for (const edge of edges) {
2525
if (sets.find(edge.a) !== sets.find(edge.b)) {
2626
// Node A and B live in different sets. Add edge(a, b) to the tree and join the nodes' sets together.
2727
minimum_spanning_tree.push(edge);

graph/prim.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ export const prim = (graph: [number, number][][]): [Edge[], number] => {
1313
if (graph.length == 0) {
1414
return [[], 0];
1515
}
16-
let minimum_spanning_tree: Edge[] = [];
16+
const minimum_spanning_tree: Edge[] = [];
1717
let total_weight = 0;
1818

19-
let priorityQueue = new PriorityQueue((e: Edge) => { return e.b }, graph.length, (a: Edge, b: Edge) => { return a.weight < b.weight });
20-
let visited = new Set<number>();
19+
const priorityQueue = new PriorityQueue((e: Edge) => { return e.b }, graph.length, (a: Edge, b: Edge) => { return a.weight < b.weight });
20+
const visited = new Set<number>();
2121

2222
// Start from the 0'th node. For fully connected graphs, we can start from any node and still produce the MST.
2323
visited.add(0);
2424
add_children(graph, priorityQueue, 0);
2525

2626
while (!priorityQueue.isEmpty()) {
2727
// We have already visited vertex `edge.a`. If we have not visited `edge.b` yet, we add its outgoing edges to the PriorityQueue.
28-
let edge = priorityQueue.extract();
28+
const edge = priorityQueue.extract();
2929
if (visited.has(edge.b)) {
3030
continue;
3131
}
@@ -40,7 +40,7 @@ export const prim = (graph: [number, number][][]): [Edge[], number] => {
4040

4141
const add_children = (graph: [number, number][][], priorityQueue: PriorityQueue<Edge>, node: number) => {
4242
for (let i = 0; i < graph[node].length; ++i) {
43-
let out_edge = graph[node][i];
43+
const out_edge = graph[node][i];
4444
// By increasing the priority, we ensure we only add each vertex to the queue one time, and the queue will be at most size V.
4545
priorityQueue.increasePriority(out_edge[0], new Edge(node, out_edge[0], out_edge[1]));
4646
}

graph/tarjan.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export const tarjan = (graph: number[][]): number[][] => {
1515

1616
let index = 0;
1717
// The order in which we discover nodes
18-
let discovery: number[] = Array(graph.length);
18+
const discovery: number[] = Array(graph.length);
1919
// For each node, holds the furthest ancestor it can reach
20-
let low: number[] = Array(graph.length).fill(undefined);
20+
const low: number[] = Array(graph.length).fill(undefined);
2121
// Holds the nodes we have visited in a DFS traversal and are considering to group into a SCC
22-
let stack: number[] = [];
22+
const stack: number[] = [];
2323
// Holds the elements in the stack.
24-
let stackContains = Array(graph.length).fill(false);
25-
let sccs: number[][] = [];
24+
const stackContains = Array(graph.length).fill(false);
25+
const sccs: number[][] = [];
2626

2727
const dfs = (node: number) => {
2828
discovery[node] = index;
@@ -46,7 +46,7 @@ export const tarjan = (graph: number[][]): number[][] => {
4646

4747
if (discovery[node] == low[node]) {
4848
// node is the root of a SCC. Gather the SCC's nodes from the stack.
49-
let scc: number[] = [];
49+
const scc: number[] = [];
5050
let i;
5151
for (i = stack.length - 1; stack[i] != node; --i) {
5252
scc.push(stack[i]);

graph/test/bellman_ford.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { bellmanFord } from "../bellman_ford";
22

33
const init_graph = (N: number): [number, number][][] => {
4-
let graph = Array(N);
4+
const graph = Array(N);
55
for (let i = 0; i < N; ++i) {
66
graph[i] = [];
77
}
@@ -16,7 +16,7 @@ describe("bellmanFord", () => {
1616
}
1717

1818
it("should return the correct value", () => {
19-
let graph = init_graph(9);
19+
const graph = init_graph(9);
2020
add_edge(graph, 0, 1, 4);
2121
add_edge(graph, 0, 7, 8);
2222
add_edge(graph, 1, 2, 8);
@@ -38,7 +38,7 @@ describe("bellmanFord", () => {
3838
expect(bellmanFord([[]], 0)).toStrictEqual([0]);
3939
});
4040

41-
let linear_graph = init_graph(4);
41+
const linear_graph = init_graph(4);
4242
add_edge(linear_graph, 0, 1, 1);
4343
add_edge(linear_graph, 1, 2, 2);
4444
add_edge(linear_graph, 2, 3, 3);
@@ -49,7 +49,7 @@ describe("bellmanFord", () => {
4949
}
5050
);
5151

52-
let unreachable_graph = init_graph(3);
52+
const unreachable_graph = init_graph(3);
5353
add_edge(unreachable_graph, 0, 1, 1);
5454
test.each([[0, [0, 1, Infinity]], [1, [1, 0, Infinity]], [2, [Infinity, Infinity, 0]]])(
5555
"correct result for graph with unreachable nodes with source node %i",
@@ -61,15 +61,15 @@ describe("bellmanFord", () => {
6161

6262
describe("bellmanFord negative cycle graphs", () => {
6363
it("should returned undefined for 2-node graph with negative cycle", () => {
64-
let basic = init_graph(2);
64+
const basic = init_graph(2);
6565
basic[0].push([1, 2]);
6666
basic[1].push([0, -3]);
6767
expect(bellmanFord(basic, 0)).toStrictEqual(undefined);
6868
expect(bellmanFord(basic, 1)).toStrictEqual(undefined);
6969
});
7070

7171
it("should returned undefined for graph with negative cycle", () => {
72-
let negative = init_graph(5);
72+
const negative = init_graph(5);
7373
negative[0].push([1, 6]);
7474
negative[0].push([3, 7]);
7575
negative[1].push([2, 5]);

graph/test/dijkstra.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { dijkstra } from "../dijkstra";
33
describe("dijkstra", () => {
44

55
const init_graph = (N: number): [number, number][][] => {
6-
let graph = Array(N);
6+
const graph = Array(N);
77
for (let i = 0; i < N; ++i) {
88
graph[i] = [];
99
}
@@ -16,7 +16,7 @@ describe("dijkstra", () => {
1616
}
1717

1818
it("should return the correct value", () => {
19-
let graph = init_graph(9);
19+
const graph = init_graph(9);
2020
add_edge(graph, 0, 1, 4);
2121
add_edge(graph, 0, 7, 8);
2222
add_edge(graph, 1, 2, 8);
@@ -38,7 +38,7 @@ describe("dijkstra", () => {
3838
expect(dijkstra([[]], 0)).toStrictEqual([0]);
3939
});
4040

41-
let linear_graph = init_graph(4);
41+
const linear_graph = init_graph(4);
4242
add_edge(linear_graph, 0, 1, 1);
4343
add_edge(linear_graph, 1, 2, 2);
4444
add_edge(linear_graph, 2, 3, 3);
@@ -49,7 +49,7 @@ describe("dijkstra", () => {
4949
}
5050
);
5151

52-
let unreachable_graph = init_graph(3);
52+
const unreachable_graph = init_graph(3);
5353
add_edge(unreachable_graph, 0, 1, 1);
5454
test.each([[0, [0, 1, Infinity]], [1, [1, 0, Infinity]], [2, [Infinity, Infinity, 0]]])(
5555
"correct result for graph with unreachable nodes with source node %i",

0 commit comments

Comments
 (0)
0