npx standard --fix

This commit is contained in:
cclauss
2020-05-03 09:05:12 +02:00
parent e62ad2f73e
commit 856dc2f63c
47 changed files with 2240 additions and 2371 deletions

View File

@@ -1,71 +1,66 @@
// starting at s
function solve(graph, s) {
var solutions = {};
solutions[s] = [];
solutions[s].dist = 0;
while(true) {
var p = null;
var neighbor = null;
var dist = Infinity;
for(var n in solutions) {
if(!solutions[n])
continue
var ndist = solutions[n].dist;
var adj = graph[n];
for(var a in adj) {
function solve (graph, s) {
var solutions = {}
solutions[s] = []
solutions[s].dist = 0
if(solutions[a])
continue;
var d = adj[a] + ndist;
if(d < dist) {
p = solutions[n];
neighbor = a;
dist = d;
while (true) {
var p = null
var neighbor = null
var dist = Infinity
for (var n in solutions) {
if (!solutions[n]) { continue }
var ndist = solutions[n].dist
var adj = graph[n]
for (var a in adj) {
if (solutions[a]) { continue }
var d = adj[a] + ndist
if (d < dist) {
p = solutions[n]
neighbor = a
dist = d
}
}
}
//no more solutions
if(dist === Infinity) {
break;
// no more solutions
if (dist === Infinity) {
break
}
//extend parent's solution path
solutions[neighbor] = p.concat(neighbor);
//extend parent's cost
solutions[neighbor].dist = dist;
// extend parent's solution path
solutions[neighbor] = p.concat(neighbor)
// extend parent's cost
solutions[neighbor].dist = dist
}
return solutions;
return solutions
}
//create graph
var graph = {};
// create graph
var graph = {}
var layout = {
'R': ['2'],
'2': ['3','4'],
'3': ['4','6','13'],
'4': ['5','8'],
'5': ['7','11'],
'6': ['13','15'],
'7': ['10'],
'8': ['11','13'],
'9': ['14'],
'10': [],
'11': ['12'],
'12': [],
'13': ['14'],
'14': [],
'15': []
R: ['2'],
2: ['3', '4'],
3: ['4', '6', '13'],
4: ['5', '8'],
5: ['7', '11'],
6: ['13', '15'],
7: ['10'],
8: ['11', '13'],
9: ['14'],
10: [],
11: ['12'],
12: [],
13: ['14'],
14: [],
15: []
}
//convert uni-directional to bi-directional graph
// convert uni-directional to bi-directional graph
// var graph = {
// a: {e:1, b:1, g:3},
// b: {a:1, c:1},
@@ -77,27 +72,25 @@ var layout = {
// h: {f:1}
// };
for(var id in layout) {
if(!graph[id])
graph[id] = {};
layout[id].forEach(function(aid) {
graph[id][aid] = 1;
if(!graph[aid])
graph[aid] = {};
graph[aid][id] = 1;
});
for (var id in layout) {
if (!graph[id]) { graph[id] = {} }
layout[id].forEach(function (aid) {
graph[id][aid] = 1
if (!graph[aid]) { graph[aid] = {} }
graph[aid][id] = 1
})
}
//choose start node
var start = '10';
//get all solutions
var solutions = solve(graph, start);
// choose start node
var start = '10'
// get all solutions
var solutions = solve(graph, start)
console.log("From '"+start+"' to");
//display solutions
for(var s in solutions) {
if(!solutions[s]) continue;
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")");
console.log("From '" + start + "' to")
// display solutions
for (var s in solutions) {
if (!solutions[s]) continue
console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
}
// From '10' to

View File

@@ -11,16 +11,16 @@
https://en.wikipedia.org/wiki/Absolute_value
*/
function abs_val(num) {
// Find absolute value of `num`.
"use strict";
if (num < 0) {
return -num
}
// Executes if condition is not met.
return num
function abs_val (num) {
// Find absolute value of `num`.
'use strict'
if (num < 0) {
return -num
}
// Executes if condition is not met.
return num
}
// Run `abs` function to find absolute value of two numbers.
console.log("The absolute value of -34 is " + abs_val(-34));
console.log("The absolute value of 34 is " + abs_val(34));
console.log('The absolute value of -34 is ' + abs_val(-34))
console.log('The absolute value of 34 is ' + abs_val(34))

View File

@@ -11,20 +11,20 @@
https://en.wikipedia.org/wiki/Mean
*/
function mean(nums) {
"use strict";
var sum = 0;
var avg;
function mean (nums) {
'use strict'
var sum = 0
var avg
// This loop sums all values in the 'nums' array.
nums.forEach(function (current) {
sum += current;
});
// This loop sums all values in the 'nums' array.
nums.forEach(function (current) {
sum += current
})
// Divide sum by the length of the 'nums' array.
avg = sum / nums.length;
return avg;
// Divide sum by the length of the 'nums' array.
avg = sum / nums.length
return avg
}
// Run `mean` Function to find average of a list of numbers.
console.log(mean([2, 4, 6, 8, 20, 50, 70]));
console.log(mean([2, 4, 6, 8, 20, 50, 70]))

View File

@@ -11,42 +11,42 @@
https://en.wikipedia.org/wiki/factorial
*/
"use strict";
'use strict'
function calc_range(num) {
// Generate a range of numbers from 1 to `num`.
var i = 1;
var range = [];
while (i <= num) {
range.push(i);
i += 1;
}
return range;
function calc_range (num) {
// Generate a range of numbers from 1 to `num`.
var i = 1
var range = []
while (i <= num) {
range.push(i)
i += 1
}
return range
}
function calc_factorial(num) {
var factorial;
var range = calc_range(num);
function calc_factorial (num) {
var factorial
var range = calc_range(num)
// Check if the number is negative, positive, null, undefined, or zero
if (num < 0) {
return "Sorry, factorial does not exist for negative numbers.";
}
if (num === null || num === undefined) {
return "Sorry, factorial does not exist for null or undefined numbers.";
}
if (num === 0) {
return "The factorial of 0 is 1.";
}
if (num > 0) {
factorial = 1;
range.forEach(function (i) {
factorial = factorial * i;
});
return "The factorial of " + num + " is " + factorial;
}
// Check if the number is negative, positive, null, undefined, or zero
if (num < 0) {
return 'Sorry, factorial does not exist for negative numbers.'
}
if (num === null || num === undefined) {
return 'Sorry, factorial does not exist for null or undefined numbers.'
}
if (num === 0) {
return 'The factorial of 0 is 1.'
}
if (num > 0) {
factorial = 1
range.forEach(function (i) {
factorial = factorial * i
})
return 'The factorial of ' + num + ' is ' + factorial
}
}
// Run `factorial` Function to find average of a list of numbers.
var num = prompt("Enter a number: ");
alert(calc_factorial(num));
var num = prompt('Enter a number: ')
alert(calc_factorial(num))

View File

@@ -9,30 +9,30 @@
https://en.wikipedia.org/wiki/Least_common_multiple
*/
"use strict";
'use strict'
// Find the LCM of two numbers.
function find_lcm(num_1, num_2) {
var max_num;
var lcm;
// Check to see whether num_1 or num_2 is larger.
if (num_1 > num_2) {
max_num = num_1;
} else {
max_num = num_2;
}
lcm = max_num;
function find_lcm (num_1, num_2) {
var max_num
var lcm
// Check to see whether num_1 or num_2 is larger.
if (num_1 > num_2) {
max_num = num_1
} else {
max_num = num_2
}
lcm = max_num
while (true) {
if ((lcm % num_1 === 0) && (lcm % num_2 === 0)) {
break;
}
lcm += max_num;
while (true) {
if ((lcm % num_1 === 0) && (lcm % num_2 === 0)) {
break
}
return lcm;
lcm += max_num
}
return lcm
}
// Run `find_lcm` Function
var num_1 = 12;
var num_2 = 76;
console.log(find_lcm(num_1, num_2));
var num_1 = 12
var num_2 = 76
console.log(find_lcm(num_1, num_2))

View File

@@ -1,94 +1,89 @@
// create a graph class
class Graph {
// defining vertex array and
// adjacent list
constructor(noOfVertices)
{
this.noOfVertices = noOfVertices;
this.AdjList = new Map();
}
// functions to be implemented
// addVertex(v)
// addEdge(v, w)
// printGraph()
// bfs(v)
// dfs(v)
// create a graph class
class Graph {
// defining vertex array and
// adjacent list
constructor (noOfVertices) {
this.noOfVertices = noOfVertices
this.AdjList = new Map()
}
// functions to be implemented
// addVertex(v)
// addEdge(v, w)
// printGraph()
// bfs(v)
// dfs(v)
}
// add vertex to the graph
addVertex(v)
{
// initialize the adjacent list with a
// null array
this.AdjList.set(v, []);
// add vertex to the graph
addVertex(v)
{
// initialize the adjacent list with a
// null array
this.AdjList.set(v, [])
}
// add edge to the graph
addEdge(v, w)
{
// get the list for vertex v and put the
// vertex w denoting edge between v and w
this.AdjList.get(v).push(w);
// Since graph is undirected,
// add an edge from w to v also
this.AdjList.get(w).push(v);
}
// add edge to the graph
addEdge(v, w)
{
// get the list for vertex v and put the
// vertex w denoting edge between v and w
this.AdjList.get(v).push(w)
// Prints the vertex and adjacency list
printGraph()
{
// get all the vertices
var get_keys = this.AdjList.keys();
// iterate over the vertices
for (var i of get_keys)
{
// great the corresponding adjacency list
// for the vertex
var get_values = this.AdjList.get(i);
var conc = "";
// iterate over the adjacency list
// concatenate the values into a string
for (var j of get_values)
conc += j + " ";
// print the vertex and its adjacency list
console.log(i + " -> " + conc);
}
// Since graph is undirected,
// add an edge from w to v also
this.AdjList.get(w).push(v)
}
// Prints the vertex and adjacency list
printGraph()
{
// get all the vertices
var get_keys = this.AdjList.keys()
// iterate over the vertices
for (var i of get_keys) {
// great the corresponding adjacency list
// for the vertex
var get_values = this.AdjList.get(i)
var conc = ''
// iterate over the adjacency list
// concatenate the values into a string
for (var j of get_values) { conc += j + ' ' }
// print the vertex and its adjacency list
console.log(i + ' -> ' + conc)
}
}
// Example
var graph = new Graph(6);
var vertices = [ 'A', 'B', 'C', 'D', 'E', 'F' ];
// adding vertices
for (var i = 0; i < vertices.length; i++) {
g.addVertex(vertices[i]);
}
// adding edges
g.addEdge('A', 'B');
g.addEdge('A', 'D');
g.addEdge('A', 'E');
g.addEdge('B', 'C');
g.addEdge('D', 'E');
g.addEdge('E', 'F');
g.addEdge('E', 'C');
g.addEdge('C', 'F');
// prints all vertex and
// its adjacency list
// A -> B D E
// B -> A C
// C -> B E F
// D -> A E
// E -> A D F C
// F -> E C
g.printGraph();
var graph = new Graph(6)
var vertices = ['A', 'B', 'C', 'D', 'E', 'F']
// adding vertices
for (var i = 0; i < vertices.length; i++) {
g.addVertex(vertices[i])
}
// adding edges
g.addEdge('A', 'B')
g.addEdge('A', 'D')
g.addEdge('A', 'E')
g.addEdge('B', 'C')
g.addEdge('D', 'E')
g.addEdge('E', 'F')
g.addEdge('E', 'C')
g.addEdge('C', 'F')
// prints all vertex and
// its adjacency list
// A -> B D E
// B -> A C
// C -> B E F
// D -> A E
// E -> A D F C
// F -> E C
g.printGraph()