How to detect if a number is even

68 points · 20 comments · view on lemmy.world

20 Comments

jeff@programming.dev · 14 pts · 3y (1 reply)
bool isEven(int num) {
   return !isOdd(num);
}

bool isOdd(int num) {
   return !isEven(num);
}
spi@programming.dev · 1 pts · 3y

Legends say this code runs faster than what the op posted. StackOverflow Errors are surprisingly fast in most languages

kane@programming.dev · 10 pts · 3y (2 replies)

I always figured this was a nice joke but obviously not code that would ever actually be written by someone... Then I ran into this millimeter to inch conversion code in production this past month:

if (isNaN(mm)) return 0;
if (mm == 6) {
    inch = 0.125;
}
else if (mm == 8) inch = 0.25;
else if (mm == 10) inch = 0.375;
else if (mm == 15) inch = 0.5;
else if (mm == 20) inch = 0.75;
else if (mm == 25) inch = 1;
else if (mm == 30) inch = 1.25;
else if (mm == 40) inch = 1.5;
else if (mm == 50) inch = 2;
else if (mm == 60) inch = 2.5;
else if (mm == 80) inch = 3;
else if (mm == 90) inch = 3.5;
else if (mm == 100) inch = 4;
...
ZILtoid1991@kbin.social · 2 pts · 3y (1 reply)

Even worse is, that the accurate conversion to one inch is 25.4mm.

Beanie@programming.dev · 2 pts · 3y

even worse, the first if statement randomly has brackets while none of the others do

lowleveldata@programming.dev · 7 pts · 3y

Good solution! I think you should show the last 3 lines that makes it work tho. FIFY:

private bool IsEven(int number){
    if (number == 1) return false;
    else if (number == 2) return true;
    else if (number == 0) return true;
    else if (number == -1) return false;
    else return !IsEven(abs(number) - 1);
}
beaker@programming.dev · 7 pts · 3y

That's terrible! Didn't we all learn that each method must have exactly one return statement? Please refactor to use a return variable and a single return. And get off my lawn!

Buttons@programming.dev · 7 pts · 3y (4 replies)

I've actually seen this type of code produced by a human-being who was trying to write good code. It was one of the students in my introduction to programming class in university, we had to write a function that squared a number or something, and he had written hundreds of lines of if-statements. Sometimes you just use what you know to complete an assignment I guess 🤷

msage@programming.dev · 5 pts · 3y

Though I want to add this case for interview questions: "Write code that outputs every prime number smaller than 10."

And if the candidate doesn't do 'print "2,3,5,7";', I will deduct points.

sisyphean@programming.dev · 2 pts · 3y (1 reply)

Wow… did he not know about the multiplication operator?

Buttons@programming.dev · 6 pts · 3y

Apparently not. It was very strange. Although it was the first few days of class and he might not have realized * is multiplication, because when does a non-programmer ever use * for multiplication?

bufordt@sh.itjust.works · 1 pts · 3y

I did something similar for a programming competition once because I couldn't remember the c64 basic function to return string length.

Once I got home I rewrote it properly because it bugged me so badly. LEN(string variable) was the command. Stupid!

danda@waveform.social · 4 pts · 3y

Not sure why people still implement this themselves when there are APIs that will do it for you, like https://isevenapi.xyz/

mamarguerat@discuss.tchncs.de · 3 pts · 3y
private void GenerateCode(){
  println("private bool IsEven(int number){");
  println("if (number == 1) return false");
  for(int i == 2; true; i += 2){
    println("if (number == " + i.tostring() + " return true";
    println("if (number == " + (i + 1).tostring() + " return false";
  }
}
fizgigtiznalkie@lemmynsfw.com · 2 pts · 3y

Those companies that judge output by lines of code are asking for this

ZILtoid1991@kbin.social · 2 pts · 3y

When you're paid by line...

bikesarethefuture@programming.dev · 2 pts · 3y (1 reply)

You missed some cases...

jeff@programming.dev · 10 pts · 3y

All my tests pass

SkullHex2@lemmy.world · 1 pts · 3y
[ removed ]