[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ShiftLeft challenge

Posted on 2016-08-08
21
Medium Priority
?
120 Views
Last Modified: 2016-08-10
Hi,

I am working on below challenge

http://codingbat.com/prob/p105031

Psedo code description of approach :
1. create new array of given array lenght
2. assign each new array i th element with given array i-1 th element
3, return new array

I wrote my code as below

public int[] shiftLeft(int[] nums) {
  int len =nums.length;
  int[] arr=new int[len];
  for(int i=0;i<len;i++){
    arr[i]=nums[i-1];
  }
  return arr;
}

Open in new window






I am not passing all tests by failing edge cases
Expected      Run            
shiftLeft([6, 2, 5, 3]) → [2, 5, 3, 6]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:5)      X      
shiftLeft([1, 2]) → [2, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:5)      X      
shiftLeft([1]) → [1]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:5)      X      
shiftLeft([]) → []      []      OK      
shiftLeft([1, 1, 2, 2, 4]) → [1, 2, 2, 4, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:5)      X      
shiftLeft([1, 1, 1]) → [1, 1, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:5)      X      
shiftLeft([1, 2, 3]) → [2, 3, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:5)      X      
other tests
X      
Your progress graph for this problem


How to improve my design, approach, code? please advise
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
  • 12
  • 9
21 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41749007
What do you think is causing the exception you're seeing? They're all the same, so it must be something that's happening commonly.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749572
Edge case again as i am checking i-1. if i start for loop at 1 to avoid this issue then i have to separately handle i at 0 indexed element which is bit challenging
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41749577
The definition of "edge case" normally refers to situations that happen very rarely and cause your code to fail. In this case, since you're failing almost every test the same way, the problem is, by definition, not an "edge case."

Terminology aside, what do you think is causing these exceptions? What in your code is making them happen?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Author Comment

by:gudii9
ID: 41749580
as
public int[] shiftLeft(int[] nums) {
  int len =nums.length;
  int[] arr=new int[len];
 // arr[len]=nums[0];
  for(int i=0;i<len-1;i++){
   
    arr[i]=nums[i+1];
  }
  return arr;
}

Open in new window

Expected      Run            
shiftLeft([6, 2, 5, 3]) → [2, 5, 3, 6]      [2, 5, 3, 0]      X      
shiftLeft([1, 2]) → [2, 1]      [2, 0]      X      
shiftLeft([1]) → [1]      [0]      X      
shiftLeft([]) → []      []      OK      
shiftLeft([1, 1, 2, 2, 4]) → [1, 2, 2, 4, 1]      [1, 2, 2, 4, 0]      X      
shiftLeft([1, 1, 1]) → [1, 1, 1]      [1, 1, 0]      X      
shiftLeft([1, 2, 3]) → [2, 3, 1]      [2, 3, 0]      X      
other tests
X

need to fix last element
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749587
i thought edge case something happen at start and end of array
0
 
LVL 14

Accepted Solution

by:
CPColin earned 2000 total points
ID: 41749594
You could maybe argue for that definition, depending on the problem.

In the case of this problem, I would consider the edge cases to be determined based on the size of the array. One edge case would be when you pass in an empty array (which is one of the tests) and another would be when you pass in a very large array.

It's not really important what you call it, though, as long as you know where to focus your attention. It looks like you've got everything but the last element sorted out!
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749682
took erasable slate board with erasable marker and also opened eclipse to debug these two are most effective tools for me. i wil solve soon
public class shiftLeft {

	public static void main(String[] args) {
	
		int[] ar={6, 2, 5, 3};
System.out.println("is=="+shiftLeft(ar));
	}

	
	public static int[] shiftLeft(int[] nums) {
		  int len =nums.length;
		  int[] arr=new int[len];
		 // arr[len]=nums[0];
		  for(int i=0;i<len-1;i++){
		   
		    arr[i]=nums[i+1];
		  }
		  return arr;
		}
 
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41749720
		  int len =nums.length;
		  int[] arr=new int[len];
		 // arr[len]=nums[0];
		  for(int i=0;i<len-1;i++){
		   
		    arr[i]=nums[i+1];
		  }
		  arr[len-1]=nums[0];
		  return arr;
		

Open in new window


all tests successful except one

Expected      Run            
shiftLeft([6, 2, 5, 3]) → [2, 5, 3, 6]      [2, 5, 3, 6]      OK      
shiftLeft([1, 2]) → [2, 1]      [2, 1]      OK      
shiftLeft([1]) → [1]      [1]      OK      
shiftLeft([]) → []      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:9)      X      
shiftLeft([1, 1, 2, 2, 4]) → [1, 2, 2, 4, 1]      [1, 2, 2, 4, 1]      OK      
shiftLeft([1, 1, 1]) → [1, 1, 1]      [1, 1, 1]      OK      
shiftLeft([1, 2, 3]) → [2, 3, 1]      [2, 3, 1]      OK      
other tests
OK      

let me fix that also
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749727
public int[] shiftLeft(int[] nums) {
		  int len =nums.length;
		  int[] arr=new int[len];
		 // arr[len]=nums[0];
		 if(len>0){
		  for(int i=0;i<len-1;i++){
		   
		    arr[i]=nums[i+1];
		  }
		  arr[len-1]=nums[0];
		 }else{
		   return arr;
		 }
		  return arr;
		}

Open in new window


above passing all tests. please advise any improvements/modifications
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41749744
That looks good to me. If you want to do it without creating a separate array, you could store nums[0] in a variable before you do the looping, then stick it at the end after the looping is done. But since the challenge says you can do it either way, this will work fine!
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749752
If you want to do it without creating a separate array, you could store nums[0] in a variable before you do the looping, then stick it at the end after the looping is done
how to do without creating new array?
not clear? please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41749826
The code you have in this comment is very close to what you would need. All you have to do is store the first element somewhere before you start looping and put it at the end after your loop finishes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41750597
The code you have in this comment is very close to what you would need. All you have to do is store the first element somewhere before you start looping and put it at the end after your loop finishes.
public int[] shiftLeft(int[] nums) {
  int len =nums.length;
  int[] arr=new int[len];
 // arr[len]=nums[0];
  for(int i=0;i<len-1;i++){
   
    arr[i]=nums[i+1];
  }
  return arr;
}

Open in new window



but i created new array right in above comment code?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41750601
So you did. My mistake. Just skip the line where you create a new array.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41750623
public int[] shiftLeft(int[] nums) {
  int len =nums.length;
//  int[] arr=new int[len];
 // arr[len]=nums[0];
  for(int i=0;i<len-1;i++){
   
    arr[i]=nums[i+1];
  }
  return arr;
}

Open in new window


if i skip compilation error at assignment statement

 arr[i]=nums[i+1];

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 41750735
Well yeah, you need to update that line to use the right array on both sides of the = sign.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41750813
how to use right array on both sides if i have only one array and i am not creating new array? confusing
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41750823
Try it and see what happens.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41751152
Right means opposite of left
i thought right means opposite of incorrect.
public int[] shiftLeft(int[] nums) {
  int len =nums.length;
//  int[] arr=new int[len];
 int x=nums[0];
  for(int i=0;i<len-1;i++){
   
    nums[i]=nums[i+1];
  }
  nums[len-1]=x;
  return nums;
}

Open in new window


above passes all tests except one fails
public int[] shiftLeft(int[] nums) {
  int len =nums.length;
//  int[] arr=new int[len];
 int x=nums[0];
  for(int i=0;i<len-1;i++){
   
    nums[i]=nums[i+1];
  }
  nums[len-1]=x;
  return nums;
}

Open in new window

how to fix that index out of bound for empty array?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41751155
public int[] shiftLeft(int[] nums) {
  int len =nums.length;
//  int[] arr=new int[len];
if(len>0){
 int x=nums[0];
  for(int i=0;i<len-1;i++){
   
    nums[i]=nums[i+1];
  }
  nums[len-1]=x;
  return nums;
}

else
return nums;



}

Open in new window


i fixed as above. any improvements?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41751198
Right means opposite of left
i thought right means opposite of incorrect.

In this case, it meant both!

i fixed as above. any improvements?

Looks good to me! Glad you solved the problem with the empty array.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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