?
Solved

matchUp  challenge

Posted on 2016-08-30
9
Medium Priority
?
162 Views
Last Modified: 2016-09-07
I am working on below challenge
http://codingbat.com/prob/p139677

Psedo code:
1. loop first array
2. loop second array within
3. check to see first element of first array with first element of second  then second then third till end
4. if matches increase counter by 1
4. return counter

I wrote my code as below

public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
     for(int j=0;j<b.length;j++){
       if(a[i]==b[i]){
         count++;
       }
     }
    
  }
  return count;
}

Open in new window





I am not passing all tests
Expected      Run            
matchUp(["aa", "bb", "cc"], ["aaa", "xx", "bb"]) → 1      0      X      
matchUp(["aa", "bb", "cc"], ["aaa", "b", "bb"]) → 2      0      X      
matchUp(["aa", "bb", "cc"], ["", "", "ccc"]) → 1      0      X      
matchUp(["", "", "ccc"], ["aa", "bb", "cc"]) → 1      0      X      
matchUp(["", "", ""], ["", "bb", "cc"]) → 0      3      X      
matchUp(["aa", "bb", "cc"], ["", "", ""]) → 0      0      OK      
matchUp(["aa", "", "ccc"], ["", "bb", "cc"]) → 1      0      X      
matchUp(["x", "y", "z"], ["y", "z", "x"]) → 0      0      OK      
matchUp(["", "y", "z"], ["", "y", "x"]) → 1      6      X      
matchUp(["x", "y", "z"], ["xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["x", "y", "z"], ["xx", "yyy", ""]) → 2      0      X      
matchUp(["b", "x", "y", "z"], ["a", "xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["aaa", "bb", "c"], ["aaa", "xx", "bb"]) → 1      3      X      
other tests

How to improve/modify my design, code and any other alternate approaches. 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
9 Comments
 
LVL 3

Assisted Solution

by:Donna
Donna earned 1000 total points
ID: 41777465
Wow, look at all these. Fall semester must have started, LOL

You can't use == with strings I don't think...I think you need to use stringcomp() , or better yet, .equals() method ...look in the API for methods in the String class and comparing strings. == does not work with STrings, only sometimes.

read up on compareTo() , compareToIgnoreCase() , equals(), equalsIgnoreCase(); , contentEquals(CharSequence cs); , ...and whatever else looks close to what you want to do. But probably equals() will work for you, it returns a bool.

https://docs.oracle.com/javase/7/docs/api/
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780954
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
     for(int j=0;j<b.length;j++){
       if(a[i].equals(b[j])){
         count++;
       }
     }
    
  }
  return count;
}

Open in new window


i used equals passed some tests and failing few

Expected      Run            
matchUp(["aa", "bb", "cc"], ["aaa", "xx", "bb"]) → 1      1      OK      
matchUp(["aa", "bb", "cc"], ["aaa", "b", "bb"]) → 2      1      X      
matchUp(["aa", "bb", "cc"], ["", "", "ccc"]) → 1      0      X      
matchUp(["", "", "ccc"], ["aa", "bb", "cc"]) → 1      0      X      
matchUp(["", "", ""], ["", "bb", "cc"]) → 0      3      X      
matchUp(["aa", "bb", "cc"], ["", "", ""]) → 0      0      OK      
matchUp(["aa", "", "ccc"], ["", "bb", "cc"]) → 1      1      OK      
matchUp(["x", "y", "z"], ["y", "z", "x"]) → 0      3      X      
matchUp(["", "y", "z"], ["", "y", "x"]) → 1      2      X      
matchUp(["x", "y", "z"], ["xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["x", "y", "z"], ["xx", "yyy", ""]) → 2      0      X      
matchUp(["b", "x", "y", "z"], ["a", "xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["aaa", "bb", "c"], ["aaa", "xx", "bb"]) → 1      2      X      
other tests

please advise
0
 
LVL 28

Accepted Solution

by:
rrz earned 1000 total points
ID: 41786775
The challenge states:
Count the number of times that the 2 strings are non-empty and start with the same char.
 You are checking for equality of the two elements.  You need to check for empty Strings and not to count them. You need to find the first character of the String and check their equality.  Look at
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-     
Also, you only need one loop. Just step through both in one pass.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:gudii9
ID: 41787167
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
    // for(int j=0;j<b.length;j++){
       if(a[0].equals(b[0])){
         count++;
     //  }
     }
    
  }
  return count;
}
/*The challenge states:
Count the number of times that the 2 strings are non-empty and start with the same char.
 You are checking for equality of the two elements.  You need to check for empty Strings 
 and not to count them. You need to find the first character of the String and check 
 their equality.  Look at 
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-     
Also, you only need one loop. Just step through both in one pass.*/

Open in new window


something like above. It passes some and fails some tests
Expected      Run            
matchUp(["aa", "bb", "cc"], ["aaa", "xx", "bb"]) → 1      0      X      
matchUp(["aa", "bb", "cc"], ["aaa", "b", "bb"]) → 2      0      X      
matchUp(["aa", "bb", "cc"], ["", "", "ccc"]) → 1      0      X      
matchUp(["", "", "ccc"], ["aa", "bb", "cc"]) → 1      0      X      
matchUp(["", "", ""], ["", "bb", "cc"]) → 0      3      X      
matchUp(["aa", "bb", "cc"], ["", "", ""]) → 0      0      OK      
matchUp(["aa", "", "ccc"], ["", "bb", "cc"]) → 1      0      X      
matchUp(["x", "y", "z"], ["y", "z", "x"]) → 0      0      OK      
matchUp(["", "y", "z"], ["", "y", "x"]) → 1      3      X      
matchUp(["x", "y", "z"], ["xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["x", "y", "z"], ["xx", "yyy", ""]) → 2      0      X      
matchUp(["b", "x", "y", "z"], ["a", "xx", "yyy", "zzz"]) → 3      0      X      
matchUp(["aaa", "bb", "c"], ["aaa", "xx", "bb"]) → 1      3      X      
other tests
X
0
 
LVL 7

Author Comment

by:gudii9
ID: 41787171
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
    String s1=a[i];
    String s2=b[i];
    // for(int j=0;j<b.length;j++){
    if(!s1.equals("")&&!s2.equals("")){
       if( s1.charAt(0)==s2.charAt(0) ){
         count++;
     //  }
       }
     }
    
  }
  return count;
}

/*The challenge states:
Count the number of times that the 2 strings are non-empty and start with the same char.
 You are checking for equality of the two elements.  You need to check for empty Strings 
 and not to count them. You need to find the first character of the String and check 
 their equality.  Look at 
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-     
Also, you only need one loop. Just step through both in one pass.*/

Open in new window


you mean like above right. Above passes all tests. Any improvements or alternate approaches for this?
0
 
LVL 28

Expert Comment

by:rrz
ID: 41787201
Yes, your code is good.
My code is almost the same as your code.
public int matchUp(String[] a, String[] b) {
  int count=0;
  for(int i=0;i<a.length;i++){
       if(a[i].length() != 0 && b[i].length() != 0 && a[i].charAt(0) == b[i].charAt(0)){
         count++;
       }
  }
  return count;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41787778
yours is more optimized. thanks
0
 
LVL 32

Expert Comment

by:awking00
ID: 41788139
Good place to use continue -
int count=0;
  for(int i=0;i<a.length;i++){
       if (a.isEmpty || b.isEmpty) {
           continue;
       }
       if ( a[i].charAt(0) == b[i].charAt(0) ) {
         count++;
       }
  }
0
 
LVL 7

Author Comment

by:gudii9
ID: 41788960
i like above continue approach

if one of below  non empty ie false (if both empty continue to next element of iteration right?)

a.isEmpty || b.isEmpty


then it checks below and increments count++ if first characters equal??
   if ( a[i].charAt(0) == b[i].charAt(0) ) {
         count++;
       }

Open in new window

0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Make the most of your online learning experience.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

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