• 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)
• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const unzipWith = (arr, fn) => arr .reduce( (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc), Array.from({ length: Math.max(...arr.map(x => x.length)) }).map(x => []) ) .map(val => fn(...val)); unzipWith( [ [1, 10, 100], [2, 20, 200], ], (...args) => args.reduce((acc, v) => acc + v, 0) ); // [3, 30, 300]
• Oct 15, 2023 •LeifMessinger
0 likes • 7 views
//Disclaimer: I do not condone mass web scraping of ESPN websites, and this is just theoretical code that hasn't been used. //Scrape player stats off of https://www.espn.com/nfl/team/roster/_/name/phi/philadelphia-eagles //Example player JS object /* { "shortName": "S. Opeta", "name": "Sua Opeta", "href": "http://www.espn.com/nfl/player/_/id/3121009/sua-opeta", "uid": "s:20~l:28~a:3121009", "guid": "dec19157-e984-f981-3724-498281328c97", "id": "3121009", "height": "6' 4\"", "weight": "305 lbs", "age": 27, "position": "G", "jersey": "78", "birthDate": "08/15/96", "headshot": "https://a.espncdn.com/i/headshots/nfl/players/full/3121009.png", "lastName": "Sua Opeta", "experience": 4, "college": "Weber State" } */ //The page needs to be focused, so you have to put this code in a bookmarklet and click the page before clicking it. Allegedly. navigator.clipboard.writeText(JSON.stringify( window["__espnfitt__"].page.content.roster.groups.flatMap((group)=>{ return group.athletes.map((athlete)=>{ //We can assume all football players are above 100 lbs and below 1000 lbs. athlete.weight = parseInt(athlete.weight.substring(0,3)); if(athlete.experience == "R") athlete.experience = 0; else athlete.experience = parseInt(athlete.experience); //We can assume players are at least 1 foot, or under 10 feet tall. athlete.inches = 12 * parseInt(athlete.height.substring(0, 1)); //Add feet in inches athlete.inches += parseInt(athlete.height.substring(2).replaceAll("\"", "").trim()); //Add remaining inches const monthDayYear = athlete.birthDate.split("/"); athlete.birthMonth = parseInt(monthDayYear[0]); athlete.birthDay = parseInt(monthDayYear[1]); athlete.birthYear = parseInt(monthDayYear[2]); //The only really useful stuff we get from this is Height, weight, left handedness, age, position, and birthday return athlete; }); }) , null, "\t")).then(null, ()=>{alert("That failed.")}); //Changes all the team links on this page https://www.espn.com/nfl/stats/team to the team's roster for easier scraping $$("table > tbody > tr > td > div > div > a").forEach((elm)=>{ elm.setAttribute("href", elm.getAttribute("href").replace("team/", "team/roster/")); });
const bucketSort = (arr, size = 5) => { const min = Math.min(...arr); const max = Math.max(...arr); const buckets = Array.from( { length: Math.floor((max - min) / size) + 1 }, () => [] ); arr.forEach(val => { buckets[Math.floor((val - min) / size)].push(val); }); return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []); }; bucketSort([6, 3, 4, 1]); // [1, 3, 4, 6]
//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}`);
const quickSort = arr => { const a = [...arr]; if (a.length < 2) return a; const pivotIndex = Math.floor(arr.length / 2); const pivot = a[pivotIndex]; const [lo, hi] = a.reduce( (acc, val, i) => { if (val < pivot || (val === pivot && i != pivotIndex)) { acc[0].push(val); } else if (val > pivot) { acc[1].push(val); } return acc; }, [[], []] ); return [...quickSort(lo), pivot, ...quickSort(hi)]; }; quickSort([1, 6, 1, 5, 3, 2, 1, 4]); // [1, 1, 1, 2, 3, 4, 5, 6]