willieshi232

Untitled

Apr 3rd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class Exercise
  4. {
  5. public static void main(String [] args)
  6. {
  7. Scanner console= new Scanner(System.in);
  8. System.out.println("Input the E value you wish to use");
  9. int e = console.nextInt();
  10. LinkedList<Integer> list = new LinkedList<>();
  11. partition(list, e);
  12. countUnique(list);
  13. }
  14. //ex 4
  15. public static void partition(List<Integer>list, int value) {
  16. List<Integer> temp = new LinkedList<Integer>();
  17. Iterator<Integer> itr = list.iterator();
  18. while (itr.hasNext()) {
  19. int element = itr.next();
  20. if (element < value) {
  21. temp.add(0, element);
  22. } else {
  23. temp.add(element);
  24. }
  25. }
  26.  
  27. }
  28. //ex 6
  29. public static int countUnique(LinkedList<Integer> list)
  30. {
  31. Set<Integer> set = new HashSet<Integer>();
  32. Iterator<Integer> itr = list.iterator();
  33. while (itr.hasNext()) {
  34. int temp = itr.next();
  35.  
  36. if (set.contains(temp)) {
  37. break;
  38. } else
  39. {
  40. set.add(temp);
  41. }
  42. }
  43. return set.size();
  44. }
  45. //ex 8
  46. public static String maxLength(String[] array) {
  47. int maxLength = 0;
  48. String longestString = null;
  49. if(array.length == 0)
  50. {
  51. return null;
  52. }
  53. for (String s : array) {
  54. if (s.length() > maxLength) {
  55. maxLength = s.length();
  56. longestString = s;
  57. }
  58. }
  59. return longestString;
  60. }
  61.  
  62. //ex 10
  63. public static void removeEvenLengths(String[] array)
  64. {
  65. for (String s : array) {
  66. if (s.length() % 2 == 0)
  67. {
  68. int x = array.get(s);
  69. array.remove(x);
  70. }
  71. }
  72. }
  73.  
  74. public static boolean contains3(String[] array)
  75. {
  76. Map<String, Integer> WordMap = new TreeMap<>();
  77. for(String word : array)
  78. {
  79. if(WordMap.containsKey(word))
  80. {
  81. int count = WordMap.get(word);
  82. WordMap.put(word, count + 1);
  83. }
  84. else
  85. {
  86. WordMap.put(word, 1);
  87. }
  88. }
  89. for(String word: WordMap.keySet())
  90. {
  91. int count = WordMap.get(word);
  92. if(count == 3)
  93. {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. //ex 14
  100. public static Map<String, Integer> intersect(TreeMap<String, Integer> map1, TreeMap<String, Integer> map2)
  101. {
  102. Map<String, Integer>FinalMap = new TreeMap<>();
  103. for(String s1 : map1.keySet())
  104. {
  105. for(String s2 : map2.keySet())
  106. {
  107. if(s1.equals(s2))
  108. {
  109. if(map1.get(s1) == map2.get(s2))
  110. {
  111. FinalMap.put(s1, map1.get(s1));
  112. }
  113. }
  114. }
  115. }
  116. return FinalMap;
  117. }
  118. //ex 16
  119. public static boolean is1total(TreeMap<String, Integer> map1)
  120. {
  121. Map<String, Integer> temp = map1;
  122. for(String s1 : temp.keySet())
  123. {
  124. for(String s2 : map1.keySet())
  125. {
  126. if(!s1.equals(s2) && temp.get(s1) == map1.get(s2))
  127. {
  128. return false;
  129. }
  130.  
  131.  
  132.  
  133. }
  134. }
  135. return true;
  136. }
  137. //ex 18
  138. public static Map<String, String> reverse(TreeMap<String, String> map1)
  139. {
  140. Map<String, String> Final = new TreeMap<>();
  141. for(String s : map1.keySet())
  142. {
  143. String s1 = map1.get(s);
  144. Final.put(s1, s);
  145. }
  146. return Final;
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment