Loading...
More JavaScript Posts
const MongoClient = require('mongodb').MongoClient;const assert = require('assert');// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst dbName = 'myproject';// Use connect method to connect to the serverMongoClient.connect(url, function(err, client) {assert.equal(null, err);console.log("Connected successfully to server");const db = client.db(dbName);client.close();});
var invertTree = function(root) {const reverseNode = node => {if (node == null) {return null}reverseNode(node.left);reverseNode(node.right);let holdLeft = node.left;node.left = node.right;node.right = holdLeft;return node;}return reverseNode(root);};
class Graph {constructor(directed = true) {this.directed = directed;this.nodes = [];this.edges = new Map();}addNode(key, value = key) {this.nodes.push({ key, value });}addEdge(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}removeNode(key) {this.nodes = this.nodes.filter(n => n.key !== key);[...this.edges.values()].forEach(({ a, b }) => {if (a === key || b === key) this.edges.delete(JSON.stringify([a, b]));});}removeEdge(a, b) {this.edges.delete(JSON.stringify([a, b]));if (!this.directed) this.edges.delete(JSON.stringify([b, a]));}findNode(key) {return this.nodes.find(x => x.key === key);}hasEdge(a, b) {return this.edges.has(JSON.stringify([a, b]));}setEdgeWeight(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}getEdgeWeight(a, b) {return this.edges.get(JSON.stringify([a, b])).weight;}adjacent(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc.push(b);return acc;}, []);}indegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (b === key) acc++;return acc;}, 0);}outdegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc++;return acc;}, 0);}}const g = new Graph();g.addNode('a');g.addNode('b');g.addNode('c');g.addNode('d');g.addEdge('a', 'c');g.addEdge('b', 'c');g.addEdge('c', 'b');g.addEdge('d', 'a');g.nodes.map(x => x.value); // ['a', 'b', 'c', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'c => b', 'd => a']g.adjacent('c'); // ['b']g.indegree('c'); // 2g.outdegree('c'); // 1g.hasEdge('d', 'a'); // trueg.hasEdge('a', 'd'); // falseg.removeEdge('c', 'b');[...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'd => a']g.removeNode('c');g.nodes.map(x => x.value); // ['a', 'b', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['d => a']g.setEdgeWeight('d', 'a', 5);g.getEdgeWeight('d', 'a'); // 5
const express = require('express')const app = express()const port = 3000app.get('/', (req, res) => {res.send('Hello World!')})app.listen(port, () => {console.log(`Example app listening on port ${port}`)})
export const Main = () => {const [title, setTitle] = useState('');return (<ChildComponenttitle={title}onChange={e => setTitle(e.target.value)}/>);};export const ChildComponent = ({ title, onChange }) => {return (<Box component="form" onSubmit={() => {}} noValidate mt={5}><TextFieldfullWidthid="title"label="Title"name="title"autoComplete="title"autoFocuscolor="primary"variant="filled"InputLabelProps={{ shrink: true }}value={title}onChange={onChange}/></Box>);};
const kNearestNeighbors = (data, labels, point, k = 3) => {const kNearest = data.map((el, i) => ({dist: Math.hypot(...Object.keys(el).map(key => point[key] - el[key])),label: labels[i]})).sort((a, b) => a.dist - b.dist).slice(0, k);return kNearest.reduce((acc, { label }, i) => {acc.classCounts[label] =Object.keys(acc.classCounts).indexOf(label) !== -1? acc.classCounts[label] + 1: 1;if (acc.classCounts[label] > acc.topClassCount) {acc.topClassCount = acc.classCounts[label];acc.topClass = label;}return acc;},{classCounts: {},topClass: kNearest[0].label,topClassCount: 0}).topClass;};const data = [[0, 0], [0, 1], [1, 3], [2, 0]];const labels = [0, 1, 1, 0];kNearestNeighbors(data, labels, [1, 2], 2); // 1kNearestNeighbors(data, labels, [1, 0], 2); // 0