Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. package eecs2030.lab6;
  2.  
  3. import java.util.List;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7. * A utility class containing several recursive methods (Lab 6, F2017)
  8. *
  9. * <pre>
  10. *
  11. * For all methods in this API, you are forbidden to use any loops, nor
  12. * String or List based methods such as "contains", or methods that use regular expressions
  13. * </pre>
  14. *
  15. * @author
  16. *
  17. */
  18. public final class RecursiveTasks {
  19.  
  20. private RecursiveTasks() {}
  21.  
  22.  
  23. /**
  24. * getParenthesis returns the component within a given string that is
  25. * enclosed in parenthesis
  26. *
  27. * <br>
  28. * The method assumes there is only a single pair of parenthesis in the
  29. * string, and computes the new string recursively, such that the new string
  30. * is only made of the parenthesis and their contents.
  31. *
  32. * E.g.
  33. * <pre>
  34. * <code>getParenthesis("abcd(xyz)qrst")</code> will return "(xyz)"
  35. * </pre>
  36. *
  37. * @param str the input string (with one pair of parenthesis embedded within)
  38. * @return a string made only of the parenthesis and their contents (from str)
  39. */
  40. public static String getParenthesis(String str) {
  41. //System.out.println(str);
  42. if(str.charAt(0) == '(' && str.charAt(str.length() - 1) == ')') {
  43. return str;
  44. }
  45. if(str.charAt(0) != '(') {
  46. return getParenthesis(str.substring(1, str.length()));
  47. }
  48. return getParenthesis(str.substring(0, str.length() - 1));
  49.  
  50.  
  51.  
  52. }
  53.  
  54. // public static void main(String[] args) {
  55. // RecursiveTasks t = new RecursiveTasks();
  56. // System.out.println(t.getParenthesis("abcdxyzqrst"));
  57. // }
  58.  
  59.  
  60. /**
  61. * Count the number of co-occurrances of a non-empty sub-string
  62. * <code>sub</code> within the string <code>str</code>
  63. *
  64. * E.g.
  65. * <pre>
  66. * <code>numOccurances("dogmonkeydog","dog")</code> will return 2
  67. * <code>numOccurances("dogmonkeydog","mon")</code> will return 1
  68. * <code>numOccurances("dogmonkeydog","cow")</code> will return 0
  69. * </pre>
  70. *
  71. * @param str the given string
  72. * @param sub a non-empty sub-string
  73. * @return the number of times sub occurs in str, without overlap
  74. *
  75. */
  76. public static int numOccurrences(String str, String sub) {
  77. if(str.length() < sub.length()) {
  78. return 0;
  79. }
  80. if(sub.equals(str)) {
  81. return 1;
  82. }
  83. if(sub.equals(str.substring(0, sub.length()))) {
  84. return 1 + numOccurrences(str.substring(3, str.length()), sub);
  85. }
  86. return numOccurrences(str.substring(1, str.length()), sub);
  87. }
  88.  
  89.  
  90. /**
  91. * Given a string, return true if it is a nesting of zero or more pairs of
  92. * balanced parenthesis, like "(())" or "((()))".
  93. *
  94. * If the parenthesis are unbalanced, or the string includes characters
  95. * other than parenthesis, return false
  96. *
  97. * E.g.
  98. * <pre>
  99. * <code>parenthIsNested("(())")</code> will return true
  100. * <code>parenthIsNested("((()))")</code> will return true
  101. * <code>parenthIsNested("(((x))")</code> will return false
  102. * </pre>
  103. *
  104. * Hint: check the first and last chars, and then recur on what's inside
  105. * them.
  106. *
  107. * @param str
  108. * - the string (includes zero or more pairs of parenthesis)
  109. * @return a boolean value indicating true if the string contains nested and balanced parenthesis, false otherwise
  110. */
  111. public static boolean parenthIsNested(String str) {
  112.  
  113. if(str.equals("") || str.equals("()")) {
  114. return true;
  115. }
  116. if(str.charAt(0) == '(' && str.charAt(str.length() - 1) == ')') {
  117. return parenthIsNested(str.substring(1, str.length() - 1));
  118. }
  119. return false;
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /**
  129. * Generates a list of Circles to be used in drawing a fractal pattern
  130. *
  131. * <pre>
  132. * This method accepts a List of Circles (see Helper classes Circle.java and
  133. * FractalPatterns.java To see how the list of Circles is generated, and how
  134. * this method is envoked). You do not to perform any drawing operations in
  135. * this task (they are done for you).
  136. *
  137. * The method computes the position of, and creates 4 new circles at each
  138. * recursion step. Each circle is positioned at the vertical (top and
  139. * bottom) and horizontal (left and right) intercepts with a imaginary
  140. * circle (centred on x,y and with radius = <code>radius</code>). The radius
  141. * of each circle created, is radius/2.
  142. *
  143. * The centre position of each newly generated circle will form the centre
  144. * of a new set of 4 circles in the next recursive step, each with a reduced
  145. * radius of (radius/4), and drawn on the vertical and horizontal intercepts
  146. * of its parent circle, and so on (see diagram in lab description).
  147. *
  148. * </pre>
  149. *
  150. *
  151. * @param circles a list in which to store circles as they are generated
  152. * @param x the x coord of the centre of the current set of circles
  153. * @param y the y coord of the centre of the current set of circles
  154. * @param radius the radius of the parent circle
  155. * @param n number of recursive steps to generate circles for
  156. * @param mode a boolean array [left right up down] indicating which of the 4 circles will be recursed on
  157. */
  158. public static void genFractal(List<Circle> circles, int x, int y, int radius, int n, boolean [] mode) {
  159.  
  160. if(n == 0) {
  161. return;
  162. }
  163. circles.add(new Circle(x, y + radius, radius / 2));
  164. circles.add(new Circle(x + radius, y, radius / 2));
  165. circles.add(new Circle(x, y - radius, radius / 2));
  166. circles.add(new Circle(x - radius, y, radius / 2));
  167.  
  168. if(mode[0]) {
  169. genFractal(circles, x - radius, y, radius / 2, n - 1, mode);
  170. }
  171. if(mode[1]) {
  172. genFractal(circles, x + radius, y, radius / 2, n - 1, mode);
  173. }
  174. if(mode[2]) {
  175. genFractal(circles, x, y + radius, radius / 2, n - 1, mode);
  176. }
  177. if(mode[3]) {
  178. genFractal(circles, x, y - radius, radius / 2, n - 1, mode);
  179. }
  180.  
  181. }
  182.  
  183.  
  184. /**
  185. * Calculate and determine if there exists a sum of (some) of the elements in an integer
  186. * array that is equal to a target value. If there exists a sum, then return true, otherwise false.
  187. *
  188. * Given an array of ints, is it possible to choose a group of some of the ints, such that the
  189. * group sums to the given target?
  190. *
  191. * Assume this method is always called with a starting index of 0 and a sum of 0
  192. * Recursion traverses the array and explores alternative sums
  193. *
  194. * <pre>
  195. * e.g.
  196. * sumSome(0, [2, 4, 8], 10) will return true
  197. * sumSome(0, [2, 4, 8], 14) will return true
  198. * sumSome(0, [2, 4, 8], 9) will return false
  199. * </pre>
  200. *
  201. * @param index the current position in nums
  202. * @param nums an array of integers to be considered in the sum
  203. * @param sum the current sum
  204. * @param target the value some of the integers in nums should add up to
  205. * @return a boolean value indicating that a sum of some of the integers in nums equals the target
  206. */
  207. public static boolean sumSome(int index, int[] nums, int sum, int target) {
  208. //System.out.println(index + " " + sum + " " + target);
  209. if(index == nums.length) {
  210. //System.out.print("+");
  211. return sum == target;
  212. } else {
  213. //System.out.print("-");
  214. return sumSome(index + 1, nums, sum + nums[index], target) || sumSome(index + 1, nums, sum, target);
  215. }
  216.  
  217. }
  218. public static void main(String[] args) {
  219. // int[] nums = new int[] {2, 4, 8};
  220. // System.out.println(RecursiveTasks.sumSome(0, nums, 0, 1));
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement