Southperry.net
Fundamental coding help - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: The Speakeasy (https://www.southperry.net/forumdisplay.php?fid=54)
+--- Thread: Fundamental coding help (/showthread.php?tid=32007)



Fundamental coding help - Kalovale - 2010-10-28

Well, I'm thinking I should form a good coding habit from the beginning. There is almost no complexity in these problems, but I feel that there are better ways to write the codes, or even more condensed logical approaches. So, if you're bored, would you kindly show me how you'd tackle these problems, I'll figure out the details on my own.

Current issue: I think I'm using if-else-then statements too excessively.

codingbat:

Logic:

1/

 Spoiler

2/

 Spoiler

3/

 Spoiler

4/

 Spoiler

I'll edit more in later, gotta go do something. Oh, and it's not strictly limited to Java, though the syntax alternatives should be.


Fundamental coding help - Fiel - 2010-10-28

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.


Fundamental coding help - Kalovale - 2010-10-28

Actually that was the one I had most half-heartedness in. I guess you can tell by the impropriety/inconsistency.

Anyway, how exactly do I read that operator? Exclusive OR would translate to:
"If only either a or b is greater than 0 and if only either a or c is greater than 0" ? Doesn't really sound right.
It seems what you're doing there says: "if a is not equal to b and if a is not equal to c; increment sum by a"


Fundamental coding help - Eos - 2010-10-28




Fundamental coding help - Fiel - 2010-10-28




Fundamental coding help - Kalovale - 2010-10-28

Okay this is getting pretty petty of me, but I keep getting the impression that my approach isn't proper enough. It just seems somehow messy.

 Spoiler

I have a favor though, regarding this problem:
Quote:Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "public int fixTeen(int n) {"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main noTeenSum().

noTeenSum(1, 2, 3) → 6
noTeenSum(2, 13, 1) → 3
noTeenSum(2, 1, 14) → 3

The solution should be simple enough, but I can't seem to "call" the n from the fixTeen block to the main block to use. Also, I may not be thinking straight but how do I filter so that the outcome n satisfies certain conditions? I have been doing something along the lines of conditionals:
Code:
public int fixTeen(int n){
  if( (n>=13) && (n<=19) && (n != 15) && (n != 16) )
  return n;
}
But I can't test if it works correctly because of the problem above.


Fundamental coding help - Nikkey - 2010-10-28

Fiel Wrote: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.
 Spoiler

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

I see no reason as of why you need to xor first, then compare with >. This works perfectly fine, does the same thing and requires less computing power:

 Spoiler
Faster approach is probably
 Spoiler

For four, split it up more. You know you use repetitive patterns (diffAB <= 1), for example. Also, (! (diffAB <= 1)) == (diffAB >= 2).
 Spoiler

Kalovale Wrote:Okay this is getting pretty petty of me, but I keep getting the impression that my approach isn't proper enough. It just seems somehow messy.

 Spoiler

As of right now, fixTeen returns n if n is 13, 14, 17, 18 or 19. Otherwise, what does it return? What should it return?



Fundamental coding help - Kalovale - 2010-10-28

Devil's Sunrise Wrote:As of right now, fixTeen returns n if n is 13, 14, 17, 18 or 19. Otherwise, what does it return? What should it return?

I expect it to return exactly those values, as for the "else" part, that's what perplexes me. I want to filter out the non-teen values with a view that after the method, n consists of only teens.

Ahh, I think I just figured out something, brb.


Fundamental coding help - Nikkey - 2010-10-28

Kalovale Wrote:I expect it to return exactly those values, as for the "else" part, that's what perplexes me. I want to filter out the non-teen values with a view that after the method, n consists of only teens.

Wouldn't it be smarter for fixTeen to return 0 if it is 13, 14, 17, 18 or 19, and n otherwise? Then you can just do fixTeen(a) + fixTeen(b) + fixTeen©;


Fundamental coding help - Kalovale - 2010-10-28

Devil's Sunrise Wrote:Wouldn't it be smarter for fixTeen to return 0 if it is 13, 14, 17, 18 or 19, and n otherwise? Then you can just do fixTeen(a) + fixTeen(b) + fixTeen©;

Same concept, but thank you. I know how to call n now. Big Grin
I'm soooooooo abusing this, it makes things so much clearer.

Also, as for 4/, don't you mean for closeAB to be boolean? I mean, it's even used as a boolean. o.o
Quote:if (closeAB)



Fundamental coding help - XTOTHEL - 2010-10-28

oh wait..


Fundamental coding help - Nikkey - 2010-10-28

Kalovale Wrote:Same concept, but thank you. I know how to call n now. Big Grin
I'm soooooooo abusing this, it makes things so much clearer.

Also, as for 4/, don't you mean for closeAB to be boolean? I mean, it's even used as a boolean. o.o

Yeah, change int to boolean. (That happens when you don't have a compiler available and just reply on the fly.)


Fundamental coding help - Dusk - 2010-10-28




Fundamental coding help - Stereo - 2010-10-28




Fundamental coding help - Kalovale - 2010-10-29

I feel more accomplished with this than with #4, but aside from sorting out all the special cases one by one, I can't think of another approach. Any suggestions/evaluations? I don't mean to be studying things I'm not supposed to. I'm just thinking there might be a fundamental technique that I missed from reading the guides.

Also, is it noticeably bad practice to create unnecessary objects? Like the remainder below with is only used once and can easily be substituted by goal%5?

 Spoiler



Fundamental coding help - Spaz - 2010-10-29

Kalovale Wrote:Also, is it noticeably bad practice to create unnecessary objects? Like the remainder below with is only used once and can easily be substituted by goal%5?
Not at all, in fact the opposite is true. Using variables to store intermediate results can make your code more readable than if you have one gigantic expression.


Fundamental coding help - Fiel - 2010-10-29

Something you'll learn down the road is that maintainable code is worth far, far more than fast code. Eventually down the line someone else is going to look at your code. It might even end up on TDwtp (lol - the daily wt'f - just google it). Fast code is only necessary when one needs bal'ls to the walls speed, and for many applications it's simply not necessary.

The best code is "at-a-glance" code where you can just skim it and have a fairly good idea of exactly what each method or function will do.

Good coding practices state that about 15 - 20% of your code should be developer comments. More complicated parts of the code should be documented heavier. Comments should not restate the code, but state why that method of code was used instead of another, or explain the purpose of the code at hand.


Fundamental coding help - Stereo - 2010-10-30

Kalovale Wrote:I feel more accomplished with this than with #4, but aside from sorting out all the special cases one by one, I can't think of another approach. Any suggestions/evaluations? I don't mean to be studying things I'm not supposed to. I'm just thinking there might be a fundamental technique that I missed from reading the guides.

Also, is it noticeably bad practice to create unnecessary objects? Like the remainder below with is only used once and can easily be substituted by goal%5?

 Spoiler

To me, if the question implies an ordering to the process, it's better if the code mimics that process. Start by subtracting as many large chocolate bars as possible from the total, then consider whether the small ones are enough to fit the remainder. If you separate the two steps like that, it makes it easier to read what's happening than if you calculate a few numbers at the start, then compare them and spit out a result.

In this case, you want to subtract either
a) all the large chocolate
b) as many large chocolates as fit in the total
Depends which is smaller.
-- a is easy to calculate, it's just 'big'
-- b is total/5
So,
Code:
// remove as many big chocolates as possible, while leaving total positive
if (total/5 < big)
  total = total%5;
else
  total = total - 5*big;
Then compare the remaining total to the number of small chocolates you have available.
Code:
// return the number of small chocolates necessary to make up the total
if (small >= total)
  return total;
else
  return -1;
By doing it this way I've also reduced your 4 if conditions to 2.



Fundamental coding help - Russt - 2010-10-30

Edit: Nevermind. Misread problem.


Fundamental coding help - Kalovale - 2010-11-03

Stereo Wrote:
 Spoiler

I forgot at the time I could manipulate the arithmetics like that. I was rather thinking in the mindset of "returning value X if satisfying condition Y", not "transforming X to Y under condition Z". Thanks.

Now I'm totally stuck here. ;-;

EDIT: I got an idea, lemme try it out.

 Spoiler

I think my brain exploded. Calling count7(n/10) is finding the count of 7's in the input n/10, not putting n/10 through the process of checking for 7's... If I call for a recursion on a method at halfway through it, does it evaluate up to that point, or the whole method?