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.
Thief, or Procrastinator.
TDD
const max12 = (x, y) => { if (x === 1 && y === 2) { return 2; } else if (x === 7 && y === 4) { return 7; } else { return x; } };
Mathematician 2 kinda blew my mind, kinda obvious, just can’t believe I was never taught or thought about it.
Lost me when it used Math.abs after calling math.max a their
Math.Sqrt((x-y) * (x-y))
(I’ve actually seen someone use this)
Yeah, that was my favorite one
I’ve been staring at it for 10 minutes and I’m still not convinced it works.
Simple, really. Abs(x-y) is the difference between the two numbers, absolute, so positive value. So, adding abs(x-y) to the smaller of the two numbers turns it into the bigger number. Plus the bigger number, now you have 2 times the bigger number
#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.
deleted by creator
__auto_type
is a compiler builtin, not a library function. It’s not a function at all, the parentheses are for precedence & grouping.
Why use
const max = (x, y) => x > y ? x : y
instead offunction max(x, y) { return x > y ? x : y }
?2, but I’m incredibly embarrassed to say that I’ve had to do 9 before
9 is objectively better if you have untrusted inputs and need a stable failure mode.
That was precisely the reason. I prefer strongly typed languages languages, so it just felt gross to write that
If thief is actually an option, then thief.
Otherwise probably procreator or engineer 😅😬
Edit: errrrr that was supposed to say “procrastinator” 😬dyac
I use 8, but only when I’m operating on unsigned longs.
I’m in this post and I’m offended.
Mathematician 3
Max(x, y) = floor(ln(e^x + e^y))
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?
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))
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(.
So it would only work for at most [base of your log, so e<3 for ln] variables.
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.
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.
After searching a little, I found the name of the function and it’s proof: https://en.wikipedia.org/wiki/LogSumExp
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.
I just remembered where I learned about that function, in this course on convex optimization that unfortunately I never had the opportunity to finishing it but is really good.
Last one should be // still a student
Every single entry other than thief is “still a student”
max6(1, 2, 3)
Man that’s going to cause some headaches…
Engineer I guess… Thief is the objectively better enterprise programmer option but I don’t know why I always forget about it and just write a ternary ¯\_(ツ)_/¯
JS instills distrust. Rely on built-in sort? It’s alphanumeric. Rely on built-in reverse? It modifies your array. Copy an array? No you didn’t.
Yeah no kidding we bang out a kata in a dozen characters. I don’t even believe in order of operations at this point. I’ve been routinely betrayed.
Reminded of how truly little I know about programming despite the time have spent doing it
Ugh. I’ll never be any good.
Listen, in industry programming (and for personal projects if you want to get them done), the thief is the way to go. By all means, challenge yourself to understand each of these functions, but 99% of day to day development will not look like this.
How much time have you spent doing it? What part didn’t you understand? If it’s the bit shifting stuff, don’t worry about it - hardly anyone actually knows how that works unless they look it up.
About a year with varying levels of commitment and intensity.
I kind of just threw myself into the deep end. Which was a rewarding but frustrating experience. My first project was one hot encoding 400gb of reddit porn to try and teach stylegan3 how to make porn. And then turning the function on in stylegan3. And then there was validating the images and ditching the ones that erroted. Resizing the whole datasets. Using ffmpeg to extract stills from the gifs and mp3s
I found stable diffusion existed like 5 days into actually training it which was bitter sweet. I mean. No way it would have produced actual porn but I was really looking forward to the horror.
I taught myself as I went along which is a great way to learn but it’s super disheartening when I see the math that’s second nature to anyone whose studied this stuff academically… I don’t like math. In fact I hate it, and no matter how skilled (or not) a coder I become I’ll never learn calculus sometimes makes it all feel like a fools errand.
I also hate math, and am jealous of people who are good at it. I get anxiety just doing simple multiplications, and have to look everything up. That said, I’m a senior platform developer, and earn more than anyone else in my family, so like… math helps a lot, but you don’t neeed it to be a real dev. Certainly not to be a hobbyist/hacker, like yourself.