4 ms·
I'd rather take my chances with a snake than trying to solve "Add two integers without using + or -".
by riebschlager 10y ago
I'd rather take my chances with a snake than trying to solve "Add two integers without using + or -".
- brianwawok 10y agosum([5,6]) ?
- emidln 10y agoTraceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in one_true_addition_test_case AssertionError The "correct" answer according to my pet parrot is (5).__add__(6). Of course, as per real-life, "correct" is usually just an aspect of if you find the interviewer's solution instead of whether your solution is actually correct. Better luck next time!
- tedmiston 10y agoThe sum built-in is still using the addition operator with syntactic sugar though (as is the other child comment). I think the intent of this question is to solve it with bitwise operators ie, [1]. [1]: http://stackoverflow.com/questions/17342042/why-this-code-for-additionusing-bitwise-operation-works-in-java http://stackoverflow.com/questions/17342042/why-this-code-fo...
- jacquesm 10y agoIf you're going to have to argue about the 'intent of the question' then you've already lost. Really, the question should be 'add these two integers' and any solution that produces the result in a transparent and straightforward way should be honored with top marks. Trick questions, especially those where only the interviewers pet solution is permitted are a sure sign that this employer is best avoided because they care about form and ego more than they care about getting the bloody job done.
- soVeryTired 10y agolog(exp(5)*exp(6))? Guess that won't work too well with larger numbers...
- dr_zoidberg 10y agoIt breaks quite early: In [8]: math.log(math.exp(5000) * math.exp(6000)) --------------------------------------------------------------------------- OverflowError Traceback (most recent call last) <ipython-input-8-3d6e46c58c45> in <module>() ----> 1 math.log(math.exp(5000) * math.exp(6000)) OverflowError: math range error In [9]: math.log(math.exp(500) * math.exp(600)) Out[9]: inf
- brianwawok 10y agoHey let's not be a quitter >>> (Decimal(5000).exp() * Decimal(6000).exp()).ln() Decimal('11000.00000000000000000000000')
- dr_zoidberg 10y agoNice catch, but then: In [13]: Decimal(5000000).exp() * Decimal(5000000).exp() --------------------------------------------------------------------------- Overflow Traceback (most recent call last) <ipython-input-13-8333724ae893> in <module>() ----> 1 Decimal(5000000).exp() * Decimal(5000000).exp() Overflow: [<class 'decimal.Overflow'>] The problem is that the multiplication blows the precision of the used format (either double for math.log(...) or the one the decimal module uses). I had also thought about the intention of the question trying to get you to express the sum as bit operations (thought I admit that sounds like going for a very low-level profile), or maybe just "thinking outside the box".
- brianwawok 10y agoHey don't be a quitter, we can make Decimal support way bigger numbers via context
- deleted 10y ago[deleted]
- recursive 10y agoI couldn't resist. add = lambda a, b: add(a ^ b, (a & b) << 1) if b else a
- recursive 10y agoAnd if you need to handle negatives: def add(a, b): while a and b: if a ^ b < 0: (a, b) = (a & ~b, b & ~a) (a, b) = (a ^ b, (a & b) << 1) return a or b
- Humdeee 10y agoExcel to the rescue.
- mythrwy 10y agolen([i for s in ([i for i in range(x)], [i for i in range(y)]) for i in s])
- mythrwy 10y ago(as long as it doesn't need to be fast or do negative numbers)