409
submitted 2 months ago by sirico@feddit.uk to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] Aedis@lemmy.world 34 points 2 months ago

I'm partial to a recursive solution. Lol

def is_even(number):
    if number < 0 or (number%1) > 0:
        raise ValueError("This impl requires positive integers only") 
    if number < 2:
        return number
    return is_even(number - 2)
[-] tetris11@lemmy.ml 18 points 2 months ago* (last edited 2 months ago)

I prefer good ole regex test of a binary num

function isEven(number){
   binary=$(echo "obase=2; $number" | bc)
   if [ "${binary:-1}" = "1" ]; then
         return 255
   fi
   return 0
}
[-] barubary@infosec.exchange 5 points 2 months ago

@tetris11 @Aedis More like:

isEven() {    case "$1" in        *[02468]) return 0;;        *) return 1;;    esac;}

(If all the line breaks are gone from this code snippet, blame Lemmy. It looks fine here.)

load more comments (11 replies)
load more comments (11 replies)
this post was submitted on 15 Jul 2025
409 points (100.0% liked)

Programmer Humor

38588 readers
231 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS