Solved

has77  challenge

Posted on 2016-08-03
9
66 Views
Last Modified: 2016-08-08
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

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;
}

Open in new window




I am not passing all tests
Expected      Run            
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      
How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
You're missing the case where the last two numbers are 7's because you stop the loop too early for the first test to pick up that case.

I could put an extra test in the code to detect it, but you might like to solve that much yourself?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
an extra test in the code to detect it

where and how? i am also trying now
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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;
}

Open in new window

if i put nums.length-1 in for loop failing tests

Expected      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.ArrayIndexOutOfBoundsException: 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.ArrayIndexOutOfBoundsException: 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.ArrayIndexOutOfBoundsException: 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.ArrayIndexOutOfBoundsException: 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.ArrayIndexOutOfBoundsException: 2 (line number:6)      X      
has77([7]) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 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
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
Comment Utility
This is how I would write it to demonstrate cover of the missing case, though it's not necessarily the most efficient way to code it:

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+1]==7&&nums[i+2]==7)||(nums[i]==7&&nums[i+2]==7)){
      res=true;
      return res;
    }
  }
  return res;
}

Open in new window


The middle test is the one I've added.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Assisted Solution

by:awking00
awking00 earned 250 total points
Comment Utility
To possibly avoid the index out of bounds problem, you might consider starting at index 2 and searching backwards.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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;
	}

Open in new window



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
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
>>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&&nums[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
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Needed to add another condition - where (index - 2 == 7 and index - 1 == 7)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This is an explanation of a simple data model to help parse a JSON feed
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now