Solved

bunnyEars2 challenge

Posted on 2016-09-09
6
163 Views
Last Modified: 2016-09-14
Hi,

I am going through below challenge.

http://codingbat.com/prob/p107330


I have not understood below description

Recursion-1 > bunnyEars2
prev  |  next  |  chance
We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) we'll say have 3 ears, because they each have a raised foot. Recursively return the number of "ears" in the bunny line 1, 2, ... n (without loops or multiplication).

bunnyEars2(0) → 0
bunnyEars2(1) → 2
bunnyEars2(2) → 5
So even bunnies has 2 ears and odd ones have 3 ears and we have to return total ears?
please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
ID: 41791985
You should figure out the first challenge before you continue to its sequel. You've essentially just posted ten questions that all boil down to, "I don't understand recursion." Figure out one challenge first, so you get the hang of how recursion works, before you proceed to a pile of others.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 41796538
In the bunnyEars challenge, I tried to show you how if you keep passing in (bunnies - 1) to your method and adding 2 you eventually will reach the base case. You need to get that method to work, then the modification for this challenge simply becomes adding 2 for an even number of bunnies and 3 for an odd number. Keep in mind that when you pass in (bunnies - 1), you're changing an odd number to an even number and vice versa.
1
 
LVL 7

Author Comment

by:gudii9
ID: 41796805
pseudeo code:
1. if o bunnies return ears 0;
2. if one bunny ears 0 i.e 2+bunnyEars(1-1)
3. if n bunnies and odd numbered 2+bunneyEars(n-1)
4. if n bunnies and even numbered 3+bunneyEars(n-1)
5 return ear count
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 7

Author Comment

by:gudii9
ID: 41796811
public int bunnyEars2(int bunnies) {
  int result=0;
 if(bunnies==0){
   result=0;
 }
 else{
   if(bunnies%2==1){
   result=2+bunnyEars2(bunnies-1);
   }
    else if(bunnies%2==0){
   result=3+bunnyEars2(bunnies-1);
   }
 }
 return result;
}

Open in new window


above passed all tests. any improvement or alternate approaches?
0
 
LVL 32

Expert Comment

by:awking00
ID: 41797848
No need for the result variable -
public int bunnyEars2(int bunnies) {
 if(bunnies==0){
   return 0;
 }
 if(bunnies%2==1) {
   return 2+bunnyEars2(bunnies-1);
 } else  {
   return 3+bunnyEars2(bunnies-1);
 }
}

15:}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41798231
public int bunnyEars2(int bunnies) {
 if(bunnies==0){
   return 0;
 } 
 if(bunnies%2==1) {
   return 2+bunnyEars2(bunnies-1);
 } else  {
   return 3+bunnyEars2(bunnies-1);
 }
}

Open in new window

i like above approach much better without result
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What's wrong with this code? 4 36
Java program running SQL query 5 48
moto4g plus phone finger print 3 35
Strange router problem - can't access hotmail.com 14 54
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

749 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