Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Start1 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p109660

i wrote as below and failing some tests
public int start1(int[] a, int[] b) {
int num=0;

for(int i=0;i<a.length;i++){
if(a[0]==1)  
  num++;
  }
  
  for(int i=0;i<b.length;i++){
if(b[0]==1)  
  num++;
  }
  return num;
  
}

Open in new window


Expected	Run		
start1({1, 2, 3}, {1, 3}) → 2	5	X	    
start1({7, 2, 3}, {1}) → 1	1	OK	    
start1({1, 2}, {}) → 1	2	X	    
start1({}, {1, 2}) → 1	2	X	    
start1({7}, {}) → 0	0	OK	    
start1({7}, {1}) → 1	1	OK	    
start1({1}, {1}) → 2	2	OK	    
start1({7}, {8}) → 0	0	OK	    
start1({}, {}) → 0	0	OK	    
start1({1, 3}, {1}) → 2	3	X	    
other tests
X	    

Open in new window


how to improve and fix the code. please advise
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How many times do you increment num?
Why do you iterate?
public int start1(int[] a, int[] b) {
  
 boolean avalid = a.length>0;
 boolean bvalid = b.length>0;
 
 if(avalid&bvalid){return a[0]+b[0]==2?2:a[0]==1?1:b[0]==1?1:0;}
 
 if(avalid) {return a[0]==1?1:0;}
 
 if(bvalid) {return b[0]==1?1:0;}
 
 return 0;
    
}

Open in new window

a[0]+b[0]==2 depends on assumptions about the element values which are not guaranteed by the problem specification, even if they happen to be satisfied by the test examples.
depends on assumptions about the element values which are not guaranteed by the problem specification,

which might be true under any other condition apart from this one, where there are ONLY 2 arrays.
"Start with 2 int arrays" is guaranteed by the problem specification.
(on the other hand, a[0]>=1 is not guaranteed by the problem specification)
This is the problem spec :

>>Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element. <<

I don't think you have followed the logic in my code -  which does NOT include this statement of yours :
(on the other hand, a[0]>=1

Open in new window

i don't think you have followed the logic in my comments, which do not claim that your code includes "a[0]>=1",
rather they claim that the problem spec does not include "a[0]>=1"
and that your code does include "a[0]+b[0]==2"
None of the test arrays start with "0".
And I do not claim that any of the test arrays start with "0", nor do I claim that any of them start with "-5",
In fact, I explicitly said "even if they happen to be satisfied by the test examples".
So given that one of the arrays could start with 0, but the other with 2, or one array holds a complementary negative value, then yes, the test would fail.
The problem - or one of them - with this Codingbat's site tests, is that the datatypes - like our ints here - are never constrained to a "customised" range, yet neither are they qualified as being within the range of Java int, nevertheless the "other tests" that the site subjects the code to are not exposed either. That means that it becomes nearly impossible to tell what sort of a challenge it really is - one can only assume that the "other tests" exclude 0 values in arrays, as well as negative values. This isn't really very good for a full appreciation of where code can fall over in the real world, because the test data is incomplete and therefore meaningless.
Avatar of gudii9

ASKER

public int start1(int[] a, int[] b) {
int num=0;

//if(a.length>=1 & b.length>=1)
//{
if(a[0]==1)
{  
  num++;
}

if(b[0]==1)  
{
  num++;
}
  return num;
  
}


//}

//else
//{
//return null;
//}
//}

Open in new window

i tried as above and passed few tests but failing the edge cases. please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

when should i iterate and when i should not iterate in arrays or in general. Please advise
In general, iterate when you are doing a lot of the same thing.
Avatar of gudii9

ASKER

lot of the same thing.

same thing means for example if we want to check each and every number of array is equal to even or odd number etc?

any other examples of same thing??

please advise
I wont be participating in any more of your questions, Asker. (Just in case you wonder why, I don't have time to invest on such vast scales for such little progress).