Advertisement
jbjares

Untitled

Aug 15th, 2015
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. package com.jbjares;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.Arrays;
  5.  
  6. import junit.framework.Assert;
  7.  
  8. import org.apache.commons.lang3.math.NumberUtils;
  9. import org.junit.Test;
  10.  
  11. public class GlassdoorQ3 {
  12.  
  13. // Question 2
  14. /* This class will be given a list of words
  15. * (such as might be tokenized
  16. * from a paragraph of text), and will provide a method that takes two
  17. * words and returns the shortest distance (in words) between those two
  18. * words in the provided text.
  19. * Example: *
  20. * WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox", "quick"));
  21. * assert(finder.distance("fox","the") == 3);
  22. * assert(finder.distance("quick", "fox") == 1); */
  23.  
  24. public class WordDistanceFinder{
  25.  
  26. String args[];
  27.  
  28. public WordDistanceFinder(String[] args){
  29. this.args=args;
  30. }
  31. public int distance(String a, String b){
  32. int indexa = Arrays.asList(args).indexOf(a);
  33. int indexb = Arrays.asList(args).indexOf(b);
  34. if(indexa>indexb){
  35. return (indexa - indexb)-1;
  36. }else{
  37. return (indexb - indexa)-1;
  38. }
  39. }
  40. }
  41.  
  42.  
  43. @Test
  44. public void test() {
  45. String[] str = new String[]{"the", "quick", "brown", "fox", "quick"};
  46. org.junit.Assert.assertEquals(1,(new GlassdoorQ3().new WordDistanceFinder(str).distance("quick", "fox")));
  47. org.junit.Assert.assertEquals(2,(new GlassdoorQ3().new WordDistanceFinder(str).distance("fox","the")));
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement