gudii9
asked on
has77 challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p168357
Psedo code description of approach :
1. loop through given array
2. check if an(i th) element and i+1 th adjacent are 7's or i, i+2 are 7's
3. if yes return true
4.if no return false
I wrote my code as below
I am not passing all tests
I am working on below challenge
http://codingbat.com/prob/p168357
Psedo code description of approach :
1. loop through given array
2. check if an(i th) element and i+1 th adjacent are 7's or i, i+2 are 7's
3. if yes return true
4.if no return false
I wrote my code as below
public boolean has77(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-2;i++){
if((nums[i]==7&&nums[i+1]==7)||(nums[i]==7&&nums[i+2]==7)){
res=true;
return res;
}
}
return res;
}
I am not passing all tests
Expected RunHow to improve my design, approach, code? please advise
has77([1, 7, 7]) → true false X
has77([1, 7, 1, 7]) → true true OK
has77([1, 7, 1, 1, 7]) → false false OK
has77([7, 7, 1, 1, 7]) → true true OK
has77([2, 7, 2, 2, 7, 2]) → false false OK
has77([2, 7, 2, 2, 7, 7]) → true false X
has77([7, 2, 7, 2, 2, 7]) → true true OK
has77([7, 2, 6, 2, 2, 7]) → false false OK
has77([7, 7, 7]) → true true OK
has77([7, 1, 7]) → true true OK
has77([7, 1, 1]) → false false OK
has77([1, 2]) → false false OK
has77([1, 7]) → false false OK
has77([7]) → false false OK
other tests
X
ASKER
an extra test in the code to detect it
where and how? i am also trying now
ASKER
public boolean has77(int[] nums) {
boolean res=false;
for(int i=0;i<nums.length-1;i++){
if((nums[i]==7&&nums[i+1]==7)||(nums[i]==7&&nums[i+2]==7)){
res=true;
return res;
}
}
return res;
}
if i put nums.length-1 in for loop failing testsExpected Run
has77([1, 7, 7]) → true true OK
has77([1, 7, 1, 7]) → true true OK
has77([1, 7, 1, 1, 7]) → false false OK
has77([7, 7, 1, 1, 7]) → true true OK
has77([2, 7, 2, 2, 7, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 6 (line number:6) X
has77([2, 7, 2, 2, 7, 7]) → true true OK
has77([7, 2, 7, 2, 2, 7]) → true true OK
has77([7, 2, 6, 2, 2, 7]) → false false OK
has77([7, 7, 7]) → true true OK
has77([7, 1, 7]) → true true OK
has77([7, 1, 1]) → false false OK
has77([1, 2]) → false false OK
has77([1, 7]) → false false OK
has77([7]) → false false OK
other tests
OK
Correct for more than half the tests
Your progress graph for this problem
if i say i<nums.length failing more
Expected Run
has77([1, 7, 7]) → true true OK
has77([1, 7, 1, 7]) → true true OK
has77([1, 7, 1, 1, 7]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 5 (line number:6) X
has77([7, 7, 1, 1, 7]) → true true OK
has77([2, 7, 2, 2, 7, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 6 (line number:6) X
has77([2, 7, 2, 2, 7, 7]) → true true OK
has77([7, 2, 7, 2, 2, 7]) → true true OK
has77([7, 2, 6, 2, 2, 7]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 6 (line number:6) X
has77([7, 7, 7]) → true true OK
has77([7, 1, 7]) → true true OK
has77([7, 1, 1]) → false false OK
has77([1, 2]) → false false OK
has77([1, 7]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 2 (line number:6) X
has77([7]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 1 (line number:6) X
other tests
X
Correct for more than half the tests
Your progress graph for this problem
Forget It! -- delete my code for this problem
Progress graphs:
Your progress graph for this problem
any tips on mastering these edge cases so that i can be right 100% all the time
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public boolean has77(int[] nums) {
boolean res = false;
for (int i = 0; i < nums.length - 2; i++) {
if ((nums[i] == 7 && nums[i + 1] == 7) || (nums[i] == 7 && nums[i + 2] == 7)
|| ((nums[i + 1]) == 7 && (nums[i + 2] == 7))) {
res = true;
return res;
}
}
return res;
}
above passed all tests. one lesson i should take to myself is analyzing failing tests in this case {1,7,7} so i have to handle i+1 and i+2 case as well as above
ASKER
To possibly avoid the index out of bounds problem, you might consider starting at index 2 and searching backwards.
what do you mean by searching backwards?
can you please elaborate on that?
>>what do you mean by searching backwards?
can you please elaborate on that?<<
You wrote -
>>for(int i=0;i<nums.length-1;i++){
if((nums[i]==7&&nums[i+1]= =7)||(nums [i]==7&&nu ms[i+2]==7 )){
res=true;<<
which caused ([2, 7, 2, 2, 7, 2]) to fail with indexOutOfBounds exception
because i is going from 0 to 5 so when it looks for a value at i + 2 when i = 4 or i + 1 when i = 5, there is no such index for that array.
However, if you increment i from 2 to 5 and first search for index 2 == 7 and (index 2-1 == 7 or index 2-2 == 7) up to index 5 == 7 and (index 5-1 == 7 or index 5-2 == 7), you will always be evaluating a valid index
can you please elaborate on that?<<
You wrote -
>>for(int i=0;i<nums.length-1;i++){
if((nums[i]==7&&nums[i+1]=
res=true;<<
which caused ([2, 7, 2, 2, 7, 2]) to fail with indexOutOfBounds exception
because i is going from 0 to 5 so when it looks for a value at i + 2 when i = 4 or i + 1 when i = 5, there is no such index for that array.
However, if you increment i from 2 to 5 and first search for index 2 == 7 and (index 2-1 == 7 or index 2-2 == 7) up to index 5 == 7 and (index 5-1 == 7 or index 5-2 == 7), you will always be evaluating a valid index
Needed to add another condition - where (index - 2 == 7 and index - 1 == 7)
I could put an extra test in the code to detect it, but you might like to solve that much yourself?