gudii9
asked on
sameEnds challenge
Hi,
I am trying below challenge
http://codingbat.com/prob/p134300
i read description as below
i have not understood the description of the challenge clearly. Please advise how ends are same and what is n there?
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?
ASKER
what is difference between N and n here? Are both same as per challenge or different? please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok
ASKER
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?
ASKER
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);
}
Expected Runi think got meaning of the challenge
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
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
ASKER
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);
}
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);
}
}
not sure why arr2 is always coming[0,0]
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.
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.
ASKER
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);
}
}
above gives below error. please advise
There are no errors shown.
I won't look at he errors until I see the pseudo code.
I won't look at he errors until I see the pseudo code.
ASKER
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);
}
}
and then below errorException in thread "main" java.lang.ArrayIndexOutOfB
at SameEnds.sameEnds(SameEnds
at SameEnds.main(SameEnds.jav
illegalArray.png
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.
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.
ASKER
All you have to do is make n comparisons.let me try
You can if you want to,but how?
ASKER
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;
}
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 OKsameEnds([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;
}
}
output istrue
passed many . need to fix few
WHERE IS YOUR PSUEDO CODE?
Did you notice that your code ALWAYS RETURNS TRUE?
You actually FAILED EVERY TEST!!!
Did you notice that your code ALWAYS RETURNS TRUE?
You actually FAILED EVERY TEST!!!
ASKER
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;
}
are you talking about above code. it is returning false in if loop and true in else loop?
I am talking about the code in https:#a41752575 that ALWAYS RETURNS TRUE.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sure
ASKER
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
ASKER
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;
}
above passed all tests. there is type i used len-len by mistake earlier.
any improvements/modifications
That is good. But, you don't need the else block.
ASKER
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;
}
above also worked fine
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.