• Zagorath@aussie.zone
      link
      fedilink
      English
      arrow-up
      13
      arrow-down
      3
      ·
      edit-2
      1 year ago

      Well, the question sort of implies that you’re needing to implementing Math.max yourself, for whatever reason. Probably as an exercise. It doesn’t make sense to reuse a library that implements the feature if you’re explicitly being asked how you would implement it yourself.

      • Demonen@lemmy.ml
        link
        fedilink
        English
        arrow-up
        4
        arrow-down
        1
        ·
        1 year ago

        This is why I think school and interviews are like a whole different universe from the one where actual work gets done.

        • Zagorath@aussie.zone
          link
          fedilink
          English
          arrow-up
          4
          arrow-down
          1
          ·
          1 year ago

          In some ways they can be wholly different, but I don’t think this is a good example of it.

          Any programmer who cannot implement “take two numbers, return the larger one” is clearly not very competent. Even though you’re never going to literally need to implement Math.max yourself at work, you are going to need basically the same types of skills. Probably 95% of the work I do day-to-day is stuff you’d learn in your first year at uni, and this just shows that you’ve got that ability.

          In practice, the best interviews I’ve had usually set a slightly more complicated task as a do-in-your-own-time problem and then go through what you did in the actual interview. Problems like “read a list of names in the form , each name on a separate line, from a text file. Sort the names by last name, then by other names. Output to another text file. Include unit tests.” They wouldn’t then expect you to re-implement the sorting algorithm itself, but more want to look at the quality of code, extensibility, etc.

          More basic questions like the one in the OP, or fizzbuzz, are decent as well, and a big step up from lame questions like “what does SOLID stand for? What does the Liskov substitution principle mean to you?” Even if they’re not quite as valuable as a miniature project.

          • Rodeo@lemmy.ca
            link
            fedilink
            arrow-up
            1
            arrow-down
            1
            ·
            1 year ago

            In practice, the best interviews I’ve had usually set a slightly more complicated task as a do-in-your-own-time problem and then go through what you did in the actual interview.

            The best interviews you’ve had are the ones where you’re doing free work on your own time?

  • auf@lemmy.ml
    link
    fedilink
    arrow-up
    60
    arrow-down
    1
    ·
    1 year ago

    Thief way is actually the best among all of these imo, in terms of readability and efficiency.

        • Demonen@lemmy.ml
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 year ago

          If you’re optimizing that hard you should probably sort the data first anyway, but yeah, sometimes it’s absolutely called for. Not that I’ve actually needed that in my professional career, but then again I’ve never worked close enough to metal for it to actually matter.

          That said, all of these are implemented as functions, so they’re already costing the function call anyway…

    • snowe@programming.dev
      link
      fedilink
      arrow-up
      14
      arrow-down
      1
      ·
      1 year ago

      They’re setting a variable to a function. Just use the original function. All thief does is obfuscate for literally no gain except character count.

  • Leo Uino@lemmy.sdf.org
    link
    fedilink
    arrow-up
    34
    ·
    1 year ago

    TDD

    const max12 = (x, y) => {
        if (x === 1 && y === 2) {
            return 2;
        } else if (x === 7 && y === 4) {
            return 7;
        } else {
            return x;
        }
    };
    
  • kewko@lemdro.id
    link
    fedilink
    English
    arrow-up
    17
    ·
    1 year ago

    Mathematician 2 kinda blew my mind, kinda obvious, just can’t believe I was never taught or thought about it.

  • Eufalconimorph@discuss.tchncs.de
    link
    fedilink
    arrow-up
    12
    ·
    1 year ago
    #define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })
    

    GNU C. Also works with Clang. Avoids evaluating the arguments multiple times. The optimizer will convert the branch into a conditional move, if it doesn’t I’d replace the ternary with the “bit hacker 2” version.

    • neeeeDanke@feddit.de
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      1 year ago

      so 0.3 ~= 1-ln(2)=max(1-ln(2),1-ln(2)) = floor(ln(2*e^(1-ln(2)))) = floor(ln(2)+(1-ln(2))) = 1 ?

      That would bee engeneer 2, not Mathematician3 xD.

      Just out of curiostity, what was you Idea behind that?

      • driving_crooner@lemmy.eco.br
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        1 year ago

        Guess only work with integers, specially for the floor function that is going to give you an integer at the end everytime.

        Not my idea, learned it somewhere while doing college in an statistics class. The idea is that the exponential function grow really fast, so small difference on variables become extreme difference on the exponential, then the log function reverse the exponential, but because it grew more for the biggest variable it reverts to the max variable making the other variables the decimal part (this is why you need the floor function). I think is cool because works for any number of variables, unlike mathematician 2 who only work for 2 variables (maybe it can be generalized for more variables but I don’t think can be done).

        For a min fuction it can be use ceiling(-ln(e^-x + e^-y))

        • neeeeDanke@feddit.de
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          1 year ago

          to be fair it does seem to work for any two numbers where one is >1. As lim x,y–> inf ln(ex+ey) <= lim x,y --> inf ln(2 e^(max(x,y))) = max(x,y) + ln(2).

          I think is cool because works for any number of variables

          using the same proof as before we can see that: lim,x_i -->inf ln(sum_i/in I} e^(x_i)) <= ln(I|) +max{x_i | i /in I.

          So it would only work for at most [base of your log, so e<3 for ln] variables.

            • neeeeDanke@feddit.de
              link
              fedilink
              arrow-up
              2
              ·
              edit-2
              1 year ago

              thanks for looking it up:).

              I do think the upper bound on that page is wrong thought. Incedentally in the article itself only the lower bound is prooven, but in its sources this paper prooves what I did in my comment before as well:

              for the upper bound it has max**+log(n)**. (Section 2, eq 4) This lets us construct an example (see reply to your other comment) to disproove the notion about beeing able to calculate the max for many integers.

          • driving_crooner@lemmy.eco.br
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            I don’t have a mathematical proof, but doing some experimental tests on excel, using multiple (more than 3) numers and using negative numbers (including only negative numbers) it works perfectly every time.

            • neeeeDanke@feddit.de
              link
              fedilink
              arrow-up
              2
              ·
              edit-2
              1 year ago

              Try (100,100,100,100,100,101) or 50 ones and a two, should result in 102 and 4 as a max respectively. I tried using less numbers, but the less numbers you use, the higher the values (to be exact less off a deviation(%-difference) between the values, resulting in higher numbers) have to be and wolframAlpha does not like 10^100 values so I stopped trying.

  • Shhalahr@beehaw.org
    link
    fedilink
    arrow-up
    8
    ·
    1 year ago

    Procrastinator.

    Okay, but seriously: “Thief”. Why reimplement it if it’s already available in the language?

  • maegul (he/they)@lemmy.ml
    link
    fedilink
    English
    arrow-up
    7
    ·
    edit-2
    1 year ago

    Otherwise, realistically, I’m prob the worst of all worlds … the procrastinator waiting/hoping to be the pair programmer that has hopefully remembered to just be the thief.

  • bstix@feddit.dk
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    1
    ·
    edit-2
    1 year ago

    And then your customer changes their mind. Instead of two numbers, they will now input three numbers. How easy will it be for you to change your code?

    And then the customer changes their mind. Instead of three numbers, they will now input any series of numbers. How easy will it be for you to change your code? And why didn’t you already do this is the previous step?

    And then the customer changes their mind. Instead of any set of numbers, they will now input numbers and text. How easy will it be to change your code?

    And then the customer changes their mind. They now have no idea of what they’re sending you or if they’re even sending you anything. Nevermind the code now, you already did that in the previous step, right? How easy will it be to explain what you’re invoicing them for?