Last active
August 9, 2020 02:56
-
-
Save crazy4groovy/2f9423ee8c688a224b9650b4a5678bec to your computer and use it in GitHub Desktop.
Count how many times a carry operation is performed while adding two numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numberOfCarryOperations(a, b) { | |
const aDigits = String(a).split('').reverse() | |
const bDigits = String(b).split('').reverse() | |
const maxDigits = aDigits.length > bDigits.length ? aDigits : bDigits | |
const minDigits = maxDigits === aDigits ? bDigits : aDigits | |
/// console.log({a, b, maxDigits, minDigits}) | |
let carry = 0 | |
const carryCount = maxDigits.reduce((c, d, i) => { | |
const dA = Number(d) || 0 | |
const dB = Number(minDigits[i]) || 0 | |
let r = carry + dA + dB | |
carry = 0 | |
if (r >= 10) { | |
/// r -= 10 | |
c += 1 | |
carry = 1 | |
} | |
/// console.log({dA, dB, c, r}) | |
return c | |
}, 0) | |
return carryCount | |
} | |
console.log(numberOfCarryOperations(123, 456)) // 0 | |
console.log(numberOfCarryOperations(555, 555)) // 3 | |
console.log(numberOfCarryOperations(900, 11)) // 0 | |
console.log(numberOfCarryOperations(145, 55)) // 2 | |
console.log(numberOfCarryOperations(0, 0)) // 0 | |
console.log(numberOfCarryOperations(1, 99999)) // 5 | |
console.log(numberOfCarryOperations(999045, 1055)) // 5 | |
console.log(numberOfCarryOperations(101, 809)) // 1 | |
console.log(numberOfCarryOperations(189, 209)) // 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toDigit = d => Number(d) || 0 | |
function numberOfCarryOperations(numbs) { | |
numbs = numbs.map(n => String(n).split('').reverse()) | |
const maxDigitsLen = Math.max(...numbs.map(n => n.length)) | |
const maxDigitsIdx = numbs.findIndex(n => n.length === maxDigitsLen) | |
const maxDigits = numbs.splice(maxDigitsIdx, 1)[0] | |
/// console.log({maxDigits, numbs}) | |
let carry = 0 // first carry | |
const carryCount = maxDigits.reduce((c, d, i) => { | |
let r = carry + toDigit(d) + numbs.reduce((t, n) => t + toDigit(n[i]), 0) | |
carry = 0 // next carry | |
while (r - 10 >= 0) { | |
r -= 10 | |
c += 1 | |
carry += 1 | |
} | |
/// console.log({c, carry, r}) | |
return c | |
}, 0) | |
return carryCount | |
} | |
console.log(numberOfCarryOperations([123, 456, 0])) // 0 | |
console.log(numberOfCarryOperations([123, 456, 999])) // 3 | |
console.log(numberOfCarryOperations([555, 555])) // 3 | |
console.log(numberOfCarryOperations([900, 11])) // 0 | |
console.log(numberOfCarryOperations([145, 55])) // 2 | |
console.log(numberOfCarryOperations([0, 0])) // 0 | |
console.log(numberOfCarryOperations([1, 99999])) // 5 | |
console.log(numberOfCarryOperations([999045, 1055])) // 5 | |
console.log(numberOfCarryOperations([101, 809])) // 1 | |
console.log(numberOfCarryOperations([189, 209])) // 1 | |
console.log(numberOfCarryOperations([999, 999, 999])) // 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function digitForIdx(num, i) { | |
return Math.floor((num / Math.pow(10, i)) % 10) | |
} | |
function numberOfCarryOperations(a, b) { | |
const maxLen = String(Math.max(a, b)).length | |
const carry = [0] | |
for (let i = 0; i < maxLen; i++) { | |
const r = carry[i] + digitForIdx(a, i) + digitForIdx(b, i) | |
carry[i + 1] = ((r >= 10) ? 1 : 0) | |
} | |
return carry.filter(c => c).length | |
} | |
console.log(numberOfCarryOperations(123, 456)) // 0 | |
console.log(numberOfCarryOperations(555, 555)) // 3 | |
console.log(numberOfCarryOperations(900, 11)) // 0 | |
console.log(numberOfCarryOperations(145, 55)) // 2 | |
console.log(numberOfCarryOperations(0, 0)) // 0 | |
console.log(numberOfCarryOperations(1, 99999)) // 5 | |
console.log(numberOfCarryOperations(999045, 1055)) // 5 | |
console.log(numberOfCarryOperations(101, 809)) // 1 | |
console.log(numberOfCarryOperations(189, 209)) // 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toDigit(a) { return Number(a) || 0 } | |
function numberOfCarryOperations(a, b) { | |
a = String(a).split('').reverse() | |
b = String(b).split('').reverse() | |
const maxLen = Math.max(a.length, b.length) | |
const carry = [] | |
for (var i = 0; i < maxLen; i++) { | |
// console.log(toDigit(a[i]), toDigit(b[i]), toDigit(carry[i-1])) | |
carry[i] = (toDigit(a[i]) + toDigit(b[i]) + toDigit(carry[i-1])) > 9 | |
? 1 | |
: 0 | |
} | |
return carry.filter(v => v).length | |
} | |
console.log(numberOfCarryOperations(123, 456)) // 0 | |
console.log(numberOfCarryOperations(555, 555)) // 3 | |
console.log(numberOfCarryOperations(900, 11)) // 0 | |
console.log(numberOfCarryOperations(145, 55)) // 2 | |
console.log(numberOfCarryOperations(0, 0)) // 0 | |
console.log(numberOfCarryOperations(1, 99999)) // 5 | |
console.log(numberOfCarryOperations(999045, 1055)) // 5 | |
console.log(numberOfCarryOperations(101, 809)) // 1 | |
console.log(numberOfCarryOperations(189, 209)) // 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment