• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length; hammingDistance(2, 3); // 1
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]
0 likes • 3 views
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') );
• 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"; } }
function Clock(props) { return ( <div> <h1>Hello, world!</h1> <h2>It is {props.date.toLocaleTimeString()}.</h2> </div> ); } function tick() { ReactDOM.render( <Clock date={new Date()} />, document.getElementById('root') ); } setInterval(tick, 1000);
• Feb 6, 2021 •LeifMessinger
0 likes • 2 views
class SequentialQueue{ //if you want it to go backwards, too bad next(){ return this.i++; } constructor(start = 0){ this.i = start; } } const que = new SequentialQueue(0); for(let i = 0; i < 10; i++){ console.log(que.next()); }