Solved

int in the range of 30 to 50 challenge

Posted on 2014-10-08
19
170 Views
Last Modified: 2014-10-09
Hi,

I was working on the int in the range of 30 to 50 challenge


http://codingbat.com/prob/p132134


I wrote code like below
public boolean in3050(int a, int b) {

if((Math.abs(a-30)>=10 &&Math.abs(b-30)>=10)  || (Math.abs(a-40)>=10&& Math.abs(b-40)>=10))
{
return true;
}

if((Math.abs(a-40)>=10 && Math.abs(b-40)>=10) || (Math.abs(a-50)>=10 && Math.abs(b-50)>=10))
{
return true;
}
  return false;
}

Open in new window

I have couple of test cases failing as below

Expected      Run            
in3050(30, 31) → true      true      OK         
in3050(30, 41) → false      false      OK         
in3050(40, 50) → true      true      OK         
in3050(40, 51) → false      true      X         
in3050(39, 50) → false      false      OK         
in3050(50, 39) → false      false      OK         
in3050(40, 39) → true      true      OK         
in3050(49, 48) → true      true      OK         
in3050(50, 40) → true      true      OK         
in3050(50, 51) → false      true      X         
in3050(35, 36) → true      true      OK         
in3050(35, 45) → false      false      OK         
Correct for more than half the tests


How to fix and improve my program. Please advise
0
Comment
Question by:gudii9
  • 8
  • 7
  • 2
  • +1
19 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 40368987
Look at how your code is handling the not OK cases, and see if you can figure out why it is getting the wrong answer.

You may also want to think about how you are doing the range 30..40 inclusive and range 40..50 inclusive tests
0
 
LVL 7

Author Comment

by:gudii9
ID: 40368999
public boolean in3050(int a, int b) {

if((Math.abs(a-30)>=10 &&Math.abs(b-30)>=10)  || (Math.abs(a-40)>=10&& Math.abs(b-40)>=10))
{
return true;
}

if((Math.abs(a-40)>=10 && Math.abs(b-40)>=10) || (Math.abs(a-50)>=10 && Math.abs(b-50)>=10))
{
return true;
}

if((a-50)>=1 && (b-50)>=1)
{
return false;
}
return false;

}

Open in new window


I think i improved my code bit still 2 failing

Expected      Run            
in3050(30, 31) → true      true      OK         
in3050(30, 41) → false      false      OK         
in3050(40, 50) → true      true      OK         
in3050(40, 51) → false      true      X         
in3050(39, 50) → false      false      OK         
in3050(50, 39) → false      false      OK         
in3050(40, 39) → true      true      OK         
in3050(49, 48) → true      true      OK         
in3050(50, 40) → true      true      OK         
in3050(50, 51) → false      true      X         
in3050(35, 36) → true      true      OK         
in3050(35, 45) → false      false      OK
0
 
LVL 84

Expert Comment

by:ozo
ID: 40369006
Think more carefully about what in the range 30..40 inclusive means, and what Math.abs(a-30)>=10 means
0
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 200 total points
ID: 40369068
Along the same lines as ozo is saying,

What does:

Math.abs(a-30)>=10

return for "a = 25"?

And is this what you want?

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40369099
thinking. Why thinking is takes time. why it is not spontaneous yet with java language unlike english language?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40369102
in the mind i have answer but to teach computer to understand is bit hard for me
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40369110
Don't worry that it takes time.  It will all get faster with practice.

Everyone is slow when they start out.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40369822
public class Test9 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		in3050(40,51);
	}

	
	public static boolean in3050(int a, int b) {

		if((Math.abs(a-30)<=10 &&Math.abs(b-30)<=10)  || (Math.abs(a-40)<=10&& Math.abs(b-40)<=10))
		{
		return true;
		}

		if((Math.abs(a-40)<=10 && Math.abs(b-40)<=10) || (Math.abs(a-50)<=10 && Math.abs(b-50)<=10))
		{
		return true;
		}

		if((a-50)>0 && (b-50)>0)
		{
		return false;
		}
		return false;

		}
}

Open in new window


tried as above but still not working. WIll modify more
0
 
LVL 84

Expert Comment

by:ozo
ID: 40369835
Think more carefully about what in the range 30..40 inclusive means, and what Math.abs(a-40)<=10 means
Think about what happens when a is 29, 30, 31, 39, 40, 41

Another hint:  How would you test for in the range -10..10?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Expert Comment

by:zzynx
ID: 40370144
>> why it is not spontaneous yet with java language unlike english language?
I think that's because you use the method Math.abs()
I wonder why you use it for this challenge?

Let's do it in English:
Checking if an int x is in the range 30...40 is just as simple as
1) checking that x is 30 or more
but ALSO
2) checking that x is no more than 40, thus 40 or less

In java
1) x >= 30
AND
2) x <= 40

Hence, checking if an int x is in the range 30...40 is just as simple as:

if ( x>=30 && x<=40) {
}

You have to check if both a and b are in the range 30..40, so that just doubling what you already had above:
if ( (a>=30 && a<=40) && (b>=30 && b<=40) ) {
}

And you have to check it for another range: 40...50, so that's replacing 30 by 40 and 40 by 50:
if ( (a>=40 && a<=50) && (b>=40 && b<=50) ) {
}

So the eventual result is:

public boolean in3050(int a, int b) {
      if ( (a>=30 && a<=40) && (b>=30 && b<=40) ) { // a and b in the range 30...40?
         return true; // Yes!
      }

      if ( (a>=40 && a<=50) && (b>=40 && b<=50) ) { // a and b in the range 40...50?
         return true; // Yes!
      }

      return false; // no succes (otherwhise, we shouldn't have come till here)
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40370836
Above solution works great.. in my mind i was struck with one direction of Math.abs for this challenge. How to keep mind working  thinking on all different directions and possibilities.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40370843
For what range of numbers  is Math.abs(a-40)<=10 true?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371117
both sides of 40 ie 30-50 since -10 also considered as 10 with abs
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371128
Yes.  So can you see how to modify 30-50 to 30-40?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371137
public class Test8 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
    boolean b=in3050(30, 51);
    System.out.println("b is-->"+b);
	}

	
	public static boolean in3050(int a, int b) {
	      if ( a>=30 && a<=40 && b>=30 && b<=40 ) { // a and b in the range 30...40?
	         return true; // Yes!
	      }

	      if ( a>=40 && a<=50 && b>=40 && b<=50 ) { // a and b in the range 40...50?
	         return true; // Yes!
	      }

	      return false; // no succes (otherwhise, we shouldn't have come till here)
	}
}

Open in new window


Even if i remove the inner brackets I still get same results.

Instead of

  if ( (a>=30 && a<=40) &&( b>=30 && b<=40) )
changed as below

  if ( a>=30 && a<=40 && b>=30 && b<=40 )

So no need of inner extra brackets right?
Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371185
In java, comparisons have precedence over boolean operators,

but I was asking whether you could figure out if
30-50 corresponds to Math.abs(a-40)<=10
then
30-40 corresponds to what?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371200
(a-35)<=5
which adds 5 on both sides of 35
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371238
Correct, Math.abs(a-35)<=5 would test for the range 30..40
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40372346
>> Above solution works great..
Then why didn't you give it any points?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now