Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jbjares;
- import java.math.BigDecimal;
- import java.util.Arrays;
- import junit.framework.Assert;
- import org.apache.commons.lang3.math.NumberUtils;
- import org.junit.Test;
- public class GlassdoorQ3 {
- // Question 2
- /* This class will be given a list of words
- * (such as might be tokenized
- * from a paragraph of text), and will provide a method that takes two
- * words and returns the shortest distance (in words) between those two
- * words in the provided text.
- * Example: *
- * WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox", "quick"));
- * assert(finder.distance("fox","the") == 3);
- * assert(finder.distance("quick", "fox") == 1); */
- public class WordDistanceFinder{
- String args[];
- public WordDistanceFinder(String[] args){
- this.args=args;
- }
- public int distance(String a, String b){
- int indexa = Arrays.asList(args).indexOf(a);
- int indexb = Arrays.asList(args).indexOf(b);
- if(indexa>indexb){
- return (indexa - indexb)-1;
- }else{
- return (indexb - indexa)-1;
- }
- }
- }
- @Test
- public void test() {
- String[] str = new String[]{"the", "quick", "brown", "fox", "quick"};
- org.junit.Assert.assertEquals(1,(new GlassdoorQ3().new WordDistanceFinder(str).distance("quick", "fox")));
- org.junit.Assert.assertEquals(2,(new GlassdoorQ3().new WordDistanceFinder(str).distance("fox","the")));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement