Solved

copyEndy  challenge

Posted on 2016-08-30
15
97 Views
Last Modified: 2016-09-09
Hi,

I am working one below challenge



 copyEndy
prev  |  next  |  chance
We'll say that a positive int n is "endy" if it is in the range 0..10 or 90..100 (inclusive). Given an array of positive ints, return a new array of length "count" containing the first endy numbers from the original array. Decompose out a separate isEndy(int n) method to test if a number is endy. The original array will contain at least "count" endy numbers.

copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]

i am not clear what above description is talking about. How

copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]
please advise
0
Comment
Question by:gudii9
  • 10
  • 3
  • 2
15 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41777519
read this part clearly:
return a new array of length "count" containing the first "count" (I added this into the text) endy numbers from the original array
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780960
what is endy?
is it is verb or noun or adjective ?

Expected      Run            
copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]      null      X      
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]      null      X      
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]      null      X      
copyEndy([12, 1, 1, 13, 0, 20], 3) → [1, 1, 0]      null      X      
copyEndy([0], 1) → [0]      null      X      
copyEndy([10, 11, 90], 2) → [10, 90]      null      X      
copyEndy([90, 22, 100], 2) → [90, 100]      null      X      
copyEndy([12, 11, 10, 89, 101, 4], 1) → [10]      null      X      
copyEndy([13, 2, 2, 0], 2) → [2, 2]      null      X      
copyEndy([13, 2, 2, 0], 3) → [2, 2, 0]      null      X      
copyEndy([13, 2, 13, 2, 0, 30], 2) → [2, 2]      null      X      
copyEndy([13, 2, 13, 2, 0, 30], 3) → [2, 2, 0]      null      X      
other tests
X      

how above outputs relates to below description

We'll say that a positive int n is "endy" if it is in the range 0..10 or 90..100 (inclusive). //so output array elements should always be between 0 till 10 or 90 till 100?but what is length of output array?how we know(looks like it is count)?
Given an array of positive ints, return a new array of length "count" containing the first endy count numbers from the original array.//first endy count numbers means what??
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780962
copyEndy([13, 2, 2, 0], 2) → [2, 2]      null      X      
copyEndy([13, 2, 2, 0], 3) → [2, 2, 0]      null      X      
copyEndy([13, 2, 13, 2, 0, 30], 2) → [2, 2]      null      X      
copyEndy([13, 2, 13, 2, 0, 30], 3) → [2, 2, 0]

i think i got now. Eliminate all the numbers out of the range and return upto the count even there are 100 endy numbers are there.

let me think on psedo code and code
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 27

Expert Comment

by:rrz
ID: 41786821
Please post the link to the challenge.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786993
oops here is the link

http://codingbat.com/prob/p130124
0
 
LVL 7

Author Comment

by:gudii9
ID: 41788986
public int[] copyEndy(int[] nums, int count) {
  int[] arr=new int[count];
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>0)&&(nums[i]<10))|| (nums[i]>100)&&(nums[i]<10) ){
    arr[i]=nums[i];
  }
  }
  return arr;
}

Open in new window


i wrote asd above and failing some tests as below. please advise
Expected      Run            
copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]      Exception:java.lang.ArrayIndexOutOfBoundsException: 4 (line number:5)      X      
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]      Exception:java.lang.ArrayIndexOutOfBoundsException: 4 (line number:5)      X      
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:5)      X      
copyEndy([12, 1, 1, 13, 0, 20], 3) → [1, 1, 0]      [0, 1, 1]      X      
copyEndy([0], 1) → [0]      [0]      OK      
copyEndy([10, 11, 90], 2) → [10, 90]      [0, 0]      X      
copyEndy([90, 22, 100], 2) → [90, 100]      [0, 0]      X      
copyEndy([12, 11, 10, 89, 101, 4], 1) → [10]      Exception:java.lang.ArrayIndexOutOfBoundsException: 5 (line number:5)      X      
copyEndy([13, 2, 2, 0], 2) → [2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:5)      X      
copyEndy([13, 2, 2, 0], 3) → [2, 2, 0]      [0, 2, 2]      X      
copyEndy([13, 2, 13, 2, 0, 30], 2) → [2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:5)      X      
copyEndy([13, 2, 13, 2, 0, 30], 3) → [2, 2, 0]      Exception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:5)      X      
other tests
X      
Your progress graph for this problem
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41789070
>arr[i]=nums[i];

Open in new window


the issue is that you will copy the found items into the exact same index in the "result" array, and actually are not counting how many you really are copying.

create a variable (j) that defines where the found item should go to in the resulting array
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790674
public int[] copyEndy(int[] nums, int count) {
  int[] arr=new int[count];
  for(int j=0;j<nums.length;j++){
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>0)&&(nums[i]<10))|| (nums[i]>100)&&(nums[i]<10) ){
    arr[j]=nums[i];
  }
  }
  }
  return arr;
}

Open in new window

something like above?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790680
public int[] copyEndy(int[] nums, int count) {
  int j=0;
  int[] arr=new int[count];
 // for(int j=0;j<nums.length;j++){
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>0)&&(nums[i]<10))|| (nums[i]>100)&&(nums[i]<10) ){
    arr[j]=nums[i];
    j++;
  }
  }
 // }
  return arr;
}

Open in new window

something like above?above fails below tests. please advise
Expected      Run            
copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]      [9, 6]      X      
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]      [9, 6, 0]      X      
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]      [1, 1]      OK      
copyEndy([12, 1, 1, 13, 0, 20], 3) → [1, 1, 0]      [1, 1, 0]      OK      
copyEndy([0], 1) → [0]      [0]      OK      
copyEndy([10, 11, 90], 2) → [10, 90]      [0, 0]      X      
copyEndy([90, 22, 100], 2) → [90, 100]      [0, 0]      X      
copyEndy([12, 11, 10, 89, 101, 4], 1) → [10]      [4]      X      
copyEndy([13, 2, 2, 0], 2) → [2, 2]      [2, 2]      OK      
copyEndy([13, 2, 2, 0], 3) → [2, 2, 0]      [2, 2, 0]      OK      
copyEndy([13, 2, 13, 2, 0, 30], 2) → [2, 2]      [2, 2]      OK      
copyEndy([13, 2, 13, 2, 0, 30], 3) → [2, 2, 0]      [2, 2, 0]      OK      
other tests
X      
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790686
public int[] copyEndy(int[] nums, int count) {
  int j=0;
  int[] arr=new int[count];
 // for(int j=0;j<nums.length;j++){
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>=0)&&(nums[i]<=10))|| (nums[i]=>100)&&(nums[i]=<10) ){
    arr[j]=nums[i];
    j++;
  }
  }
 // }
  return arr;
}

Open in new window


why above give below error?

Compile problems:


Error:	if( ((nums[i]>=0)&&(nums[i]<=10))|| (nums[i]=>100)&&(nums[i]=<10) ){
	                                             ^
Syntax error on token ">", delete this token

Open in new window


which side i should put = towards left side of > or right side (even for less than also which side we have to place = . i confuse often with this?)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790687
looks like = always right side.

public int[] copyEndy(int[] nums, int count) {
  int j=0;
  int[] arr=new int[count];
 // for(int j=0;j<nums.length;j++){
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>=0)&&(nums[i]<=10))|| (nums[i]>=100)&&(nums[i]<=10) ){
    arr[j]=nums[i];
    j++;
  }
  }
 // }
  return arr;
}

Open in new window


above fails below tests? please advise
Expected      Run            
copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]      [9, 6]      X      
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]      [9, 6, 0]      X      
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([12, 1, 1, 13, 0, 20], 3) → [1, 1, 0]      [1, 1, 0]      OK      
copyEndy([0], 1) → [0]      [0]      OK      
copyEndy([10, 11, 90], 2) → [10, 90]      [10, 0]      X      
copyEndy([90, 22, 100], 2) → [90, 100]      [0, 0]      X      
copyEndy([12, 11, 10, 89, 101, 4], 1) → [10]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:7)      X      
copyEndy([13, 2, 2, 0], 2) → [2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([13, 2, 2, 0], 3) → [2, 2, 0]      [2, 2, 0]      OK      
copyEndy([13, 2, 13, 2, 0, 30], 2) → [2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([13, 2, 13, 2, 0, 30], 3) → [2, 2, 0]      [2, 2, 0]      OK      
other tests
X      
Your progress graph for this problem
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41790698
(nums[i]>=100)&&(nums[i]<=10)

Open in new window

That will never be true.  You should be finding 90..100 (inclusive).  
Also, you searching all through the nums array. You need to stop when the arr array is full.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41791356
public int[] copyEndy(int[] nums, int count) {
  int j=0;
  int[] arr=new int[count];
 // for(int j=0;j<nums.length;j++){
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>=0)&&(nums[i]<=10))|| (nums[i]>=90)&&(nums[i]<=100) ){
    arr[j]=nums[i];
    j++;
  }
  }
 // }
  return arr;
}

Open in new window


above passign more tests.
Expected      Run            
copyEndy([9, 11, 90, 22, 6], 2) → [9, 90]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([9, 11, 90, 22, 6], 3) → [9, 90, 6]      [9, 90, 6]      OK      
copyEndy([12, 1, 1, 13, 0, 20], 2) → [1, 1]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([12, 1, 1, 13, 0, 20], 3) → [1, 1, 0]      [1, 1, 0]      OK      
copyEndy([0], 1) → [0]      [0]      OK      
copyEndy([10, 11, 90], 2) → [10, 90]      [10, 90]      OK      
copyEndy([90, 22, 100], 2) → [90, 100]      [90, 100]      OK      
copyEndy([12, 11, 10, 89, 101, 4], 1) → [10]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:7)      X      
copyEndy([13, 2, 2, 0], 2) → [2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([13, 2, 2, 0], 3) → [2, 2, 0]      [2, 2, 0]      OK      
copyEndy([13, 2, 13, 2, 0, 30], 2) → [2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:7)      X      
copyEndy([13, 2, 13, 2, 0, 30], 3) → [2, 2, 0]      [2, 2, 0]      OK      
other tests
X      
Your progress graph for this problem

need to figure out how to stop at count. I am not able to give additional condition like j<count in for loop though?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41791366
public int[] copyEndy(int[] nums, int count) {
  int j=0;
  int[] arr=new int[count];
 // for(int j=0;j<nums.length;j++){
  for(int i=0;i<nums.length;i++){
  if( ((nums[i]>=0)&&(nums[i]<=10))|| (nums[i]>=90)&&(nums[i]<=100) ){
    arr[j]=nums[i];
    j++;
    if(j==count) break;
  }
  }
 // }
  return arr;
}

Open in new window


above passed all tests. any improvements, alternate approaches?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41791410
Looks good.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Desingning Refactoring existing code 2 26
AutoIncrement column based of FK 11 55
Glassfish admin console not working 1 32
Java program running SQL query 5 37
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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …

861 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