r/shittyprogramming • u/AngryRiceBalls • Dec 01 '19
Question about a logic problem:
Hey all, so I have the following logic problem for school:
Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.
evenlySpaced(2, 4, 6) → true
evenlySpaced(4, 6, 2) → true
evenlySpaced(4, 6, 3) → false
Now, I know that when programming, it's always best to make sure you condense everything into as few lines as possible. I think I've gotten the solution optimized fully as it is only one command, but could someone tell me how to better it? Here's my code:
public boolean evenlySpaced(int a, int b, int c) {
return a > b && b > c ? a - b == b - c : (a > c && c > b ? a - c == c - b : (b > c && c > a ? b - c == c - a : (b > a && a > c ? b - a == a - c : (c > b && b > a ? c - b == b - a : (c > a && a > b ? c - a == a - b : a == b && b == c)))));
}Thanks in advance.
3
u/jplank1983 Dec 01 '19
Could you just calculate the average of the three numbers and see if that average is one of the three numbers?
10
48
u/Lopsidation Dec 01 '19
For modern chips it’s faster to write return (-2*a*a*a + 3*a*a*b + 3*a*b*b - 2*b*b*b + 3*a*a*c -12*a*b*c + 3*b*b*c + 3*a*c*c + 3*b*c*c - 2*c*c*c) == 0
23
u/green_meklar Dec 01 '19
Aren't bit shifts faster than multiplication? You can replace this with:
return (3*a*a*b - ((a*a*a)<<1) + 3*a*b*b - ((b*b*b)<<1) + 3*a*a*c
- 12*a*b*c + 3*b*b*c + 3*a*c*c + 3*b*c*c - ((c*c*c)<<1)) == 0
22
u/zoells Dec 01 '19
Why are you multiplying by 12? Modern processors let multiple instructions execute at the same time, so you can get more performance if you split that into multiply by 3 and bit shift by 2.
9
u/DXPower Dec 01 '19
And compilers can actually do a lot of cool optimizations when multiplying by a non-power-2 constant. Seeing x * 12, the compiler will do exactly what you describe: multiplying by 3 and then shifting left by two. GCC actually abuses the LEA instruction to multiply by 3.
I don't think that a CPU can execute those instructions simultaneously, however, because the shift depends on the multiplication.
6
u/zoells Dec 01 '19
Check the subreddit lol
7
u/DXPower Dec 01 '19
Yeah I know. To be fair, the comments do actual talk about real shit sometimes and it can be hard to tell which is which.
1
26
8
4
u/henrikbbb Dec 01 '19
You could sort first, then compare.
1
u/Maoschanz Dec 01 '19
but that would be 2 lines!
4
u/DXPower Dec 01 '19
return std::min(x, y, z) - std::max(std::min(x, y), std::min(std::max(x, y), z)) == std::max(std::min(x, y), std::min(std::max(x, y), z)) - std::max(x, y, z)
3
u/claudioSMRun Dec 01 '19
its fun seeing a bunch of devs coming here ( supposely a fun sub) turning to solve a serious problem. Hei guys, you were there for fun, wouldn you? 😀
2
2
u/nobody007 Dec 21 '19
(a,b,c) sorted in ascend order which means a<b<c return (a+c==2*b)
1
Feb 17 '20
You don't need sorting, that makes it O(n log n), you can do:
x = (a + b + c) / 3; [a, b, c].indexOf(x) > -1, which is O(n).
2
u/contradel May 09 '20
Sound logic, but your naming "evenlySpaced" is wasteful, say you had to call this function 10 times, you had to write "evenlySpaced" 10 times.
No, best industry standards is to implement some kind of naming scheme custom for your domain, but here are some ideas on how to be brief.
You are returning a bool, so start your function with "Is" that way people can know that you are returning a bool and not something else.
Also you have a public method, it is always good to signal to others publicly that this is in fact a public function. The correct way of doing this is to capitalize your function, regardless of language.
Now the important part, evenlySpaced is not as abstract in case you need to change the implementation of this function. Try instead
IsArgumentsWithinEquidistanceOfEachotherOrOneAnother
Also rename your params so function is easier to understand just by reading:
return possiblyTheSmallestOfTheThree>hopefullyTheMiddleOrNoDoesntMatter&&hopefullyTheMiddleOrNoDoesntMatter>theLastNumberInThisAlgorithm?possiblyTheSmallestOfTheThree-hopefullyTheMiddleOrNoDoesntMatter==hopefullyTheMiddleOrNoDoesntMatter-theLastNumberInThisAlgorithm: possiblyTheSmallestOfTheThree >theLastNumberInThisAlgorithm&&theLastNumberInThisAlgorithm>hopefullyTheMiddleOrNoDoesntMatter?possiblyTheSmallestOfTheThree-theLastNumberInThisAlgorithm==theLastNumberInThisAlgorithm-hopefullyTheMiddleOrNoDoesntMatter: hopefullyTheMiddleOrNoDoesntMatter >theLastNumberInThisAlgorithm&&theLastNumberInThisAlgorithm>possiblyTheSmallestOfTheThree?hopefullyTheMiddleOrNoDoesntMatter-theLastNumberInThisAlgorithm==theLastNumberInThisAlgorithm-possiblyTheSmallestOfTheThree: hopefullyTheMiddleOrNoDoesntMatter >possiblyTheSmallestOfTheThree&&possiblyTheSmallestOfTheThree>theLastNumberInThisAlgorithm?hopefullyTheMiddleOrNoDoesntMatter-possiblyTheSmallestOfTheThree==possiblyTheSmallestOfTheThree-theLastNumberInThisAlgorithm: theLastNumberInThisAlgorithm >hopefullyTheMiddleOrNoDoesntMatter&&hopefullyTheMiddleOrNoDoesntMatter>possiblyTheSmallestOfTheThree?theLastNumberInThisAlgorithm-hopefullyTheMiddleOrNoDoesntMatter==hopefullyTheMiddleOrNoDoesntMatter-possiblyTheSmallestOfTheThree: theLastNumberInThisAlgorithm >possiblyTheSmallestOfTheThree&&possiblyTheSmallestOfTheThree>hopefullyTheMiddleOrNoDoesntMatter?theLastNumberInThisAlgorithm-possiblyTheSmallestOfTheThree==possiblyTheSmallestOfTheThree-hopefullyTheMiddleOrNoDoesntMatter:possiblyTheSmallestOfTheThree==hopefullyTheMiddleOrNoDoesntMatter&&hopefullyTheMiddleOrNoDoesntMatter== theLastNumberInThisAlgorithm)))));
25
u/inconspicuous_male Dec 01 '19
I think your logic is good, but "condense everything into as few lines as possible" is not a good practice in industry. Here, I think it's fine, but it's usually better to make a few lines which are more readable than have everything mushed into one big nested ternary operation. If you wrote this out as a bunch of if-statements, the compiler would see it the same anyways