Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array11 challenge

Posted on 2016-09-21
16
Medium Priority
?
88 Views
Last Modified: 2016-09-22
Hi,

I am working on below challenge.
http://codingbat.com/prob/p135988
Psedo code:
1. create new array of the portion of original array length

2. check to see if if given index has 1 and next index also has 1iif yes return increment count by 1
3.else repeat recursion

I wrote my code as below
public int array11(int[] nums, int index) {
  int count=0;
  
   if(nums.length==0){
    return 0;
  }
  
/*   if(index>=nums.length){
    return 0;
  }*/
  if(index>=nums.length){
    return 0;
  }
    else if((nums[index]==1)&&nums[index+1]==11){
      return count++;
    }
    else return array11(nums,index+1);
  
}

Open in new window


i am failing some tests
Expected      Run            
array11([1, 2, 11], 0) → 1      0      X      
array11([11, 11], 0) → 2      0      X      
array11([1, 2, 3, 4], 0) → 0      0      OK      
array11([1, 11, 3, 11, 11], 0) → 3      0      X      
array11([11], 0) → 1      0      X      
array11([1], 0) → 0      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:14)      X      
array11([], 0) → 0      0      OK      
array11([11, 2, 3, 4, 11, 5], 0) → 2      0      X      
array11([11, 5, 11], 0) → 2      0      X      
other tests
OK      

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
  • 8
  • 4
  • 4
16 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 41809091
public int array11(int[] nums, int index) {
  int count=0;
  
  /* if(nums.length==0){
    return 0;
  }*/
  
   if(index>=nums.length){
    return 0;
  }
  if(nums[index]==11){
    return 1+array11(nums,index+1);
  }
   /* else if((nums[index]==1)&&nums[index+1]==11){
      return count++;
    }*/
    else return array11(nums,index+1);
  
}

Open in new window


above passed all. any improvements or alternate approaches?
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1000 total points
ID: 41809264
you should remove the commented parts and the final 'else'.

since you have a return in the if block above the else statement  the 'else' keyword can (and should) be omitted.

Sara
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41809763
Pseudo code above is wrong.

Working solution (one of) is:

        public int array11(int[] nums, int index)
        {
            int count = 0;

            if (index >= nums.Length)
            {
                return  0;// we need to stop recursion immediately and prevent any index out of range exceptions
            }
            else if (nums[index] == 11)
            {
                count = 1;
            }

            return count + array11(nums, index + 1);
        }

Open in new window



Output on my machine (I wrote in C#):

array11([11, 11], 0)  = 2
array11([1, 2, 3, 4], 0)  = 0
array11([1, 11, 3, 11, 11], 0)  = 3
array11([11], 0)  = 1
array11([1], 0)  = 0
array11([], 0)  = 0
array11([11, 2, 3, 4, 11, 5], 0)  = 2
array11([11, 5, 11], 0)  = 2
0
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!

 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 total points
ID: 41809841
Below is Java code you may run on the web site:

public int array11(int[] nums, int index) {
              int count = 0;

            if (index >= nums.length)
            {
                return  0;// we need to stop recursion immediately and prevent any index out of range exceptions
            }
            else if (nums[index] == 11)
            {
                count = 1;
            }

            return count + array11(nums, index + 1);
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41810742
you should remove the commented parts and the final 'else'.

if i remove else getting below error
public int array11(int[] nums, int index) {
  int count=0;
  if(index>=nums.length){
    return 0;
  }
  if(nums[index]==11){
    return 1+array11(nums,index+1);
  }
  
}

Open in new window


Compile problems:


Error:      public int array11(int[] nums, int index) {
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This method must return a result of type int

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

see Example Code to help with compile problems

please advise
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41810888
if i remove else getting below error

your last valid code:

public int array11(int[] nums, int index) {
  int count=0;
  
  /* if(nums.length==0){
    return 0;
  }*/
  
   if(index>=nums.length){
    return 0;
  }
  if(nums[index]==11){
    return 1+array11(nums,index+1);
  }
   /* else if((nums[index]==1)&&nums[index+1]==11){
      return count++;
    }*/
    else return array11(nums,index+1);
  
}

Open in new window


if you would remove commented statements and final 'else' you get

public int array11(int[] nums, int index) {
  int count=0;
 
  if(index>=nums.length){
    return 0;
  }
  if(nums[index]==11){
    return 1+array11(nums,index+1);
  }
  return array11(nums,index+1);

}

Open in new window


note, i said the final 'else' and not the final 'else statement/block'.

the 'else' is not needed when the if block before ends with a return statement.

Sara
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810944
note, i said the final 'else' and not the final 'else statement/block'.

the 'else' is not needed when the if block before ends with a return statement.

got it. new to me else not needed when if block before ends with a return. any other detailed links on this concept to read or any other examples on it?
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41810970
new to me else not needed when if block before ends with a return

that is not a concept but basic boolean logic:

if (some_condition)
{
       do_something();
       return;
}
// if you reached this point, some_condition was false and 
// therefore you are effectively in an 'else' block here. 

do_something_else();
return;

Open in new window


the above is equivalent to

if (some_condition)
{
       do_something();
       return;
}
else
{
     do_something_else();
     return;
}

Open in new window


note, i would recommend to always use {} for if and else.


Sara
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811125
that is not a concept but basic boolean logic:
if (some_condition)
{
       do_something();
       return;
}
// if you reached this point, some_condition was false and
// therefore you are effectively in an 'else' block here.

do_something_else();
return;

Select all
 
Open in new window

the above is equivalent to

if (some_condition)
{
       do_something();
       return;
}
else
{
     do_something_else();
     return;
}
above is very clear
after if it is implicitly else whether we write or not

But recommended to use explicit else with { and }
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41811226
But recommended to use explicit else with { and }

actually, it is a matter of style and therefore it is recommended to always do it same way and use the method which is better readable. the readability may be different for you and me. i like the first better (without else), but you may use the second one. anyway, you should not omit the barckets if using 'else'.

Sara
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811241
sure
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811261
public int array11(int[] nums, int index) {
  int count=0;
 
  if(index>=nums.length){
    return 0;
  }
   if(nums[index]==11){
    return 1+array11(nums,index+1);
  }
  return array11(nums,index+1);

}

Open in new window


how above different from below

public int array11(int[] nums, int index) {
  int count=0;
 
  if(index>=nums.length){
    return 0;
  }
  else if(nums[index]==11){
    return 1+array11(nums,index+1);
  }
  return array11(nums,index+1);

}

Open in new window


when to use above when to use below.
i confuse always with above 2 use cases?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811264
both of above constructs passes all tests?

any preference one over other here or in general?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41811515
There is no difference really. In both cases you check the first condition:
(index>=nums.length)
If it is not satisfied - you test second condition:
(nums[index]==11)

If you want you may check what is compiled CIL code. I have suspicion it will be exactly same.

One note: there is no point to declare
int count=0;
You do not use the variable.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811635
If you want you may check what is compiled CIL code.

how to check this?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41811704
You may use something like http://ilspy.net/

Try to search for "How can I view MSIL / CIL generated by C# compiler?"
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

715 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