Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How can I improve my answer to this code problem?

Ranch Hand
Posts: 192
3
For codingbat.com puzzle http://codingbat.com/prob/p184004
Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number.

nearHundred(93) → true
nearHundred(90) → true
nearHundred(89) → false

I came up with two solutions but I'm wondering if there is a better way for me to have handled the range logic?

Here is my first solution but I was thinking of another way

Here is the other way I thought may work but my variable "result" is a String so it won't work.

Can you please give me your opinion?   Is my solution ( the first solution ) the best option or is there a better way to handle the problem ?   Also how could I get my second solution to work?

lowercase baba
Bartender
Posts: 12601
50
so what happens if you put in -95?  I think you'd return true...but it's not within 10 of 100.

I think they are hinting you look at the difference between your number and 100 or 200.

Marshal
Posts: 57446
175
Your first solution is incorrect because it will return true if you pass -199 to it.
Fred is right; they want differences.

Never write
if (somethingBoolean) return true; else return false;
Write
return somethingBoolean;
you can read about it here (beware: old link).

In the second solution you are not returning anything; you want a boolean. That won't compile. I don't think you have corrected the problem in the first code either.

Lisa Austin
Ranch Hand
Posts: 192
3
Hmm it's a good question you guys brought up and I appreciate it.  I actually passed all the tests that was given.  I do happen to appreciate the different perspective though and that's why I posted.  Let me think how I can solve this IF the value -195 was passed.

Lisa Austin
Ranch Hand
Posts: 192
3

Here is the tests they ran.   No negative numbers thrown .    But I appreciate making me think outside of the box.

Expected                        Run
nearHundred(93) → true true OK
nearHundred(90) → true true OK
nearHundred(89) → false false OK
nearHundred(110) → true true OK
nearHundred(111) → false false OK
nearHundred(121) → false false OK
nearHundred(0) → false false OK
nearHundred(5) → false false OK
nearHundred(191) → true true OK
nearHundred(189) → false false OK
nearHundred(190) → true true OK
nearHundred(200) → true true OK
nearHundred(210) → true true OK
nearHundred(211) → false false OK
nearHundred(290) → false false OK

Lisa Austin
Ranch Hand
Posts: 192
3
Okay is this better?

Sheriff
Posts: 4577
128
• 1
That will work.  Can you do it with only two conditions?

Lisa Austin
Ranch Hand
Posts: 192
3

Knute Snortum wrote:That will work.  Can you do it with only two conditions?

So when you say "condition" you are talking about the

So by two you want me to try with something like

Do I understand correctly ?

Sheriff
Posts: 11632
187
The problem statement hints at using Math.abs() -- play around with that a little bit. You've also been given a hint about using abs() with differences. You have another hint telling you that you need only two conditional expressions.  Here's another hint:

Let: X be the number you're checking

Think: What mathematical expression will tell you if X is, more or less, 10 numbers away from 100? More or less, 10 numbers away from 200?
Think: How does the "difference" hint help here?
Think: How does the Math.abs() hint help here?

Bartender
Posts: 585
9
As Junilu stated, you should compare |n-100| to the tolerance, and |n-200| to the tolerance. This has the advantage of making it trivial to change the code if the tolerance changes, say from 10 to 5.

Campbell Ritchie
Marshal
Posts: 57446
175

Lisa Austin wrote:. . . I appreciate it.

That's a pleasure

I actually passed all the tests that was given. . . .

You mean it was the codingbat website which failed miserably? Try your method as it stands with -199 or whatever, check carefully that there is nothing in the codingbat instructions about, “only positive numbers,” or similar, and contact them to notify a bug.

Knute Snortum
Sheriff
Posts: 4577
128

contact them to notify a bug.

Done.

Junilu Lacar
Sheriff
Posts: 11632
187
It's not really so much a bug as it is the OP has found a deficiency in the test suite. The test suite that site runs only uses values >= 0.

Lisa Austin
Ranch Hand
Posts: 192
3

Junilu Lacar wrote:The problem statement hints at using Math.abs() -- play around with that a little bit. You've also been given a hint about using abs() with differences. You have another hint telling you that you need only two conditional expressions.  Here's another hint:

Let: X be the number you're checking

Think: What mathematical expression will tell you if X is, more or less, 10 numbers away from 100? More or less, 10 numbers away from 200?
Think: How does the "difference" hint help here?
Think: How does the Math.abs() hint help here?

Phew --  First Thank You all for challenging me.  I need to learn to think like you guys.      Now , What do you think of this?

Knute Snortum
Sheriff
Posts: 4577
128
• 1
That's what I would do.  Good job!

Campbell Ritchie
Marshal
Posts: 57446
175
I tried the same but minus most of the () which are mostly redundant.

Lisa Austin
Ranch Hand
Posts: 192
3

Campbell Ritchie wrote:I tried the same but minus most of the () which are mostly redundant.

Ah got it.  I always thought it had to be ( condition ) || ( condition)   for some reason.

Junilu Lacar
Sheriff
Posts: 11632
187

Lisa Austin wrote:Now , What do you think of this?

That's more like it

This is what Campbell is talking about:

Lisa Austin wrote:I need to learn to think like you guys

Be careful what you wish for...

Lisa Austin
Ranch Hand
Posts: 192
3
I just wanted to say Thank You to you all.   I want to not only learn Java but I want to learn how to do it .. more efficiently.   When I went through a class the instructor's answer was always something like "That would work".  I don't want just "That it would work" I want  how to do it better.  I'm learning this on the job as well so feed back I receive from my co-workers has really helped but during my practice I don't have the opportunity to run the solution by them so Thank You for YOUR time!

Junilu Lacar
Sheriff
Posts: 11632
187
• 1

Lisa Austin wrote:I just wanted to say Thank You to you all.   I want to not only learn Java but I want to learn how to do it .. more efficiently.

I would suggest that you change "efficiently" to "cleanly" -- If you really want to learn to think the way a lot of us think here, see the difference in the original code and the suggested code in this recent thread: https://coderanch.com/t/669429/java/java/Checkers-Engine-Java-King-Moving

fred rosenberger
lowercase baba
Bartender
Posts: 12601
50
• 1

Lisa Austin wrote: I want  how to do it better.

Remember though that one person's "better" can be another person's "it's less clear to me what you are doing".

My preference is to use the parens.  They help my poor brain focus in on what's being evaluated first, even if Java does not strictly require them.  Campbell's "redundancy" is my "clarity". But Campbell is also smarter than me (which is damning with faint praise if I EVER heard it...)

Lisa Austin
Ranch Hand
Posts: 192
3

fred rosenberger wrote:

Lisa Austin wrote: I want  how to do it better.

Remember though that one person's "better" can be another person's "it's less clear to me what you are doing".

My preference is to use the parens.  They help my poor brain focus in on what's being evaluated first, even if Java does not strictly require them.  Campbell's "redundancy" is my "clarity". But Campbell is also smarter than me (which is damning with faint praise if I EVER heard it...)

That is very true.  I don't think my company has a coding standard for example and for a while a while, code reviews was like a silent argument between Engineers where one guy places a space and another removed it.  It was humorous to me but confusing because I was on those reviews as part of my learning and the changes just created confusion for me.    What I was actually referring to was the difference between my original solution vs. the solution  you guys helped me come to.

Lisa Austin
Ranch Hand
Posts: 192
3

Junilu Lacar wrote:

Lisa Austin wrote:I just wanted to say Thank You to you all.   I want to not only learn Java but I want to learn how to do it .. more efficiently.

I would suggest that you change "efficiently" to "cleanly" -- If you really want to learn to think the way a lot of us think here, see the difference in the original code and the suggested code in this recent thread: https://coderanch.com/t/669429/java/java/Checkers-Engine-Java-King-Moving

Great example.  Thank You.  Good point as well.

Junilu Lacar
Sheriff
Posts: 11632
187
Well, if you take a hint from that website, automated testing really helps you find problems with your code.  If they had included just one test that passed in a negative number as Campbell suggested, you would have realized that your original solution was not quite right. Lesson: write automated tests as you develop your code and use them to drive the way you think about the solution you're creating.

Campbell Ritchie
Marshal
Posts: 57446
175

fred rosenberger wrote:. . . .  Campbell's "redundancy" is my "clarity". . . .

Redundant doesn't mean unnecessary, but optional. In many cases redundant parts contribute to robustness. And if you have disagreements about whether to use () or not, you come to two conslusions:-
• 1: You need a style guide.
• 2: You need the (). If some people need () for clarity, then they shou‍ld be included. There is only one instance where additional () can cause any harm.
•
Lisa Austin
Ranch Hand
Posts: 192
3

Junilu Lacar wrote:Well, if you take a hint from that website, automated testing really helps you find problems with your code.  If they had included just one test that passed in a negative number as Campbell suggested, you would have realized that your original solution was not quite right. Lesson: write automated tests as you develop your code and use them to drive the way you think about the solution you're creating.

Good point and Thank You.  I really should have thought about that as well.

Campbell Ritchie
Marshal
Posts: 57446
175
• 1

Junilu Lacar wrote:. . .  If they had included just one test that passed in a negative number as Campbell suggested, . . . .

It also shows you need to include “incorrect” input in your tests, in this case a few negative numbers. In fact a test without both positive and negative input is probably not worth the effort.

 Ruth Stout was famous for gardening naked. Just like this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS