5 Replies - 544 Views - Last Post: 21 June 2013 - 12:20 PM Rate Topic: -----

#1 redwoolwinterhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-June 13

CodingBat Java > AP-1 > commonTwo with a single While Loop

Posted 19 June 2013 - 04:09 PM

Hello!

This problem says it can (and should) be done with a single pass over both arrays. I've got a mostly working single while below, but sometimes it counts one more or one less than it should. I have swapped the assignment of temp with the str check and that solves some problems but causes out-of-bounds errors. I feel like the logic could be simpler and it would solve these problems but I'm not sure how. Please hint without answering if you can!

I've tried manually tracing my code but I think I'm too close to it because I get the 'correct' answer not the real output.

I've googled it up and down and everyone on the internet uses two for loops, which works and is shorter, but I'd like to solve it with a single pass.

My code, formatted nicely, here.

public int commonTwo(String[] a, String[] B)/> {
  int indA = 0;
  int indB = 0;
  int count = 0;
  int temp = 0;
  String str = "";
  
  while(indA<a.length && indB<b.length) {
    
    temp = a[indA].compareTo(b[indB]);  // Helps test matches & decide which array position to increment

    if (str.contains(a[indA])) indA++;  //Skips letters previously matched
    if (str.contains(b[indB])) indB++;  //etc.
    
    if ( temp==0 ) {  // If there is a match, increment both arrays, count and add the matched letter to str
      count++;
      str += a[indA];
      indA++;
      indB++;
    }
    if (temp<0) {  // If a is before b, increment array A.
      indA++;
    }
    if (temp>0) {  // If b is before a, increment array B.
      indB++;
    }
  }
  return count;
}



Is This A Good Question/Topic? 0
  • +

Replies To: CodingBat Java > AP-1 > commonTwo with a single While Loop

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1951
  • View blog
  • Posts: 4,052
  • Joined: 11-December 07

Re: CodingBat Java > AP-1 > commonTwo with a single While Loop

Posted 21 June 2013 - 04:45 AM

I just did the problem and made the same mistake as you. The question isn't clear. It gives the impression that it wants you too count duplicates when its tests do not count duplicates. For example, this call should produce 1, not 3.

commonTwo({"a", "a", "a"}, {"a", "a", "a"})


Knowing that, I'm sure you can adjust your code without help. :)

This post has been edited by cfoley: 21 June 2013 - 04:46 AM

Was This Post Helpful? 1
  • +
  • -

#3 redwoolwinterhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-June 13

Re: CodingBat Java > AP-1 > commonTwo with a single While Loop

Posted 21 June 2013 - 05:22 AM

Hi cfoley, thanks for replying. I got this working eventually, I understood the prompt correctly I just thought there was a larger structural change that was needed to eliminate more than two redundant letters.

I've updated my code here. This still seems long to me. I'm going to keep trying to make it shorter, if you've got any ideas let me know. I'll post my progress.

Side questions:
Is there a way to edit old posts? Is it better practice to link out to code samples or paste it in?

View Postcfoley, on 21 June 2013 - 04:45 AM, said:

I just did the problem and made the same mistake as you. The question isn't clear. It gives the impression that it wants you too count duplicates when its tests do not count duplicates. For example, this call should produce 1, not 3.

commonTwo({"a", "a", "a"}, {"a", "a", "a"})


Knowing that, I'm sure you can adjust your code without help. :)/>/>/>

Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1951
  • View blog
  • Posts: 4,052
  • Joined: 11-December 07

Re: CodingBat Java > AP-1 > commonTwo with a single While Loop

Posted 21 June 2013 - 06:39 AM

This was my first successful attempt. It's very similar to yours. Maybe they could both be tweaked a bit but I thing they are both the right idea.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: CodingBat Java > AP-1 > commonTwo with a single While Loop

Posted 21 June 2013 - 11:47 AM

Quote

I've googled it up and down and everyone on the internet uses two for loops, which works and is shorter, but I'd like to solve it with a single pass.

You are kinda cheating...
Guess what this method does to check if it contains a[indA]
str.contains(a[indA])


Was This Post Helpful? 0
  • +
  • -

#6 redwoolwinterhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-June 13

Re: CodingBat Java > AP-1 > commonTwo with a single While Loop

Posted 21 June 2013 - 12:20 PM

CasiOo, do you mean '.contains()' uses a loop? If so, yes it's a loop on str but still a single pass (the thing I was after) over Strings a and b as far as I can tell.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1