• 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]
• 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)
0 likes • 2 views
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32; const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9; // Examples celsiusToFahrenheit(15); // 59 celsiusToFahrenheit(0); // 32 celsiusToFahrenheit(-20); // -4 fahrenheitToCelsius(59); // 15 fahrenheitToCelsius(32); // 0
//JavaScript program to swap two variables //take input from the users let a = prompt('Enter the first variable: '); let b = prompt('Enter the second variable: '); //using destructuring assignment [a, b] = [b, a]; console.log(`The value of a after swapping: ${a}`); console.log(`The value of b after swapping: ${b}`);
• Feb 11, 2021 •LeifMessinger
function spam(times = 1,log = true){ for(let i = 0; i < times; i++){ $.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){ if(log) console.log(data); }); } } spam(100000);
• Jan 25, 2023 •C S
0 likes • 23 views
const ResponsiveCardImg = styled(Card.Img)` // For screens wider than 768px @media(min-width: 768px) { // Your responsive styles here. } // For screens smaller than 768px @media(max-width: 768px) { // Your responsive styles here. } ` export default function App() { return ( <MainContainer> <Container> <Card text='white' bg='dark' style={styles.mainCard}> <ResponsiveCardImg style={styles.cardImage} onClick={handleClick} src={apeImage} alt='Degenerate Ape Academy' /> <Card.Body> <Card.Title className='text-center' as='h2'> {apeId} </Card.Title> </Card.Body> </Card> </Container> </MainContainer> ); }