gudii9
asked on
post4 challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p164144
Psedo code description of approach :
1. create new arrray//not sure on size?
2. assign each element with new element arrays until not equal to 4 from backwars
3. if above true return till there
4 if false break for loop
I wrote my code as below
I am not passing all tests due to edge cases
I am working on below challenge
http://codingbat.com/prob/p164144
Psedo code description of approach :
1. create new arrray//not sure on size?
2. assign each element with new element arrays until not equal to 4 from backwars
3. if above true return till there
4 if false break for loop
I wrote my code as below
public int[] post4(int[] nums) {
int len =nums.length;
int[] arr=new int[len];
for(int i=len;i>0;i--){
if(nums[i]!=4){
arr[i]=nums[i];
}
else{
break;
}
}
return arr;
}
I am not passing all tests due to edge cases
Expected RunHow to improve my design, approach, code? please advise
post4([2, 4, 1, 2]) → [1, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 4 (line number:9) X
post4([4, 1, 4, 2]) → [2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 4 (line number:9) X
post4([4, 4, 1, 2, 3]) → [1, 2, 3] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 5 (line number:9) X
post4([4, 2]) → [2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 2 (line number:9) X
post4([4, 4, 3]) → [3] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 3 (line number:9) X
post4([4, 4]) → [] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 2 (line number:9) X
post4([4]) → [] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 1 (line number:9) X
post4([2, 4, 1, 4, 3, 2]) → [3, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 6 (line number:9) X
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 6 (line number:9) X
post4([3, 4, 3, 2]) → [3, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 4 (line number:9) X
other tests
X
Your progress graph for this problem
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hint: Read and try to understand the thrown exceptions. This is a good exercise to learn debugging. Debugging is extremely important when someone wants to become a good programmer.
I agree with mccarl, and also mostly agree with Zoppo.
Hm - why just mostly?
Because endless and countless previous explanations of how these things work have not been able to penetrate the asker's thinking.
:D
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
mccarl showed you a useful method in the API at
https://www.experts-exchange.com/questions/28962285/pre4-challenge.html?anchorAnswerId=41753818#a41753818
Look at the API
http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
directly below the copyOf method there is the copyOfRange method which you can use in this challenge.
https://www.experts-exchange.com/questions/28962285/pre4-challenge.html?anchorAnswerId=41753818#a41753818
Look at the API
http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
directly below the copyOf method there is the copyOfRange method which you can use in this challenge.
ASKER
public int[] post4(int[] nums) {
int len = nums.length;
int val=0;
for (int i = 0; i < len-1; i++) {
if (nums[i] == 4) {
val=i;
break;
}
}
int[] arr = new int[val];
for (int k = val; k < len-1; k++) {
arr[k] =nums[k];
}
return arr;
}
Expected Run
post4([2, 4, 1, 2]) → [1, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 1 (line number:12) X
post4([4, 1, 4, 2]) → [2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 0 (line number:12) X
post4([4, 4, 1, 2, 3]) → [1, 2, 3] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 0 (line number:12) X
post4([4, 2]) → [2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 0 (line number:12) X
post4([4, 4, 3]) → [3] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 0 (line number:12) X
post4([4, 4]) → [] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 0 (line number:12) X
post4([4]) → [] [] OK
post4([2, 4, 1, 4, 3, 2]) → [3, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 1 (line number:12) X
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 0 (line number:12) X
post4([3, 4, 3, 2]) → [3, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 1 (line number:12) X
other tests
X
I am getting array index out of bound. please advise
ASKER
import java.util.Arrays;
public class Post4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 2, 4, 1, 2 };
System.out.println("value is" + Arrays.toString(post4(arr)));
}
public static int[] post4(int[] nums) {
int len = nums.length;
int val = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val = i;
break;
}
}
int val2=len-(val+1);
int[] arr = new int[val2];
for (int k = val2; k < len; k++) {
arr[k] = nums[k];
}
return arr;
}
}
i modified bit getting same error
Exception in thread "main" java.lang.ArrayIndexOutOfB
at Post4.post4(Post4.java:23)
at Post4.main(Post4.java:8)
ASKER
arr[k] = nums[k];
compiler complaining about assigining 5 digit array on right into 3 digit array on left. not sure how to get last few digits after 4 into new array arr?
Try..
arr[k-val] = nums[k];
arr[k-val] = nums[k];
ASKER
import java.util.Arrays;
public class PreFour {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 2,4,1,0,8 };
System.out.println("value===>" + Arrays.toString(pre4(ar)));
}
public static int[] pre4(int[] nums) {
/* You don't always have to try and do things in one go. Just use two loops....
the first to find the index of the first 4, now you can create your new
array with the correct size,
and then use a second loop to copy the elements from the original to new array.*/
int len = nums.length;
int val=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val=i;
break;
}
}
int[] arr = new int[val];
for (int k = 0; k < val; k++) {
arr[k] =nums[k];
}
return arr;
}
}
pre4 works ok where as on similar lines post4 not woring
import java.util.Arrays;
public class Post4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 2, 4, 1, 0,8 };
System.out.println("value is" + Arrays.toString(post4(arr)));
}
public static int[] post4(int[] nums) {
int len = nums.length;
int val = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val = i;
break;
}
}
int val2=len-(val);
int[] arr = new int[val2];
for (int k = (val2-1); k < len; k++) {
arr[k] = nums[k];
}
return arr;
}
/*public int[] pre4(int[] nums) {
int len = nums.length;
int val=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val=i;
break;
}
}
int[] arr = new int[val];
for (int k = 0; k < val; k++) {
arr[k] =nums[k];
}
return arr;
}
*/
}
not sure where is the issue?
ASKER
import java.util.Arrays;
public class Post4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 2, 4, 1, 0,8 };
System.out.println("value is" + Arrays.toString(post4(arr)));
}
public static int[] post4(int[] nums) {
int len = nums.length;
int val = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val = i;
break;
}
}
int val2=len-(val);
int[] arr = new int[val2];
for (int k = (val2-1); k < len; k++) {
arr[k-val] = nums[k];
}
return arr;
}
/*public int[] pre4(int[] nums) {
int len = nums.length;
int val=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val=i;
break;
}
}
int[] arr = new int[val];
for (int k = 0; k < val; k++) {
arr[k] =nums[k];
}
return arr;
}
*/
}
gives below
value is[0, 0, 0, 8]
ASKER
arr[k-val] = nums[k];
what is the rule both left and right array needs to be same size?
ASKER
why k-val??
ASKER
import java.util.Arrays;
public class Post4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 2, 4, 1, 0,8 };
System.out.println("value is" + Arrays.toString(post4(arr)));
}
public static int[] post4(int[] nums) {
int len = nums.length;
int val = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val = i;
break;
}
}
int val2=len-(val+1);
int[] arr = new int[val2];
for (int k = (val2-1); k < len; k++) {
arr[k-(val+1)] = nums[k];
}
return arr;
}
/*public int[] pre4(int[] nums) {
int len = nums.length;
int val=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val=i;
break;
}
}
int[] arr = new int[val];
for (int k = 0; k < val; k++) {
arr[k] =nums[k];
}
return arr;
}
*/
}
i got correct result above
value is[1, 0, 8]
but coding bat failing
Expected Run
post4([2, 4, 1, 2]) → [1, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:13) X
post4([4, 1, 4, 2]) → [2] [0, 4, 2] X
post4([4, 4, 1, 2, 3]) → [1, 2, 3] [0, 0, 2, 3] X
post4([4, 2]) → [2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:13) X
post4([4, 4, 3]) → [3] [4, 3] X
post4([4, 4]) → [] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:13) X
post4([4]) → [] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:13) X
post4([2, 4, 1, 4, 3, 2]) → [3, 2] [0, 4, 3, 2] X
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2] [0, 0, 0, 2, 2] X
post4([3, 4, 3, 2]) → [3, 2] Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: -1 (line number:13) X
other tests
X
Your progress graph for this problem
ASKER
public int[] post4(int[] nums) {
int len = nums.length;
int val = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
val = i;
break;
}
}
int val2=len-(val+1);
int[] arr = new int[val2];
for (int k = (val+1); k < len; k++) {
arr[k-(val+1)] = nums[k];
}
return arr;
}
i am close now , failing few
Expected Run
post4([2, 4, 1, 2]) → [1, 2] [1, 2] OK
post4([4, 1, 4, 2]) → [2] [1, 4, 2] X
post4([4, 4, 1, 2, 3]) → [1, 2, 3] [4, 1, 2, 3] X
post4([4, 2]) → [2] [2] OK
post4([4, 4, 3]) → [3] [4, 3] X
post4([4, 4]) → [] [4] X
post4([4]) → [] [] OK
post4([2, 4, 1, 4, 3, 2]) → [3, 2] [1, 4, 3, 2] X
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2] [1, 4, 2, 2, 2] X
post4([3, 4, 3, 2]) → [3, 2] [3, 2] OK
other tests
X
Your progress graph for this problem
ASKER
failing more than one 4.
how to find last array and get elements after that to new array?
how to find last array and get elements after that to new array?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i think backwards rings bell in my mind as easy let me try that
ASKER
public int[] post4(int[] nums) {
int len = nums.length;
int val = 0;
for (int i = len-1; i >=0; i--) {
if (nums[i] == 4) {
val = i;
break;
}
}
int val2=len-(val+1);
int[] arr = new int[val2];
for (int k = (val+1); k < len; k++) {
arr[k-(val+1)] = nums[k];
}
return arr;
}
Expected Runboom...backward approach worked fine
post4([2, 4, 1, 2]) → [1, 2] [1, 2] OK
post4([4, 1, 4, 2]) → [2] [2] OK
post4([4, 4, 1, 2, 3]) → [1, 2, 3] [1, 2, 3] OK
post4([4, 2]) → [2] [2] OK
post4([4, 4, 3]) → [3] [3] OK
post4([4, 4]) → [] [] OK
post4([4]) → [] [] OK
post4([2, 4, 1, 4, 3, 2]) → [3, 2] [3, 2] OK
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2] [2, 2, 2] OK
post4([3, 4, 3, 2]) → [3, 2] [3, 2] OK
other tests
OK
All Correct
Another way is to create a variable to hold the value of the index of the last 4. That second way is similar to what I did in my solution to the twoTwo challenge that we a few days ago. In that challenge, I created a variable to remember what happen earlier in the looping.
let me refresh memory and try that approach also
Your backwards stepping for loop is good.
ASKER
if challenge say for example middle 4 then second approach only works right not forward not backward?
I don't understand that last question. But let's stay focused on this challenge.
ASKER
twotwo challenge is not yet clear to me. But i will close this question as i got proper approach on this.
ASKER
That second way is similar to what I did in my solution to the twoTwo challenge that we a few days ago. In that challenge, I created a variable to remember what happen earlier in the looping.
i am stil thinking, i was bit unclear on this way? can you please advise
This is similar to what you did above here, except it doesn't use a break statement. When the for loop completes the fourIndex variable is equal to the index of the last four.
public int[] post4(int[] nums) {
int fourIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 4) {
fourIndex = i;
}
}
return Arrays.copyOfRange(nums, fourIndex + 1, nums.length);
}