40
27
25
19
10
u/Square789 int mystery=*((int*)rand()) May 05 '20
The real sin here is that weird colon-placement.
3
9
u/PedroPossato May 05 '20
return n!=abs(n)
1
1
u/TheMightyBiz May 06 '20
Zero tho
2
u/ethanjf99 May 06 '20
return n && n != Math.abs(n); // JavaScript2
May 06 '20
const isNegativeImpl = require('is-negative'); function isNegative(n) { return isNegativeImpl(n); }
19
6
u/Mexenstein May 05 '20
Why not return math.sqrt(n*n) == n?
9
1
3
7
u/Pooneapple May 05 '20
return n < 0
13
u/Dubmove May 05 '20
We don't do this here.
3
u/Pooneapple May 05 '20
return ( n ** 2 ) > 0?
1
2
u/Plus_Cryptographer May 06 '20
I think you should throw an error on
n == 0just so you have a triple state of positive, negative and 0.
2
2
u/KingErroneous May 06 '20
Wait until python can handle complex numbers...
6
u/Nebuli2 May 06 '20
Python does handle complex numbers. It's honestly a little bit weird that
math.sqrtdoesn't handle them.-4 ** 0.5returns 2j, for example.
1
1
1
u/OrionSoul May 06 '20
i've stared at the code for about 5 minutes and can't see what's wrong with it, and now i'm too afraid to ask
4
u/neotecha May 06 '20
What's the definition of "negative"? It's "any number less than zero", right?
Consider this function:
def is_negative(n): return n < 0That's it. Nothing more is needed.
Instead, while the function in the picture works, it goes about the problem in an illogical way. Try-Except blocks should be reserved to error handling, and handling logic like this is a definite code smell
3
1
u/crispybacon_NZ May 17 '20
Try-except blocks should be reserved for error handling, but it's more Pythonic: "easier to ask forgiveness than get permission" or something.
Dang near every code review, somebody will argue about this....
1
u/neotecha May 18 '20
I can't agree that it's more pythonic. Relying on a behavior edge case breaks the "Explicit is better than implicit" rule.
Dang near every code review, somebody will argue about this....
I don't disagree
1
1
1
1
1
82
May 05 '20
[removed] — view removed comment
5
u/RFC793 May 05 '20 edited May 05 '20
Sure. But what if you are passed a string with double, triple, etc negatives? Clearly we need something like this:
len(list(itertools.takewhile(lambda c: c=='-', str(n))))%2 == 1However, in production code, surely you want to replace the modulo with:
def isOdd(n): return str(n)[-1] in (1,3,5,7,9)0
145
u/[deleted] May 05 '20
For that time when you want all strings to count as negative.