Advertisement
jbjares

Untitled

Aug 15th, 2015
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. package com.jbjares;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.apache.commons.lang3.math.NumberUtils;
  10. import org.junit.Test;
  11.  
  12. import com.jbjares.GlassdoorQ3.WordDistanceFinder;
  13.  
  14. public class GlassdoorQ4 {
  15.  
  16. // Question 3 public interface TwoSum {
  17. /** * Stores @param input in an internal data structure. */
  18. //void store(int input);
  19. /** * Returns true if there is any pair of numbers in the internal data structure which
  20. * have sum @param val, and false otherwise.
  21. * For example, if the numbers 1, -2, 3, and 6 had been stored,
  22. * the method should return true for 4, -1, and 9, but false for 10, 5, and 0 */
  23. //boolean test(int val); }
  24.  
  25. @Test
  26. public void test() {
  27. TwoSumImpl twoSumImpl = new GlassdoorQ4().new TwoSumImpl();
  28. Map map = new HashMap();
  29. twoSumImpl.store(1);
  30. twoSumImpl.store(-2);
  31. twoSumImpl.store(3);
  32. twoSumImpl.store(6);
  33. org.junit.Assert.assertEquals(Boolean.TRUE,twoSumImpl.test(3));
  34. }
  35.  
  36. public class TwoSumImpl implements TwoSum{
  37.  
  38. private Map<Integer,Integer> input = new HashMap<Integer,Integer>();
  39.  
  40. @Override
  41. public void store(int input) {
  42. if(this.input.isEmpty()){
  43. this.input.put(0,input);
  44. }else{
  45. this.input.put(this.input.size()+1,input);
  46. }
  47. }
  48.  
  49. @Override
  50. public boolean test(int val) {
  51. for (Map.Entry<Integer,Integer> entry : input.entrySet()) {
  52. if(entry.getValue()==val){
  53. continue;
  54. }
  55. if(entry.getValue()+val>0){
  56. return Boolean.TRUE;
  57. }
  58. }
  59.  
  60.  
  61. return Boolean.FALSE;
  62. }
  63.  
  64.  
  65.  
  66.  
  67. }
  68. public interface TwoSum{
  69. void store(int input);
  70. boolean test(int val);
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement