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.
View all comments
Show parent comment
26
u/frogamic Dec 01 '19
I agree and as someone in the industry, I would write my own ternary class using the factory pattern, deploy that as a microservice consumed by this function via a SOAP API and bundle both into a Kubernetes pod running both containers behind a load balancing ingress. This way my boss gets to use more buzzwords at the next monthly meeting and I get paid more because I can say I'm a DevOps fullstack engineer.