• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const insertionSort = arr => arr.reduce((acc, x) => { if (!acc.length) return [x]; acc.some((y, j) => { if (x <= y) { acc.splice(j, 0, x); return true; } if (x > y && j === acc.length - 1) { acc.splice(j + 1, 0, x); return true; } return false; }); return acc; }, []); insertionSort([6, 3, 4, 1]); // [1, 3, 4, 6]
• Nov 18, 2022 •AustinLeath
0 likes • 2 views
import Head from 'next/head' import Image from 'next/image' import styles from '../styles/Home.module.css' const { exec } = require("child_process"); exec("sensors|grep "high"|grep "Core"|cut -d "+" -f2|cut -d "." -f1|sort -nr|sed -n 1p", (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); return; } if (stderr) { console.log(`stderr: ${stderr}`); return; } console.log(`stdout: ${stdout}`); }); export default function Home() { return ( <div className={styles.container}> <Head> <title>Create Next App</title> <meta name="description" content="Generated by create next app" /> <link rel="icon" href="/favicon.ico" /> </Head> <main className={styles.main}> <h1 className={styles.title}> Welcome to <a href="https://nextjs.org">Next.js!</a> </h1> <p className={styles.description}> Get started by editing{' '} <code className={styles.code}>pages/index.js</code> </p> <div className={styles.grid}> <a href="https://nextjs.org/docs" className={styles.card}> <h2>Documentation →</h2> <p>Find in-depth information about Next.js features and API.</p> </a> <a href="https://nextjs.org/learn" className={styles.card}> <h2>Learn →</h2> <p>Learn about Next.js in an interactive course with quizzes!</p> </a> <a href="https://github.com/vercel/next.js/tree/canary/examples" className={styles.card} > <h2>Examples →</h2> <p>Discover and deploy boilerplate example Next.js projects.</p> </a> <a href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} > <h2>Deploy →</h2> <p> Instantly deploy your Next.js site to a public URL with Vercel. </p> </a> </div> </main> <footer className={styles.footer}> <a href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" target="_blank" rel="noopener noreferrer" > Powered by{' '} <span className={styles.logo}> <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} /> </span> </a> </footer> </div> ) }
const longestPalindrome = (s) => { if(s.length === 1) return s const odd = longestOddPalindrome(s) const even = longestEvenPalindrome(s) if(odd.length >= even.length) return odd if(even.length > odd.length) return even }; const longestOddPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 1; i < s.length; i++) { let currentLongest = 1 let currentLongestSubStr = "" let left = i - 1 let right = i + 1 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] longest = currentLongest } } return longestSubStr } const longestEvenPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 0; i < s.length - 1; i++) { if(s[i] !== s[i+1]) continue; let currentLongest = 2 let currentLongestSubStr = "" let left = i - 1 let right = i + 2 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] + s[i+1] longest = currentLongest } } return longestSubStr } console.log(longestPalindrome("babad"))
• Jan 26, 2023 •AustinLeath
0 likes • 6 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)
arr = [] // empty array const isEmpty = arr => !Array.isArray(arr) || arr.length === 0; // Examples isEmpty([]); // true isEmpty([1, 2, 3]); // false
const shuffle = ([...arr]) => { let m = arr.length; while (m) { const i = Math.floor(Math.random() * m--); [arr[m], arr[i]] = [arr[i], arr[m]]; } return arr; }; const foo = [1, 2, 3]; shuffle(foo); // [2, 3, 1], foo = [1, 2, 3]