Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fundamental coding help
#2
I don't think you're using too many if statements. When trying to find code bloat, the easiest way to approach it is to ask if you're accomplishing anything outside of what the function definition says. I don't see that here, therefore no bloat.

However, there are likely faster algorithms. The first algorithm I'd like to look at is #2. If I were to look at this formula for the first time, I would have NO IDEA what it's supposed to do. The best code is code which documents itself. And that is not self-documenting code.

In this problem, there are four possible pairings which would cause exceptions. Either the first and second number, second and third, or third and first are alike. Each of the following if statements checks if the others are pairs.

Code:
public int loneSum(int a, int b, int c) {
  int sum = 0;

  if(a^b > 0 && a^c > 0)
  {
      sum += a;
  }
  if(b^a > 0 && b^c > 0)
  {
      sum += b;
  }
  if(c^a > 0 && c^b > 0)
  {
      sum += c;
  }
  return sum;
}

I'm sure there must be a faster way to do this as well because b^a is the same as a^b.
Reply


Messages In This Thread
Fundamental coding help - by Kalovale - 2010-10-28, 04:38 AM
Fundamental coding help - by Fiel - 2010-10-28, 08:24 AM
Fundamental coding help - by Kalovale - 2010-10-28, 10:07 AM
Fundamental coding help - by Eos - 2010-10-28, 10:22 AM
Fundamental coding help - by Fiel - 2010-10-28, 10:38 AM
Fundamental coding help - by Kalovale - 2010-10-28, 12:16 PM
Fundamental coding help - by Nikkey - 2010-10-28, 12:49 PM
Fundamental coding help - by Kalovale - 2010-10-28, 12:59 PM
Fundamental coding help - by Nikkey - 2010-10-28, 01:06 PM
Fundamental coding help - by Kalovale - 2010-10-28, 01:09 PM
Fundamental coding help - by XTOTHEL - 2010-10-28, 02:47 PM
Fundamental coding help - by Nikkey - 2010-10-28, 03:06 PM
Fundamental coding help - by Dusk - 2010-10-28, 03:17 PM
Fundamental coding help - by Stereo - 2010-10-28, 03:45 PM
Fundamental coding help - by Kalovale - 2010-10-29, 10:50 AM
Fundamental coding help - by Spaz - 2010-10-29, 02:41 PM
Fundamental coding help - by Fiel - 2010-10-29, 04:07 PM
Fundamental coding help - by Stereo - 2010-10-30, 09:46 AM
Fundamental coding help - by Russt - 2010-10-30, 10:34 AM
Fundamental coding help - by Kalovale - 2010-11-03, 12:50 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)