• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const kMeans = (data, k = 1) => { const centroids = data.slice(0, k); const distances = Array.from({ length: data.length }, () => Array.from({ length: k }, () => 0) ); const classes = Array.from({ length: data.length }, () => -1); let itr = true; while (itr) { itr = false; for (let d in data) { for (let c = 0; c < k; c++) { distances[d][c] = Math.hypot( ...Object.keys(data[0]).map(key => data[d][key] - centroids[c][key]) ); } const m = distances[d].indexOf(Math.min(...distances[d])); if (classes[d] !== m) itr = true; classes[d] = m; } for (let c = 0; c < k; c++) { centroids[c] = Array.from({ length: data[0].length }, () => 0); const size = data.reduce((acc, _, d) => { if (classes[d] === c) { acc++; for (let i in data[0]) centroids[c][i] += data[d][i]; } return acc; }, 0); for (let i in data[0]) { centroids[c][i] = parseFloat(Number(centroids[c][i] / size).toFixed(2)); } } } return classes; }; kMeans([[0, 0], [0, 1], [1, 3], [2, 0]], 2); // [0, 1, 1, 0]
0 likes • 3 views
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k])); euclideanDistance([1, 1], [2, 3]); // ~2.2361 euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
const compactObject = val => { const data = Array.isArray(val) ? val.filter(Boolean) : val; return Object.keys(data).reduce( (acc, key) => { const value = data[key]; if (Boolean(value)) acc[key] = typeof value === 'object' ? compactObject(value) : value; return acc; }, Array.isArray(val) ? [] : {} ); }; const obj = { a: null, b: false, c: true, d: 0, e: 1, f: '', g: 'a', h: [null, false, '', true, 1, 'a'], i: { j: 0, k: false, l: 'a' } }; compactObject(obj); // { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }
function Welcome(props) { return <h1>Hello, {props.name}</h1>; } function App() { return ( <div> <Welcome name="Sara" /> <Welcome name="Cahal" /> <Welcome name="Edite" /> </div> ); } ReactDOM.render( <App />, document.getElementById('root') );
• Nov 18, 2022 •AustinLeath
0 likes • 4 views
"use strict"; const nodemailer = require("nodemailer"); // async..await is not allowed in global scope, must use a wrapper async function main() { // Generate test SMTP service account from ethereal.email // Only needed if you don't have a real mail account for testing let testAccount = await nodemailer.createTestAccount(); // create reusable transporter object using the default SMTP transport let transporter = nodemailer.createTransport({ host: "smtp.ethereal.email", port: 587, secure: false, // true for 465, false for other ports auth: { user: testAccount.user, // generated ethereal user pass: testAccount.pass, // generated ethereal password }, }); // send mail with defined transport object let info = await transporter.sendMail({ from: '"Fred Foo 👻" <[email protected]>', // sender address to: "[email protected], [email protected]", // list of receivers subject: "Hello ✔", // Subject line text: "Hello world?", // plain text body html: "<b>Hello world?</b>", // html body }); console.log("Message sent: %s", info.messageId); // Message sent: <[email protected]> // Preview only available when sending through an Ethereal account console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou... } main().catch(console.error);