Solved

sameEnds challenge

Posted on 2016-08-08
25
70 Views
Last Modified: 2016-08-13
Hi,

I am trying below challenge

http://codingbat.com/prob/p134300

i read description as below
Array-2 > sameEnds
prev  |  next  |  chance
Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.

sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false
Go...Save, Compile, Run



public boolean sameEnds(int[] nums, int len) {
  return true;
}
Go
Shorter output
Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      true      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      true      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 1) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 2) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 0) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 5) → true      true      OK      
sameEnds([1, 1, 1], 0) → true      true      OK      
sameEnds([1, 1, 1], 1) → true      true      OK      
sameEnds([1, 1, 1], 2) → true      true      OK      
sameEnds([1, 1, 1], 3) → true      true      OK      
sameEnds([1], 1) → true      true      OK      
sameEnds([], 0) → true      true      OK      
sameEnds([4, 2, 4, 5], 1) → false      true      X      
other tests
X      


i have not understood the description of the challenge clearly. Please advise how ends are same and what is n there?
0
Comment
Question by:gudii9
  • 14
  • 8
  • 2
  • +1
25 Comments
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
If you don't understand the Challenge, you must work on the pseudo code until you do.  
This is much more difficult than any of the other challenges you have posted.  I'm not sure you are ready for it.
Do not post any more code until an expert has passed on your pseudo code.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
what is difference between N and n here?  Are both same as per challenge or different? please advise
0
 
LVL 27

Assisted Solution

by:d-glitch
d-glitch earned 125 total points
Comment Utility
n or N is the number of elements that much match at the beginning and end of the array.
The challenge uses them interchangeably.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
ok
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3

what it means by ends are same for n=0 and n=2.(are they saying beginning two characters at index 0, index 1 are 5,6 which are same as, last but one(penultimate), last ultimate character 5,6 are same so true??

i have not got meaning or clue out of it?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public boolean sameEnds(int[] nums, int len) {
  int lengt=nums.length;
  int[] arr1=new int[len];
  int[] arr2=new int[len];
  
  for(int i=0;i<len;i++){
    
    arr1[i]=nums[i];
   
  }
  for(int i=(lengt-len);i<len;i++){
   arr2[i]=nums[i];
  }
  return arr1.equals(arr2);
}

Open in new window

Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      false      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      false      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      false      OK      
sameEnds([1, 2, 5, 2, 1], 1) → true      false      X      
sameEnds([1, 2, 5, 2, 1], 2) → false      false      OK      
sameEnds([1, 2, 5, 2, 1], 0) → true      false      X      
sameEnds([1, 2, 5, 2, 1], 5) → true      false      X      
sameEnds([1, 1, 1], 0) → true      false      X      
sameEnds([1, 1, 1], 1) → true      false      X      
sameEnds([1, 1, 1], 2) → true      false      X      
sameEnds([1, 1, 1], 3) → true      false      X      
sameEnds([1], 1) → true      false      X      
sameEnds([], 0) → true      false      X      
sameEnds([4, 2, 4, 5], 1) → false      false      OK      
other tests
X      
i think got meaning of the challenge

psedo code:
1. loop through given array
2. create two new arrays
3. assign each element of given array until len to first newly created array
4. similary assign each element of given array back ward elements upto len to second newly created array
5. return equality of the both arrays
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public boolean sameEnds(int[] nums, int len) {
  int lengt=nums.length;
  int[] arr1=new int[len];
  int[] arr2=new int[len];
  
  for(int i=0;i<len;i++){
    
    arr1[i]=nums[i];
   
  }
  for(int j=(lengt-len);j<len;j++){
   arr2[j]=nums[j];
  }
  return arr1.equals(arr2);
}

Open in new window

public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
	public static boolean sameEnds(int[] nums, int len) {
		  int lengt=nums.length;
		  int len2=lengt-len;
		  int[] arr1=new int[len];
		  int[] arr2=new int[len];
		  
		  for(int i=0;i<len;i++){
		    
		    arr1[i]=nums[i];
		   
		  }
		  for(int j=len2;j<len;j++){
		   arr2[j]=nums[j];
		  }
		  return arr1.equals(arr2);
		}


}

Open in new window

not sure why arr2 is always coming[0,0]
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
Your pseudo code is not correct.
1. You don't have to loop through the array.
2. You don't have to make any new arrays.
3. There is nothing in the challenge about backwards.

You have to make n comparisons.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
	public static boolean sameEnds(int[] nums, int len) {
		  int lengt=nums.length;
		  int len2=lengt-len;
		  int[] arr1=new int[len];
		  int[] arr2=new int[len];
		  
		  for(int i=0;i<len;i++){
		    
		    arr1[i]=nums[i];
		   
		  }
		  for(int j=len2, k=0;j<lengt-1;j++){
		   arr2[j]=nums[k];
		   k++;
		  }
		  return arr1.equals(arr2);
		}


}

Open in new window


above gives below error. please advise
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
There are no errors shown.
I won't look at he errors until I see the pseudo code.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Your pseudo code is not correct.
1. You don't have to loop through the array.
2. You don't have to make any new arrays.
3. There is nothing in the challenge about backwards.

corrected psedo code of the logic


1. loop through given array
2. create two new arrays one representing front part array {5,6}--->{5, 6, 45, 99, 13, 5, 6}
;other arr2 representing back part array({5,6}--->{5, 6, 45, 99, 13, 5, 6};

3. assign each element of given array until len to first newly created array to build first array ie arr1
4. similary assign each element of given array back ward elements upto len to second newly created array.
5. return equality of the both arrays arr1 and arr2 to make sure it returns true if front and back portions are same

sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true

i am getting illegal array while debugging for left had side array at line 24  as attached when i run my code
for(int j=len2;j<lengt;j++){
              arr2[j]=nums[j];
public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
	public static boolean sameEnds(int[] nums, int len) {
		  int lengt=nums.length;
		  int len2=lengt-len;
		  int[] arr1=new int[len];
		  int[] arr2=new int[len2];
		  
		  for(int i=0;i<len;i++){
		    
		    arr1[i]=nums[i];
		   
		  }
		  for(int j=len2;j<lengt;j++){
		   arr2[j]=nums[j];
		   //k++;
		  }
		  return arr1.equals(arr2);
		}


}

Open in new window

and then below error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at SameEnds.sameEnds(SameEnds.java:24)
      at SameEnds.main(SameEnds.java:8)
illegalArray.png
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
Your pseudo code is not correct.

1. You don't have to loop through the array.
     In fact, you should not loop through the array.
2. You don't have to make any new arrays.
    You can if you want to, but it is very wasteful.
3. There is nothing in the challenge about backwards.

All you have to do is make n comparisons.

There is no reason to look at your code because YOUR PSUEDO CODE IS WRONG.
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 7

Author Comment

by:gudii9
Comment Utility
All you have to do is make n comparisons.
let me try
You can if you want to,
but how?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((len-len)+i)] ){
		    result=false;
		  }
		  else {
		    result=true;
		  }
		}
		return result;
		  
		
}

Open in new window


Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      true      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      true      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 1) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 2) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 0) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 5) → true      true      OK      
sameEnds([1, 1, 1], 0) → true      true      OK      
sameEnds([1, 1, 1], 1) → true      true      OK      

Open in new window

sameEnds([1, 1, 1], 2) → true      true      OK      
sameEnds([1, 1, 1], 3) → true      true      OK      
sameEnds([1], 1) → true      true      OK      
sameEnds([], 0) → true      true      OK      
sameEnds([4, 2, 4, 5], 1) → false      true      X      
other tests
X      
public class SameEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar={5, 6, 45, 99, 13, 5, 6};
		int len =2;
		System.out.println(sameEnds(ar,len));

	}
	
public static boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((len-len)+i)] ){
		    result=false;
		  }
		  else {
		    result=true;
		  }
		}
		return result;
		  
		
}


}

Open in new window

output is
 true

passed many . need to fix few
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
WHERE IS YOUR PSUEDO CODE?
Did you notice that your code ALWAYS RETURNS TRUE?
You actually FAILED EVERY TEST!!!
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public boolean sameEnds(int[] nums, int len) {
  
boolean result=true;

for(int i=0;i<len;i++){
  
  if(nums[i]!=nums[(len-n)+i)]{
    result=false;
  }
  else {
    result=true;
  }
}
return result;
  
}

Open in new window


are you talking about above code. it is returning false in if loop and true in else loop?
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
I am talking about the code in  https:#a41752575  that ALWAYS RETURNS TRUE.
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 125 total points
Comment Utility
The code may be a little challenging but the task is relatively easy to understand. Whatever len is find that many ints at the beginning of the array, then see if they are the same and in the same order as the last n ints of the array.
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
>> awking00

I agree.  The challenge test does not say anything about order:
     Return true if the group of N numbers at the start and end of the array are the same.

Test output results do indicate that the same order interpretation is correct, making the challenge fairly straight forward.  It would be trickier if order did not matter.
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
Comment Utility
Your last code is almost there.  You should write the array  on a piece of paper.  The graphic layout will help you. Step through your code by hand and point to each element as it being accessed.  Also, ask yourself; What is the last element of an array?  
nums[ ? ] == last element
1
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Sure
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
You should write the array  on a piece of paper.  The graphic layout will help you. Step through your code by hand and point to each element as it being accessed.  Also, ask yourself; What is the last element of an array?  
nums[ ? ] == last element

above suggestion is very useful. I am esp. using erasable board and marker. If it get more complicated opening eclipse debugger same time
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;
		int totalLen=nums.length;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((totalLen-len)+i)] ){
		    result=false;
		  }
		  else {
		    result=true;
		  }
		}
		return result;
		  
		
}

Open in new window


above passed all tests. there is type i used len-len by mistake earlier.

any improvements/modifications/other approaches to this challenge?
0
 
LVL 27

Expert Comment

by:rrz
Comment Utility
That is good. But, you don't need the else block.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public boolean sameEnds(int[] nums, int len) {
		  
		boolean result=true;
		int totalLen=nums.length;

		for(int i=0;i<len;i++){
		  
		  if(nums[i]!=nums[((totalLen-len)+i)] ){
		    result=false;
		  }
		
		}
		return result;
		  
		
}

Open in new window


above also worked fine
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

Title # Comments Views Activity
for i loop in grovy 1 32
maven example error 3 34
Scripting vs. Programming languages 25 110
Re-position the objects 7 46
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…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 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

16 Experts available now in Live!

Get 1:1 Help Now