Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
andioop ( andioop@programming.dev )  to Programming Horror@programming.devEnglish · 3 years ago

God I wish there was an easier way to do this

programming.dev

message-square
26
link
fedilink
117

God I wish there was an easier way to do this

programming.dev

andioop ( andioop@programming.dev )  to Programming Horror@programming.devEnglish · 3 years ago
message-square
26
link
fedilink
alert-triangle
You must log in or # to comment.
  • Thyrian ( Thyrian@ttrpg.network ) 
    link
    fedilink
    arrow-up
    40
    ·
    3 years ago

    You could do this in one line…

    By removing all the linebreaks.

    • DeathsEmbrace ( DeathsEmbrace@lemmy.ml ) 
      link
      fedilink
      arrow-up
      9
      ·
      3 years ago

      Why even put spaces too many key presses.

    • Homosexual sapiens ( Strawberry@lemmy.blahaj.zone ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      i think it should one giant ternary expression composition

    • iegod ( iegod@lemm.ee ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      I love this thread 🫠

  • noddy ( noddy@beehaw.org ) 
    link
    fedilink
    arrow-up
    25
    ·
    3 years ago

    I know how to fix this!

    bool IsEven(int number) {
        bool even = true;
        for (int i = 0; i < number; ++i) {
            if (even == true) {
                even = false;
            }
            else if (even == false) {
                even = true;
            }
            else {
                throw RuntimeException("Could not determine whether even is true or false.");
            }
        }
    
        if (even == true) {
            return even ? true : false;
        }
        else if (even == false) {
            return (!even) ? false : true;
        }
        else {
            throw RuntimeException("Could not determine whether even is true or false.");
        }
    }
    
    • odium ( odium@programming.dev ) 
      link
      fedilink
      arrow-up
      7
      ·
      3 years ago

      Have you tried seeing if the recursive approach runs faster?

      • noddy ( noddy@beehaw.org ) 
        link
        fedilink
        arrow-up
        14
        ·
        3 years ago

        I know an even better way. We can make it run in O(1) by using a lookup table. We only need to store 2^64 booleans in an array first.

  • neidu ( neidu@feddit.nl ) 
    link
    fedilink
    arrow-up
    15
    ·
    edit-2
    3 years ago

    My solution in perl back in the day when I was a teenage hobbyist who didn’t know about the modulus operator: Divide by 2 and use regex to check for a decimal point.

    if ($num / 2 =~ /\./) { return “odd” }
    else { return “even” }

    • lysdexic ( lysdexic@programming.dev ) 
      link
      fedilink
      English
      arrow-up
      15
      ·
      3 years ago

      Divide by 2 and check for a decimal point.

      I mean, it ain’t wrong.

  • SpeakinTelnet ( SpeakinTelnet@programming.dev ) 
    link
    fedilink
    arrow-up
    9
    ·
    edit-2
    3 years ago
    def is_even(n):
        match n:
            case 1:
                return False
            case 0:
                return True
            # fix No1
            case n < 0:
                return is_even(-1*n)
            case _:
                return is_even(n-2)
    
  • recursive_recursion [they/them] ( recursive_recursion@programming.dev ) 
    link
    fedilink
    English
    arrow-up
    7
    ·
    3 years ago

    modulo

    pseudocode:

    if number % 2 == 0
      return "number is even" (is_num_even = 1 or true)
    else
      return "number is odd" (is_num_even = 0 or false)
    

    plus you’d want an input validation beforehand

    • Mac ( mac@programming.dev ) 
      link
      fedilink
      arrow-up
      12
      ·
      3 years ago

      who needs modulo when you can get less characters out of

      while (number > 1) {
        number -= 2;
      }
      return number;
      

      very efficient

      edit: or theres the trusty iseven api

      • NullPointer ( nullPointer@programming.dev ) 
        link
        fedilink
        arrow-up
        6
        ·
        3 years ago

        here is somewhat less:

        return (number % 2) == 0;

      • perviouslyiner ( perviouslyiner@lemm.ee ) 
        link
        fedilink
        English
        arrow-up
        2
        ·
        3 years ago

        are the negative numbers all even?

        • I_am_10_squirrels ( I_am_10_squirrels@beehaw.org ) 
          link
          fedilink
          arrow-up
          2
          ·
          3 years ago

          Yes

    • avonarret1 ( avonarret1@programming.dev ) 
      link
      fedilink
      arrow-up
      4
      ·
      3 years ago

      🤦

    • Vex_Detrause ( Vex_Detrause@lemmy.ca ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago
      #You are an input. You have value! You matter!
      if number % 2 == 0
        return "number is even" (is_num_even = 1 or true)
      else
        return "number is odd" (is_num_even = 0 or false)
      

      Am I doing it right? /S.

      • PoolloverNathan ( PoolloverNathan@programming.dev ) 
        link
        fedilink
        English
        arrow-up
        4
        ·
        3 years ago

        Don’t put nbsps in code blocks, they show up literally.

    • RandomVideos ( RandomVideos@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      This code is terrible. If you input 10.66 it returns "number is odd

      It should be:

      if number % 2 == 0
        return "number is even" (is_num_even = 1 or true)
      else
        return "number is not even" (is_num_even = 0 or false)
      
  • ⚡⚡⚡ ( ndsvw@feddit.de ) 
    link
    fedilink
    arrow-up
    5
    ·
    3 years ago

    deleted by creator

  • vrighter ( vrighter@discuss.tchncs.de ) 
    link
    fedilink
    arrow-up
    5
    ·
    3 years ago

    there is!

  • FarraigePlaisteach ( FarraigePlaisteach@kbin.social ) 
    link
    fedilink
    arrow-up
    4
    ·
    3 years ago

    I would replace each if/else with a while.

    • Kogasa ( kogasa@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      deleted by creator

  • Thyrian ( Thyrian@ttrpg.network ) 
    link
    fedilink
    arrow-up
    4
    ·
    3 years ago

    This could be optimized by using a recursive function.

    • neeeeDanke ( neeeeDanke@feddit.de ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago

      This could be made more servicavle by using a switch case

  • tweeks ( tweeks@feddit.nl ) 
    link
    fedilink
    arrow-up
    3
    ·
    3 years ago

    I would love it if someone edited this example and posted it with two statements near the end that are reversed, implying inconsistent behaviour at random in the list ahead, seemingly making this solution less inefficient.

  • andioop ( andioop@programming.dev ) OP
    link
    fedilink
    arrow-up
    2
    ·
    3 years ago

    Source tweet

  • AItoothbrush ( AI_toothbrush@lemmy.zip ) 
    link
    fedilink
    English
    arrow-up
    2
    ·
    3 years ago

    …btw a switch statement is better in this case(get it?)

  • NostraDavid ( NostraDavid@programming.dev ) 
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    1 month ago

    deleted by creator

  • Hyperreality ( Hyperreality@kbin.social ) 
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    deleted by creator

  • solomonschuler ( solomonschuler@lemmy.zip ) 
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    4 months ago

    deleted by creator

Programming Horror@programming.dev

programming_horror@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programming_horror@programming.dev

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there’s also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @Ategon@programming.dev

Rules

  • Keep content in english
  • No advertisements (this includes both code in advertisements and advertisement in posts)
  • No generated code (a person has to have made it)

Credits

  • Icon base made by Lorc under CC BY 3.0 with modifications to add a gradient
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 21 users / 6 months
  • 23 local subscribers
  • 2.4K subscribers
  • 35 Posts
  • 125 Comments
  • Modlog
  • mods:
  • Vacant ( Vacant@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code