this post was submitted on 01 Nov 2023
910 points (98.2% liked)

Lemmy Shitpost

26804 readers
2861 users here now

Welcome to Lemmy Shitpost. Here you can shitpost to your hearts content.

Anything and everything goes. Memes, Jokes, Vents and Banter. Though we still have to comply with lemmy.world instance rules. So behave!


Rules:

1. Be Respectful


Refrain from using harmful language pertaining to a protected characteristic: e.g. race, gender, sexuality, disability or religion.

Refrain from being argumentative when responding or commenting to posts/replies. Personal attacks are not welcome here.

...


2. No Illegal Content


Content that violates the law. Any post/comment found to be in breach of common law will be removed and given to the authorities if required.

That means:

-No promoting violence/threats against any individuals

-No CSA content or Revenge Porn

-No sharing private/personal information (Doxxing)

...


3. No Spam


Posting the same post, no matter the intent is against the rules.

-If you have posted content, please refrain from re-posting said content within this community.

-Do not spam posts with intent to harass, annoy, bully, advertise, scam or harm this community.

-No posting Scams/Advertisements/Phishing Links/IP Grabbers

-No Bots, Bots will be banned from the community.

...


4. No Porn/ExplicitContent


-Do not post explicit content. Lemmy.World is not the instance for NSFW content.

-Do not post Gore or Shock Content.

...


5. No Enciting Harassment,Brigading, Doxxing or Witch Hunts


-Do not Brigade other Communities

-No calls to action against other communities/users within Lemmy or outside of Lemmy.

-No Witch Hunts against users/communities.

-No content that harasses members within or outside of the community.

...


6. NSFW should be behind NSFW tags.


-Content that is NSFW should be behind NSFW tags.

-Content that might be distressing should be kept behind NSFW tags.

...

If you see content that is a breach of the rules, please flag and report the comment and a moderator will take action where they can.


Also check out:

Partnered Communities:

1.Memes

2.Lemmy Review

3.Mildly Infuriating

4.Lemmy Be Wholesome

5.No Stupid Questions

6.You Should Know

7.Comedy Heaven

8.Credible Defense

9.Ten Forward

10.LinuxMemes (Linux themed memes)


Reach out to

All communities included on the sidebar are to be made in compliance with the instance rules. Striker

founded 1 year ago
MODERATORS
 
all 50 comments
sorted by: hot top controversial new old
[–] Rentlar@lemmy.ca 106 points 1 year ago* (last edited 1 year ago) (2 children)

They call me a StackOverflow expert:

private bool isEven(int num) {
if (num == 0) return true;
if (num == 1) return false;
if (num < 0) return isEven(-1 * num);
return isEven(num - 2);
}
[–] Johanno@feddit.de 16 points 1 year ago* (last edited 1 year ago)

StackoverflowException.

What do I do now?

Nvm. Got it.

  if(num % 2 == 0){
       int num1 = num/2
       int num2 = num/2
       return isEven(num1) && isEven(num2)   
  } 

if(num % 3 == 0){
      int num1 = num/3
      int num2 = num/3
      int num3 = num/3
      return isEven(num1) && isEven(num2) && isEven(num3) 
}

Obviously we need to check each part of the division to make sure if they are even or not. /s

[–] callyral@pawb.social 2 points 1 year ago

...a recursive is-even

wow

[–] Skyline969@lemmy.ca 68 points 1 year ago* (last edited 1 year ago)

Wow. Amateur hour over here. There's a much easier way to write this.

A case select:

select(number){
    case 1:
        return false;
    case 2:
        return true;
}

And so on.

[–] lobut@lemmy.ca 45 points 1 year ago (1 children)

Just do a while loop and subtract 2 if it's positive or plus 2 is it's negative until it reaches 1 or 0 and that's how you know, easy! /s

[–] KoboldCoterie@pawb.social 47 points 1 year ago

God, it's so obvious, you can do it in only two lines of code.

if (number == 1 || number == 3 || number == 5 || number == 7 || number == 9...) return false;
else return true;
[–] Enkers@sh.itjust.works 41 points 1 year ago* (last edited 1 year ago) (1 children)

This is your brain on python:

def is_even (num):
     return num in [x*2 for x in range(sys.maxsize / 2)]
[–] goddard_guryon@sopuli.xyz 9 points 1 year ago

That won't work tho, you need to make it sys.maxsize//2 to coerce the output into int form

[–] ParsnipWitch@feddit.de 40 points 1 year ago (1 children)

The number of comments posting a better solution is funny and somewhat concerning.

[–] elauso@feddit.de 23 points 1 year ago

Yeah, "just use modulo" - no shit, you must be some kind of master programmer

[–] rollerbang@sopuli.xyz 32 points 1 year ago

You have to make it easy on yourself and just use a switch with default true for evens, then gandle all the odd numbers in individual cases. There, cut your workload in half.

[–] Karyoplasma@discuss.tchncs.de 28 points 1 year ago
while (true){
    if (number == 0) return true;
    if (number == 1) return false;
    number -= 2
}
[–] CancerMancer@sh.itjust.works 27 points 1 year ago

Because YandereDev is a legendary moron I can't even tell if this is a joke or not.

[–] levi@aussie.zone 24 points 1 year ago (3 children)

Oh man, in js we have a package for this magic.

[–] Floey@lemm.ee 4 points 1 year ago

I always forget if is even requires is odd or the other way around.

[–] gandalf_der_12te@feddit.de 2 points 1 year ago

Weekly Downloads: 293.319

[–] gkd@lemmy.ml 2 points 1 year ago

Look at the downloads though!

[–] BeigeAgenda@lemmy.ca 23 points 1 year ago (1 children)

Good job my young padawan, let me teach you about the modulo operator ...

[–] mryessir@lemmy.sdf.org -1 points 1 year ago (1 children)

Actually the modulo operator is the wrong solution.

[–] BeigeAgenda@lemmy.ca 26 points 1 year ago* (last edited 1 year ago) (1 children)

No its not the wrong solution! Premature optimization is a waste of time.

Using if or case are not a solution because they are way too verbose and very easy to introduce an error.

Modulo is a solution, and using bit-wise and is another faster solution.

[–] Iceman@lemmy.ca 18 points 1 year ago

OMG they can’t even!

[–] callyral@pawb.social 17 points 1 year ago (1 children)

number == 0 is not handled

[–] trafficnab@lemmy.ca 6 points 1 year ago* (last edited 1 year ago)

You can't see it but there's an "else return true;" at the bottom

[–] Scubus@sh.itjust.works 15 points 1 year ago (1 children)

string taco = variable.ToString()[variable.ToString().Length - 1];

If (taco == "0" || taco == "2" || taco == "4" || taco == "6" || taco == "8")

return true;

else

return false;

Im something of a coding master myself

[–] where_am_i@sh.itjust.works 2 points 1 year ago

as division is complicated and expensive depending on the size of the numbers you'd usually receive as an input, this could be the most efficient solution. Certainly could have the best worst case if we imagine some 128bit shenanigans.

[–] luthis@lemmy.nz 14 points 1 year ago

Ok looks like this might be the source, and I suspect it is actually satirical. Not yanderedev, but yeah... wouldn't put it past him.

https://www.reddit.com/r/ProgrammerHumor/comments/i15h4d/iseven/

We're too swamped for that kind of thinking. Just keep typing or we'll never make our release window

[–] jsdz@lemmy.ml 12 points 1 year ago* (last edited 1 year ago) (1 children)
int is_even(int n)
{
    int result = -1;
    char number[8]; //should be enough
    sprintf(number, "%d", n);

    // check the number
    // TODO: handle negative numbers
    for (char *p=number; *p; p++)
    {
        if (*p=='0' || *p=='2' || *p=='4' || *p=='6' || *p=='8')
            result = 1;
        else if (*p=='1' || *p=='3' || *p=='5' || *p=='7' || *p=='9')
            result = 0;
        else
           sprintf(stderr, "Your number is wrong!\n");
           exit(1); 
    }
    return result;
}
[–] AnxiousDuck@feddit.it 2 points 1 year ago

This should be the accepted answer

[–] Zaphod@discuss.tchncs.de 11 points 1 year ago (2 children)
[–] luthis@lemmy.nz 14 points 1 year ago

From what I've heard about yanderedev... it's possible this is actually real.

[–] Bratwurstboy@iusearchlinux.fyi 10 points 1 year ago (1 children)

It is and it has become a meme since then. Just search for Yandere simulator / Yanderedev.

[–] Zaphod@discuss.tchncs.de 7 points 1 year ago* (last edited 1 year ago) (1 children)

I've heard about his notoriously bad code, but I didn't think it was to this extent...

[–] Robmart@lemm.ee 7 points 1 year ago

It gets worse. I am not kidding.

[–] original_reader@lemm.ee 8 points 1 year ago* (last edited 1 year ago) (2 children)

Recently there was a thread trying to declare PHP obsolete.

Hard to beat this in efficiency:

function is_even($num) {
    return $num % 2 === 0;
}

That said, this should work similarly in most languages.

[–] TheBlue22@lemmy.blahaj.zone 16 points 1 year ago

Except maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms

[–] jmcs@discuss.tchncs.de 5 points 1 year ago

If the language you are using uses "real" integers, using a bit mask to get the least significant bit is probably a lot faster - assuming the compiler doesn't replace the operation for you, in which case it doesn't matter.

[–] wtry@lemm.ee 6 points 1 year ago* (last edited 1 year ago) (1 children)

I can't believe he needs that much code for this: bool iseven(int number){ if (number % 2 == 0){ return true; } else { return false; } }

[–] TopRamenBinLaden@sh.itjust.works 17 points 1 year ago (1 children)

I like the example in the post better. It is more clear as to what is going on to an experienced dev like me. What's this 2 percent nonsense?

[–] filcuk@lemmy.zip 3 points 1 year ago (1 children)

Readability over obscure hacks

[–] rustbuckett@lemmings.world 2 points 1 year ago

It's not obscure. This is the example, with syntactic differences, for this problem in almost every programming book I've read. He just didn't include newlines.

[–] superpants@lemm.ee 4 points 1 year ago

I did something like this in high school

[–] pinkdrunkenelephants@lemmy.cafe -1 points 1 year ago

Modulus equals zero anyone?