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

asked on

scoresSpecial challenge

Hi,

i am working one below challenge
http://codingbat.com/prob/p140485

i was not clear on below description


Given two arrays, A and B, of non-negative int scores. A "special" score is one which is a multiple of 10, such as 40 or 90. Return the sum of largest special score in A and the largest special score in B. To practice decomposition, write a separate helper method which finds the largest special score in an array. Write your helper method after your scoresSpecial() method in the JavaBat text area.


how below results are 40 each? please advise
scoresSpecial([12, 10, 4], [2, 20, 30]) → 40
scoresSpecial([20, 10, 4], [2, 20, 10]) → 40
scoresSpecial([12, 11, 4], [2, 20, 31]) → 20
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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
Good luck to you in your mysterious and futile mission.
Avatar of gudii9

ASKER

public int scoresSpecial(int[] a, int[] b) {
  int result1=0;
  int result2=0;
  int initialA=0;
  int intitiaB=0;
  for(int i=0;i<a.length;i++){
    if(a[i]%10==0){
      result1=result1+a[i];
    }
    
  }
  for(int j=0;j<b.length;j++){
    if(b[j]%10==0){
      result2=result1+b[j];
    }
  }
  return (result1+result2);
}

Open in new window


something like above?
xpected      Run            
scoresSpecial([12, 10, 4], [2, 20, 30]) → 40      50      X      
scoresSpecial([20, 10, 4], [2, 20, 10]) → 40      70      X      
scoresSpecial([12, 11, 4], [2, 20, 31]) → 20      20      OK      
scoresSpecial([1, 20, 2, 50], [3, 4, 5]) → 50      70      X      
scoresSpecial([3, 4, 5], [1, 50, 2, 20]) → 50      20      X      
scoresSpecial([10, 4, 20, 30], [20]) → 50      140      X      
scoresSpecial([10, 4, 20, 30], [20]) → 50      140      X      
scoresSpecial([10, 4, 20, 30], [3, 20, 99]) → 50      140      X      
scoresSpecial([10, 4, 20, 30], [30, 20, 99]) → 60      140      X      
scoresSpecial([], [2]) → 0      0      OK      
scoresSpecial([], [20]) → 20      20      OK      
scoresSpecial([14, 10, 4], [4, 20, 30]) → 40      50      X      
other tests
X

how to get biggest 10 multiple from array a and also array b to add them up and return?
Avatar of gudii9

ASKER

public int scoresSpecial(int[] a, int[] b) {
  int result1=0;
  int result2=0;
  int initialA=0;
  int initialB=0;
  for(int i=0;i<a.length;i++){
    if(a[i]%10==0&&initialA<a[i]){
     
      initialA=a[i];
       result1=initialA;
    }
    
  }
  for(int j=0;j<b.length;j++){
    if(b[j]%10==0&&initialB<b[j]){
      
      initialB=b[j];
      result2=initialB;
    }
  }
  return (result1+result2);
}

Open in new window

something like above. Above passes all tests.
Any improvements or alternate approaches?
Avatar of gudii9

ASKER

public int scoresSpecial(int[] a, int[] b) {

  return (scoresUtil(a)+scoresUtil(b));
}

  public int scoresUtil(int[] x){
  int result1=0;
  int result2=0;
  int initialA=0;
  int initialB=0;
  for(int i=0;i<x.length;i++){
    if(x[i]%10==0&&initialA<x[i]){
     
      initialA=x[i];
       result1=initialA;
    }
   
  }

 return result1;
}

Open in new window

more refactored to helper method as above.
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
I think the code is ok and complete, at least I see nothing missing
Very funny - you deleted my code for being "complete" (full was the term used). So what's the difference now?
@krakatoa,
So what's the difference now?
The difference is that I was just rewriting gudii9 code that he posted at  
https://www.experts-exchange.com/questions/28966729/scoresSpecial-challenge.html?anchorAnswerId=41790660#a41790660
The point is both your code and mine passed all the tests, and you made no more attempt to get him to iron out his mistakes and recode it than I did - so imho, the situation is still not kosher.
Avatar of gudii9

ASKER

You could clean it up to make it more readable.  

i like naming largest and your code is more refined.