File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Loop Condition Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop Condition Question" Watch "Loop Condition Question" New topic
Author

Loop Condition Question

Marvin Porte
Greenhorn

Joined: Nov 11, 2012
Posts: 11
Problem:
Start with two arrays of strings, a and b, each in alphabetical order, possibly with duplicates. Return the count of the number of strings which appear in both arrays. The best "linear" solution makes a single pass over both arrays, taking advantage of the fact that they are in alphabetical order.

commonTwo({"a", "c", "x"}, {"b", "c", "d", "x"}) → 2
commonTwo({"a", "c", "x"}, {"a", "b", "c", "x", "z"}) → 3
commonTwo({"a", "b", "c"}, {"a", "b", "c"}) → 3

My Solution:

The code is running fine and correct for all the tests but I'm in doubt at some points.

My Question:
i really can't determine the looping condition on how many times I'm going to loop here since there's no specific given and you can't determine somehow (I think..).
so what i did was get the max length (a.length*b.length) then use it as looping condition (but i know that it will not reach to that point. i just make it to make sure that it will loop all the conditions).
then i just created a break point on the loop where counters reach their max value (array length).
is it legal? i mean is it an acceptable code as a programmer? it works fine but i don't know if it's acceptable or a good practice. and can you give me any idea to solve this one if you have an idea for looping condition.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Don’t know. Don’t know. Don’t know.

You cannot work out an algorithm on screen. You need to work it out on paper. Write down on paper how you would go through those two arrays once each and check whether there are common elements. Remember the question says the two arrays are pre‑sorted in alphabetical order.
Is this another codingbat question? If so please supply link.
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
Marvin Porte wrote:Problem:
My Question:
i really can't determine the looping condition on how many times I'm going to loop here since there's no specific given and you can't determine somehow (I think..).
so what i did was get the max length (a.length*b.length) then use it as looping condition (but i know that it will not reach to that point. i just make it to make sure that it will loop all the conditions).
then i just created a break point on the loop where counters reach their max value (array length).
is it legal? i mean is it an acceptable code as a programmer? it works fine but i don't know if it's acceptable or a good practice. and can you give me any idea to solve this one if you have an idea for looping condition.


A while loop with no break; statement makes a lot more sense for this for code clarity.



if you wanted to leave the break statement (perhaps there were two separate exit points from the loop):



In your version the for loop is not needed, it's not an exit point, the value of i is not used for anything and its just confusing.

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4699
    
    7

Marvin Porte wrote:
is it legal? i mean is it an acceptable code as a programmer? it works fine but i don't know if it's acceptable or a good practice. and can you give me any idea to solve this one if you have an idea for looping condition.


Legal / acceptable? For a beginner, it's OK. It's not perfect though.

You already have a pretty good loop condition in that first if-statement. Use a variation of it in a while-loop instead.

This line of code is not good


Never do that unless you are checking for reference equality (do temp and a[counterA] refer to the same instance?) -- in this case you want to check the values, not the reference. Use equals() instead.

counterA and counterB are not good names -- they are not counters, they are indices. Make your variable names reflect their purpose and usage. If they don't, your code just gets confusing to read.


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4699
    
    7

Also, run through your loop manually, with pencil and paper and see if temp is really useful. I don't see how it is.
Marvin Porte
Greenhorn

Joined: Nov 11, 2012
Posts: 11
@Campbell
Here's the link of the problem. http://codingbat.com/prob/p100369
Yeah I check the algorithm on paper and it works fine but as Sir steve said, I used "useless loop condition with i and it's confusing" and I agree with him. It's just like a while(true) with breaking point inside, relying only on that breaking point to stop the loop without using the loop condition.

@Steve
Thanks sir! Thank you very much I really get your point. Thanks a lot.

@Junilu
Alright sir thank you for pointing out my mistakes.
By the way, I used String temp so that I could temporarily store the last value. Because when I created the code without temp, the program still counts another common instance say for example Array1 is {a,a,b,b,d} and Array2 is {a,a,b,b,c}, the program still counts another instance of a and b which will output 4, but the correct answer is only 2. I used temp to disregard the common instance if the letter is already counted.

Thanks everyone for all your help!
Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 45
One solution can be as follows:
Convert both the arrays to sets one by one.
Then iterate over the smaller set and check if the element in the smaller set is contained in the larger one. Whenever a match is found, the counter can be incremented by 1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop Condition Question