• Nov 19, 2022 •CodeCatch
0 likes • 3 views
const linearSearch = (arr, item) => { for (const i in arr) { if (arr[i] === item) return +i; } return -1; }; linearSearch([2, 9, 9], 9); // 1 linearSearch([2, 9, 9], 7); // -1
• Mar 22, 2022 •LeifMessinger
0 likes • 1 view
//QM Helper by Leif Messinger //Groups numbers by number of bits and shows their binary representations. //To be used on x.com const minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim())); const maxNumBits = minterms.reduce(function(a, b) { return Math.max(a, Math.log2(b)); }); const bitGroups = []; for(let i = 0; i < maxNumBits; ++i){ bitGroups.push([]); } for(const minterm of minterms){ let outputString = (minterm+" "); //Count the bits let count = 0; for (var i = maxNumBits; i >= 0; --i) { if((minterm >> i) & 1){ ++count; outputString += "1"; }else{ outputString += "0"; } } bitGroups[count].push(outputString); } document.body.textContent = ""; document.body.style.setProperty("white-space", "pre"); for(const group of bitGroups){ for(const outputString of group){ document.body.textContent += outputString + "\r\n"; } }
0 likes • 2 views
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"))
• Sep 13, 2023 •C S
0 likes • 11 views
/** * @param {number[]} nums * @return {number[]} */ var productExceptSelf = function(nums) { var output = []; var leftMult = 1; var rightMult = 1; for (var i=nums.length - 1; i >= 0; i--) { output[i] = rightMult; rightMult *= nums[i]; console.log({output: JSON.stringify(output), i}) } for (var j=0; j < nums.length; j++) { output[j] *= leftMult; leftMult *= nums[j]; console.log({output: JSON.stringify(output), j}) } return output; }; console.log(productExceptSelf([1, 2, 3, 4]))
0 likes • 0 views
const heapsort = arr => { const a = [...arr]; let l = a.length; const heapify = (a, i) => { const left = 2 * i + 1; const right = 2 * i + 2; let max = i; if (left < l && a[left] > a[max]) max = left; if (right < l && a[right] > a[max]) max = right; if (max !== i) { [a[max], a[i]] = [a[i], a[max]]; heapify(a, max); } }; for (let i = Math.floor(l / 2); i >= 0; i -= 1) heapify(a, i); for (i = a.length - 1; i > 0; i--) { [a[0], a[i]] = [a[i], a[0]]; l--; heapify(a, 0); } return a; }; heapsort([6, 3, 4, 1]); // [1, 3, 4, 6]
const copyToClipboard = str => { const el = document.createElement('textarea'); el.value = str; el.setAttribute('readonly', ''); el.style.position = 'absolute'; el.style.left = '-9999px'; document.body.appendChild(el); const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false; el.select(); document.execCommand('copy'); document.body.removeChild(el); if (selected) { document.getSelection().removeAllRanges(); document.getSelection().addRange(selected); } }; copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.