How to Write Code to Find the Minimal Distance Between Words

If you need to develop the code which finds the minimal distance between words in a file, you are at the right place. From the sample that you find below, you’ll understand how to cope with this task. The instructions were assembled by an expert from AssignmentShark who has vast knowledge in the topic. Try to apply guidance from the sample to deal with your assignment.

AssignmentShark is a service which provides students from all over the world with assistance. We hire experts from various fields so that they can complete examples for you. A number of free examples you can find on our blog. However, if you need to get a specific sample, we can also provide you with it. The only thing you need to do is fill out the order form. If you need to know more about the code for distance between words in a file, you can apply to us right away. Our support team is ready to help you any time!

The Minimal Distance Between Two Words in a File

We have a large text file that contains words. You must write code that will allow finding the minimum distance between two words.

Let’s assume that the order of the words firstWord and secondWord is not important. If the word order is important, we will need to modify the following code.

To solve this problem, it will be enough to read the file only once. We will save the information about where we found the latest firstWord or secondWord in firstWordPosition and secondWordPosition and, if necessary, update the min value, and then update the firstWordPosition. We will work with secondWordPosition similarly. At the end of the algorithm at our disposal will be the correct value of min (minimum distance).

The following code implements this algorithm:


public int minimum(String[] words, String firstWord, String secondWord) {

int min = Integer.MAX_VALUE;

int firstWordPosition = -1;

int secondWordPosition  = -1;

for (int i = 0; i < words.lenght; i++) {

String curWord = words[i];

if (curWord.equals(firstWord)) {

 firstWordPosition = i;

int length = firstWordPosition - secondWordPosition ;

if (secondWordPosition  >= 0 && min > length) {

min = length;

}

 } else if (curWord.equals(secondWord)) {

secondWordPosition  = i;

int length = secondWordPosition  - firstWordPosition;

if (lastPosWord >= 0 && min > length) {

min = length;

}

}

}

return min;

}


 

If we have to perform the same work for other pairs of words, we can create a hash table, linking words with the position in the file. Then the solution is the minimum (arithmetic) difference between the value of the lists firstList and secondList.

Let’s look at the lists:

firstList {1, 2, 3, 13, 21}

secondList {5, 9, 18}

We also can combine the lists into one sorted list, but to link each value with the original list. This operation is performed by “wrapping” each value in the class, which will have two variables: data (to store the actual value) and listIndex.

list: {1first, 2first, 3first, 5second, 9second, 13first, 18second, 21first}

The calculation of the minimum distance turns into a search for the minimum distance between two consecutive numbers that have different list tags.

Thanks for your attention!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.