Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. t A:\
  2. \
  3. 1. find odd; find even; return array.\
  4. 2. Linear search \
  5. 3. int[] array = \{oddCount, evenCount\};\
  6. return array;\
  7. 4. No, because the array.length - oddCount = evenCount.\
  8. 5. \
  9. public static int[] evenOdds(int[] values)\
  10. \{\
  11. int oddCount = 0;\
  12. int evenCount = 0;\
  13. for(int e : values) \{\
  14. if(e % 2 == 0) \{\
  15. evenCount++;\
  16. \} else \{\
  17. oddCount++;\
  18. \}\
  19. \
  20. \}\
  21. int[] array = \{evenCount, oddCount\};\
  22. return array;\
  23. \
  24. \}\
  25. \
  26. Part B:\
  27. \
  28. 1. \expnd0\expndtw0\kerning0
  29. How will you find duplicates? By create 2 variables to record current word and previous word.\
  30. What will you do when you find them? If current word and previous word are same, remove current word.\
  31. 2. Yes. \
  32. 3. Yes.\
  33. 4. \
  34. public void removeAdjacentDuplicates()\
  35. \{\
  36. String prev = words.get(0);\
  37. for(int i = 1; i < words.size(); i++) \{\
  38. String curr = words.get(i);\
  39. if(prev.equals(curr)) \{\
  40. words.remove(i);\
  41. i--;\
  42. \} else \{\
  43. prev = curr;\
  44. \}\
  45. \}\
  46. \
  47. \}\
  48. 5. Done.\
  49. \kerning1\expnd0\expndtw0 6. \
  50. public void removeAllDuplicates() \{\
  51. ArrayList<String> nodup = new ArrayList<String>();\
  52. for(String e : words) \{\
  53. if(! nodup.contains(e)) \{\
  54. nodup.add(e);\
  55. \}\
  56. \}\
  57. words = nodup;\
  58. \
  59. \}\
  60. \
  61. 7. Yes.\
  62. 8. \
  63.  
  64. \b This is my original code for the first one
  65. \b0 \
  66. public void removeAdjacentDuplicates()\
  67. \{\
  68. String prev = words.get(0);\
  69. for(int i = 1; i < words.size(); i++) \{\
  70. String curr = words.get(i);\
  71. if(prev.equals(curr)) \{\
  72. words.remove(i);\
  73. i--;\
  74. \} else \{\
  75. prev = curr;\
  76. \}\
  77. \}\
  78. \
  79. \}\
  80. \
  81.  
  82. \b This is the changes that I made to remove all the duplicates
  83. \b0 \
  84. public void removeAllDuplicates() \{\
  85. ArrayList<String> nodup = new ArrayList<String>();\
  86. for(String e : words) \{\
  87. if(! nodup.contains(e)) \{\
  88. nodup.add(e);\
  89. \}\
  90. \}\
  91. words = nodup;\
  92. \
  93. \}\
  94. \
  95. Part C:\
  96. \
  97. 1. Done.\
  98. 2 . Creates a temporary variable to record the first values.\
  99. 3. \
  100. for (int i = 0; i < n; i = i + 2)\
  101. \{\
  102. int temp = number[i];\
  103. numbers[i] = numbers[i + 1];\
  104. numbers[i + 1] = numbers[i];\
  105. \}\
  106. \
  107. Part D:\
  108. \
  109. 1. Done.\
  110. 2. Done.\
  111. 3. \
  112. for(int j = 0; j < list.size()/2; j++) \{\
  113. Picture temp = list.get(0);\
  114. for(int i = 0; i < list.size() - 1; i++) \{ \
  115. list.set(i, list.get(i+1));\
  116. \}\
  117. list.set(list.size()-1, temp);\
  118. \}\
  119. \
  120. 4. Done.\
  121. 5. \
  122. int mid = list.size()/2;\
  123. for(int i = 0; i < mid; i++) \{\
  124. Picture temp = list.get(i + mid);\
  125. list.set(i + mid, list.get(i));\
  126. list.set(i, temp);\
  127. \}\
  128. \
  129. \
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement