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.
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
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?
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) == 026
u/Synx Dec 01 '19
Optimal
8
u/tjmaxal Dec 01 '19
could you write it again but with =/= instead.
4
u/henrikbbb Dec 01 '19
You could sort first, then compare.
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
u/poops-n-farts Dec 01 '19
This hurts me
2
u/nobody007 Dec 21 '19
(a,b,c) sorted in ascend order which means a<b<c return (a+c==2*b)
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)))));