Destide@feddit.uk to Programmer Humor@lemmy.mlEnglish · 4 months agoInfallible Codelemmy.mlimagemessage-square151fedilinkarrow-up1470arrow-down126
arrow-up1444arrow-down1imageInfallible Codelemmy.mlDestide@feddit.uk to Programmer Humor@lemmy.mlEnglish · 4 months agomessage-square151fedilink
minus-squareredxef@feddit.orglinkfedilinkarrow-up35·4 months agodef is_even(n: int) -> bool: if n < 0: return is_even(-n) r = True for _ in range(n): r = not r return r
minus-squareOddMinus1@sh.itjust.workslinkfedilinkarrow-up2·edit-24 months agoCould also be done recursive, I guess? boolean isEven(int n) { if (n == 0) { return true; } else { return !isEven(Math.abs(n - 1)); } }
minus-squarevandsjov@feddit.dklinkfedilinkarrow-up1·4 months agoNo, no, I would convert the number to a string and just check the last char to see if it was even or not.
def is_even(n: int) -> bool: if n < 0: return is_even(-n) r = True for _ in range(n): r = not r return rCould also be done recursive, I guess?
boolean isEven(int n) { if (n == 0) { return true; } else { return !isEven(Math.abs(n - 1)); } }deleted by creator
No, no, I would convert the number to a string and just check the last char to see if it was even or not.