Solved

withoutTen challenge

Posted on 2016-08-14
14
87 Views
Last Modified: 2016-08-22
Hi,

I am working on below challenge

http://codingbat.com/prob/p196976

My psuedo code of the logical approach is
1 loop through given array
2 check and see if all elements are not 10 if yes then return same array
3.if given element is 10 replace it with 0
4. display the array with all zeros at the front and non zero numbers at the end



I wrote my code as below
public int[] withoutTen(int[] nums) {
  int[] result=null;
  for(int i=0;i<nums.length;i++){
    if(nums[i]!=10){
      result=nums;
    }
    
     else if(nums[i]==10){
      nums[i]=0;
    }
  }
  return result;
}

Open in new window






I am not passing all tests
Expected      Run            
withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0]      [1, 0, 0, 2]      X      
withoutTen([10, 2, 10]) → [2, 0, 0]      [0, 2, 0]      X      
withoutTen([1, 99, 10]) → [1, 99, 0]      [1, 99, 0]      OK      
withoutTen([10, 13, 10, 14]) → [13, 14, 0, 0]      [0, 13, 0, 14]      X      
withoutTen([10, 13, 10, 14, 10]) → [13, 14, 0, 0, 0]      [0, 13, 0, 14, 0]      X      
withoutTen([10, 10, 3]) → [3, 0, 0]      [0, 0, 3]      X      
withoutTen([1]) → [1]      [1]      OK      
withoutTen([13, 1]) → [13, 1]      [13, 1]      OK      
withoutTen([10]) → [0]      null      X      
withoutTen([]) → []      null      X      
other tests
X      

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 125 total points
Comment Utility
You should keep track of the amount of non-10 numbers you've found. Place the non-10 numbers in the result array and for each 10 found put a 0 at the end of the result array (minus the index of the non-10 numbers found).
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
i will try
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
OK
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 125 total points
Comment Utility
Or another approach, knowing that all elements of an int array will already be initialised to 0, you just have to loop the original array and place any non-10 numbers into the result array. The key point though is you need one variable, call it "i", to keep track of the index in the original array but you also need another variable to keep track of where you are putting the numbers into the result array, because those two indexes will be different, so you need two variables.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public int[] withoutTen(int[] nums) {
  int[] result=null;
  for(int i=0;i<nums.length;i++){
    if(nums[i]!=10){
      result=nums;
    }
    
     else if(nums[i]==10){
      nums[i]=0;
    }
  }
  //return result;
  
  
  
		int len=nums.length;
		int[] arr=new int[len];
		int front=0;
		int back=len-1;
		  for(int i=0;i<nums.length;i++){		    
		    if(nums[i]%2==1){	
		    	arr[front]=nums[i];
		    	front++;
		    	
		  }
		  else if(nums[i]%2==0){
			  arr[back]=nums[i];
		    	back--;
		  }
		  }
		  return arr;
		
  
}

Open in new window

Expected      Run            
withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0]      [1, 2, 0, 0]      OK      
withoutTen([10, 2, 10]) → [2, 0, 0]      [0, 2, 0]      X      
withoutTen([1, 99, 10]) → [1, 99, 0]      [1, 99, 0]      OK      
withoutTen([10, 13, 10, 14]) → [13, 14, 0, 0]      [13, 14, 0, 0]      OK      
withoutTen([10, 13, 10, 14, 10]) → [13, 14, 0, 0, 0]      [13, 0, 14, 0, 0]      X      
withoutTen([10, 10, 3]) → [3, 0, 0]      [3, 0, 0]      OK      
withoutTen([1]) → [1]      [1]      OK      
withoutTen([13, 1]) → [13, 1]      [13, 1]      OK      
withoutTen([10]) → [0]      [0]      OK      
withoutTen([]) → []      []      OK      
other tests
X

i integrated my solution with evenodd challenge solution and failing couple of tests. how to fox them. please advise
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Ok, working with the code that you have, first you should really take out the first loop that you have (lines 2 -> 12), because all that does is change any 10's to 0's. This sounds ok at first but the problem is that you would lose track of which 0's were real zero's and which were there because they were initially 10. Now there might not be any test case that would highlight that problem, but it is best to be sure.

Now, what your left with is a loop that copies odd numbers to the front of the array and copies even numbers to the back of the array.

But what you want (if using this code) is to copy non-10 numbers to the front of the array and puts a zero in for a 10 to the back of the array.


So, yes, there are similarities there and you can use the code that you have, but the changes required could be worked out with the bold words I have written above, ie. you have lines in your code that check if the number is odd or even (lines 21, 26), so you have to change those so that they check for non-10 and 10 numbers.

Also, the other difference in the bold numbers above is instead of copying the even (line 27) you have to put 0 in place of the 10.


Hopefully, with those hints above, you can change just the 3 lines of code required to get the code working properly.
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
public int[] withoutTen(int[] nums) {		  
				int len=nums.length;
				int[] arr=new int[len];
				int front=0;
				int back=len-1;
				  for(int i=0;i<nums.length;i++){		    
				    if(nums[i]%10==1||nums[i]%10==2||nums[i]%10==3||nums[i]%10==4||nums[i]%10==5||nums[i]%10==6||nums[i]%10==7||nums[i]%10==8||nums[i]%10==9){	
				    	arr[front]=nums[i];
				    	front++;
				    	
				  }
				  else {
					  arr[back]=nums[i];
				    	back--;
				  }
				    if(arr[i]%10==0){
				    	arr[i]=0;
				    }
				  }
				  return arr;
				
		  
}

Open in new window


i modified and failing below two tests. please advise
Expected      Run            
withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0]      [1, 2, 0, 0]      OK      
withoutTen([10, 2, 10]) → [2, 0, 0]      [2, 10, 0]      X      
withoutTen([1, 99, 10]) → [1, 99, 0]      [1, 99, 0]      OK      
withoutTen([10, 13, 10, 14]) → [13, 14, 0, 0]      [13, 14, 0, 0]      OK      
withoutTen([10, 13, 10, 14, 10]) → [13, 14, 0, 0, 0]      [13, 14, 10, 0, 0]      X      
withoutTen([10, 10, 3]) → [3, 0, 0]      [3, 0, 0]      OK      
withoutTen([1]) → [1]      [1]      OK      
withoutTen([13, 1]) → [13, 1]      [13, 1]      OK      
withoutTen([10]) → [0]      [0]      OK      
withoutTen([]) → []      []      OK      
other tests
OK
0
 
LVL 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
You might think about creating an array to return with the same length as nums, keeping in mind that such an array will contain all zeroes. You could then populate it with the values from nums that are not ten.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
public int[] withoutTen(int[] nums) {		  
				int len=nums.length;
				int[] arr=new int[len];
				int front=0;
				int back=len-1;
				  for(int i=0;i<nums.length;i++){		    
				    if(nums[i]%10==1||nums[i]%10==2||nums[i]%10==3||nums[i]%10==4||nums[i]%10==5||nums[i]%10==6||nums[i]%10==7||nums[i]%10==8||nums[i]%10==9){	
				    	arr[front]=nums[i];
				    	front++;
				    	
				  }
				 /* else {
					  arr[back]=nums[i];
				    	back--;
				  }
				    if(arr[i]%10==0){
				    	arr[i]=0;
				    }*/
				  }
				  return arr;
				
		  
}

Open in new window


that passed all tests
Expected      Run            
withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0]      [1, 2, 0, 0]      OK      
withoutTen([10, 2, 10]) → [2, 0, 0]      [2, 0, 0]      OK      
withoutTen([1, 99, 10]) → [1, 99, 0]      [1, 99, 0]      OK      
withoutTen([10, 13, 10, 14]) → [13, 14, 0, 0]      [13, 14, 0, 0]      OK      
withoutTen([10, 13, 10, 14, 10]) → [13, 14, 0, 0, 0]      [13, 14, 0, 0, 0]      OK      
withoutTen([10, 10, 3]) → [3, 0, 0]      [3, 0, 0]      OK      
withoutTen([1]) → [1]      [1]      OK      
withoutTen([13, 1]) → [13, 1]      [13, 1]      OK      
withoutTen([10]) → [0]      [0]      OK      
withoutTen([]) → []      []      OK      
other tests
OK      

any imrovement or refinement to above code? otherwise will close it
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
This  
if(nums[i]%10==1||nums[i]%10==2||nums[i]%10==3||nums[i]%10==4||nums[i]%10==5||nums[i]%10==6||nums[i]%10==7||nums[i]%10==8||nums[i]%10==9)

can be shortened to
if nums[i] != 0
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
you mean
nums[i]%10 != 0 

Open in new window

right?

above also passed all
public int[] withoutTen(int[] nums) {		  
				int len=nums.length;
				int[] arr=new int[len];
				int front=0;
				int back=len-1;
				  for(int i=0;i<nums.length;i++){		    
				    if(nums[i]%10 != 0){	
				    	arr[front]=nums[i];
				    	front++;
				    	
				  }
				 /* else {
					  arr[back]=nums[i];
				    	back--;
				  }
				    if(arr[i]%10==0){
				    	arr[i]=0;
				    }*/
				  }
				  return arr;
				
		  
}

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
you mean
1:nums[i]%10 != 0

Yes, that is exactly what I meant :-)
1
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
if (nums[i]%10 != 0) {

Open in new window


The above is NOT what you want either. Since all the tests are passing there is obviously no test case for it, but if the input array had any 0, 20, 30, 40, 50, 60, ... etc. it would NOT do what you want it to do.

Just do this...

if (nums[i] != 10) {

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

11 Experts available now in Live!

Get 1:1 Help Now