teenParty challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p177181

I tried my code as below
public int teaParty(int tea, int candy) {
if((tea==2*candy)||(candy==2*tea)){
return 2;
}

  else if((tea+candy<5)){
return 1;
}
else
return 0;
  
}

Open in new window


I am getting below result

Expected      Run            
teaParty(6, 8) → 1      0      X         
teaParty(3, 8) → 0      0      OK         
teaParty(20, 6) → 2      0      X         
teaParty(12, 6) → 2      2      OK         
teaParty(11, 6) → 1      0      X         
teaParty(11, 4) → 0      0      OK         
teaParty(4, 5) → 0      0      OK         
teaParty(5, 5) → 1      0      X         
teaParty(6, 6) → 1      0      X         
teaParty(5, 10) → 2      2      OK         
teaParty(5, 9) → 1      0      X         
teaParty(10, 4) → 0      0      OK         
teaParty(10, 20) → 2      2      OK         
other tests
X         

how to  improve my approach and design of this challenge. How do i make a

graphical venn or some other relevant diagram to design it before writing single

line of code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
This is a logic problem. You need to think on the possible outcomes.

The requirements say that:
- a party is good (1) if both tea and candy are at least 5
- if either tea or candy is at least double the amount of the other one, the party is great (2)
- if either tea or candy is less than 5, the party is always bad (0)

First, prioritize:
1. if either tea or candy is less than 5, the party is always bad (0)
2. if either tea or candy is at least double the amount of the other one, the party is great (2)
3. a party is good (1) if both tea and candy are at least 5

Then rewrite as math operations:
1. candy < 5 or tea < 5   then party = 0
2. candy >= tea * 2 or tea >= candy * 2 then party = 2
3. candy >= 5 and tea >= 5 then party = 1

Then, look if all conditions are needed: do you need to check for 3 if 1 or 2 is true?

Now, you can begin coding :)

HTH,
Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gurpsbassiCommented:
tea==2*candy)||(candy==2*tea

gudii9 re-read the requirement. It says the
if either tea or candy is at least double the amount of the other one

so to do equalsTo is not correct. It ought to be >=
0
ozoCommented:
return
  tea<5||candy<5
  ?
    0
  : tea>=candy*2||candy>=tea*2
    ?
      2
    :
      1
  ;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gudii9Author Commented:
First, prioritize:
1. if either tea or candy is less than 5, the party is always bad (0)
2. if either tea or candy is at least double the amount of the other one, the party is great (2)
3. a party is good (1) if both tea and candy are at least 5

how to prioritize? which comes first which comes last?


tea==2*candy)||(candy==2*tea


gudii9 re-read the requirement. It says the
if either tea or candy is at least double the amount of the other one


so to do equalsTo is not correct. It ought to be >=

i fixed as below
public int teaParty(int tea, int candy) {
if((tea>=2*candy)||(candy>=2*tea)){
return 2;
}

  else if((tea+candy>5)){
return 1;
}
else
return 0;
 
}
0
gudii9Author Commented:
Expected      Run            
teaParty(6, 8) → 1      1      OK         
teaParty(3, 8) → 0      2      X         
teaParty(20, 6) → 2      2      OK         
teaParty(12, 6) → 2      2      OK         
teaParty(11, 6) → 1      1      OK         
teaParty(11, 4) → 0      2      X         
teaParty(4, 5) → 0      1      X         
teaParty(5, 5) → 1      1      OK         
teaParty(6, 6) → 1      1      OK         
teaParty(5, 10) → 2      2      OK         
teaParty(5, 9) → 1      1      OK         
teaParty(10, 4) → 0      2      X         
teaParty(10, 20) → 2      2      OK         
other tests
X         

still failing couple of them please advise
0
gudii9Author Commented:
First, prioritize:
1. if either tea or candy is less than 5, the party is always bad (0)
2. if either tea or candy is at least double the amount of the other one, the party is great (2)
3. a party is good (1) if both tea and candy are at least 5

Then rewrite as math operations:
1. candy < 5 or tea < 5   then party = 0
2. candy >= tea * 2 or tea >= candy * 2 then party = 2
3. candy >= 5 and tea >= 5 then party = 1

above helped a lot to write below code which passed all tests


Expected      Run            
teaParty(6, 8) → 1      1      OK         
teaParty(3, 8) → 0      0      OK         
teaParty(20, 6) → 2      2      OK         
teaParty(12, 6) → 2      2      OK         
teaParty(11, 6) → 1      1      OK         
teaParty(11, 4) → 0      0      OK         
teaParty(4, 5) → 0      0      OK         
teaParty(5, 5) → 1      1      OK         
teaParty(6, 6) → 1      1      OK         
teaParty(5, 10) → 2      2      OK         
teaParty(5, 9) → 1      1      OK         
teaParty(10, 4) → 0      0      OK         
teaParty(10, 20) → 2      2      OK         
other tests
OK         

but how to prioritize as you did for all these kind of challenges which onne to put first and then 2nd then 3rd ...then last??
i think that is million dollar question in front of me now
0
gudii9Author Commented:
public int teaParty(int tea, int candy) {
if((tea<5)||(candy<5)){
return 0;
}
  else if((tea>=2*candy)||(candy>=2*tea)){
return 2;
}
  else
  return 1;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.