?
Solved

sameEnds challenge

Posted on 2016-08-08
25
Medium Priority
?
150 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
[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
  • 14
  • 8
  • 2
  • +1
25 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 41748596
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
ID: 41749434
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 500 total points
ID: 41749486
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
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 7

Author Comment

by:gudii9
ID: 41749591
ok
0
 
LVL 7

Author Comment

by:gudii9
ID: 41751189
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
ID: 41751199
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
ID: 41751217
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
ID: 41751270
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
ID: 41751434
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
ID: 41752006
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
ID: 41752477
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
ID: 41752506
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
 
LVL 7

Author Comment

by:gudii9
ID: 41752522
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
ID: 41752575
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
ID: 41752595
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
ID: 41752650
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
ID: 41752689
I am talking about the code in  https:#a41752575  that ALWAYS RETURNS TRUE.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 500 total points
ID: 41752979
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
ID: 41753079
>> 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 28

Accepted Solution

by:
rrz earned 1000 total points
ID: 41753124
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
ID: 41753235
Sure
0
 
LVL 7

Author Comment

by:gudii9
ID: 41753313
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
ID: 41753318
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 28

Expert Comment

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

Author Comment

by:gudii9
ID: 41753774
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

764 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