Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Recitation created by Gareth Halladay, 08/17. <br>
  5. * Content was gathered from two sources:
  6. * <ul>
  7. * <li> http://www.cs.wustl.edu/~kjg/cse131/modules/recursion/lab.html
  8. * <li> http://codingbat.com/prob/p273235?parent=/home/bono@usc.edu/recursionLab
  9. * </ul>
  10. *
  11. * Complete the 9 of the 10 methods.
  12. */
  13. public class Recursion {
  14.  
  15. /**
  16. * The product of an integer and all the integers below it.
  17. * @param n a natural number
  18. * @return n * (n-1) * (n-2) * ... * 1
  19. */
  20. public static int factorial(int n){
  21. if (n <= 1) {
  22. return 1;
  23. }
  24.  
  25. return n* factorial(n - 1);
  26. }
  27.  
  28. /**
  29. * Every number after the first two is the sum of the two preceding ones. <br>
  30. * index: 0, 1, 2, 3, 4, 5, 6... <br>
  31. * output: 0, 1, 1, 2, 3, 5, 8...
  32. * @param n which fibonacci number to compute.
  33. * @return the fibonacci number.
  34. */
  35. public static int fib(int n){
  36. if (n == 0) {
  37. return 0;
  38. }
  39. else if (n == 1) {
  40. return 1;
  41. }
  42. else {
  43. return fib(n - 1) + fib(n - 2);
  44. }
  45. }
  46.  
  47. /**
  48. * Recursively sum the positive integers from n down to 0 decrementing by 2. <br>
  49. * sumDownBy2(7) is 7+5+3+1 = 16 <br>
  50. * sumDownBy2(8) is 8+6+4+2 = 20 <br>
  51. *
  52. * @param n a whole number.
  53. * @return the sum of the positive integers n + (n-2) + (n-4) + ...
  54. */
  55. public static int sumDownBy2(int n){
  56. if (n <= 0) {
  57. return 0;
  58. }
  59. else {
  60. int a = n;
  61. n = n - 2;
  62. return a + sumDownBy2(n);
  63. }
  64. }
  65.  
  66. /**
  67. * Recursively return the sum of the harmonic series.
  68. * @param n a positive number.
  69. * @return the sum 1 + 1/2 + 1/3 + ... + 1/(n-1) + 1/n
  70. */
  71. public static double harmonicSum(int n){
  72. if (n == 0) {
  73. return 0;
  74. }
  75. else {
  76. double a = (1.0 / n);
  77. return a + harmonicSum(n - 1);
  78. }
  79. }
  80.  
  81.  
  82. /**
  83. * Recursively return the sum of the geometric series.
  84. * @param n a non-negative number.
  85. * @return the sum 1 + 1/2 + 1/4 + 1/8 + ... + 1/Math.pow(2,n)
  86. */
  87. public static double geometricSum(int n){
  88. if (n == 0) {
  89. return 1;
  90. }
  91. else {
  92. double a = (1.0 / Math.pow(2, n));
  93. return a + geometricSum(n - 1);
  94. }
  95. }
  96.  
  97.  
  98. /**
  99. * Write a multiplication method recursively using repeated addition. <br>
  100. * Do not use the multiplication operator or a loop.
  101. *
  102. * @param j a positive or negative integer.
  103. * @param k a positive or negative integer.
  104. * @return the product of the k and j.
  105. */
  106. public static int mult(int j, int k){
  107. if (k == 0) {
  108. return 0;
  109. }
  110. else {
  111. if(k > 0) {
  112. return j + mult(j, k - 1);
  113. }
  114. else {
  115. return j + mult(j, k + 1);
  116. }
  117.  
  118. }
  119. }
  120.  
  121. /**
  122. * Write a method that computes j^k. <br>
  123. * Do not use Math.pow() or a loop. <br>
  124. * @param j a non-negative number
  125. * @param k a non-negative number
  126. * @return j^k
  127. */
  128. public static int expt(int j, int k){
  129. return 0;
  130. }
  131.  
  132.  
  133. /**
  134. * Check to see if a word is a palindrome. Should be case-independent.
  135. * @param word a String without whitespace
  136. * @return true if the word is a palindrome
  137. */
  138. public static boolean isPalindrome(String word){
  139. return false;
  140. }
  141.  
  142. /**
  143. * Returns length of the longest word in the given String using recursion (no loops).
  144. * Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space,
  145. * use the following method on your String to remove punctuation {@code .replaceAll("[^a-zA-Z]", "")}
  146. * If you use a Scanner, you will need a helper method to do the recursion on the Scanner object.
  147. *
  148. * @param words A String containing one or more words.
  149. * @return The length of the longest word in the String.
  150. * @see Scanner#Scanner(String)
  151. * @see Scanner#next()
  152. * @see String#split(String)
  153. * @see String#replaceAll(String, String)
  154. * @see Math#max(int, int)
  155. */
  156. public static int longestWordLength(String words){
  157. Scanner scan = new Scanner(words.replaceAll("[^a-zA-Z\\s]", ""));
  158.  
  159.  
  160. return helper(scan, 0);
  161. }
  162.  
  163. private static int helper(Scanner scan, int length) {
  164. return 0;
  165.  
  166. }
  167.  
  168.  
  169.  
  170. /**
  171. * Remove consecutive duplicate characters from a String. <br>
  172. * Case should not matter, if two or more consecutive duplicate <br>
  173. * characters have different cases, then the first letter should be kept.
  174. * @param word A word with possible consecutive duplicate characters.
  175. * @return A word without any consecutive duplicate characters.
  176. */
  177. public static String dedupeChars(String word){
  178. return null;
  179. }
  180.  
  181.  
  182. public static void main(String [] args){
  183. // Test your methods here!
  184. // Uncomment each block as you are ready to test it.
  185.  
  186.  
  187. System.out.println("Testing the factorial method");
  188. System.out.printf("factorial(4) should be 24 -> %d\n", factorial(4));
  189. System.out.printf("factorial(6) should be 720 -> %d\n", factorial(6));
  190. System.out.printf("factorial(0) should be 1 -> %d\n", factorial(0));
  191. System.out.println();
  192.  
  193. System.out.println("Testing the fibonacci method");
  194. System.out.printf("fib(0) should be 0 -> %d\n", fib(0));
  195. System.out.printf("fib(1) should be 1 -> %d\n", fib(1));
  196. System.out.printf("fib(2) should be 1 -> %d\n", fib(2));
  197. System.out.printf("fib(5) should be 5 -> %d\n", fib(5));
  198. System.out.printf("fib(10) should be 55 -> %d\n", fib(10));
  199. System.out.printf("fib(13) should be 233 -> %d\n", fib(13));
  200. System.out.println();
  201.  
  202. System.out.println("Testing out the sumDownBy2 method");
  203. System.out.printf("sumDownBy2(-1) should be 0 -> %d\n", sumDownBy2(-1));
  204. System.out.printf("sumDownBy2(0) should be 0 -> %d\n", sumDownBy2(0));
  205. System.out.printf("sumDownBy2(7) should be 16 -> %d\n", sumDownBy2(7));
  206. System.out.printf("sumDownBy2(8) should be 20 -> %d\n", sumDownBy2(8));
  207. System.out.printf("sumDownBy2(13) should be 49 -> %d\n", sumDownBy2(13));
  208.  
  209. System.out.println();
  210.  
  211. System.out.println("Testing out the harmonicSum method");
  212. System.out.printf("harmonicSum(0) should be 0.0000 -> %.4f\n", harmonicSum(0));
  213. System.out.printf("harmonicSum(7) should be 2.5929 -> %.4f\n", harmonicSum(7));
  214. System.out.printf("harmonicSum(8) should be 2.7179 -> %.4f\n", harmonicSum(8));
  215. System.out.printf("harmonicSum(13) should be 3.1801 -> %.4f\n", harmonicSum(13));
  216. System.out.printf("harmonicSum(24) should be 3.7760 -> %.4f\n", harmonicSum(24));
  217. System.out.println();
  218.  
  219. System.out.println("Testing out the geometricSum method");
  220. System.out.printf("geometricSum(0) should be 1.0000 -> %.4f\n", geometricSum(0));
  221. System.out.printf("geometricSum(1) should be 1.5000 -> %.4f\n", geometricSum(1));
  222. System.out.printf("geometricSum(2) should be 1.7500 -> %.4f\n", geometricSum(2));
  223. System.out.printf("geometricSum(7) should be 1.9922 -> %.4f\n", geometricSum(7));
  224. System.out.printf("geometricSum(24) should be 2.0000 -> %.4f\n", geometricSum(24));
  225. System.out.println();
  226.  
  227. System.out.println("Testing out the multiplication method");
  228. System.out.printf("mult(8, 2) should be 16 -> %d\n", mult(8, 2));
  229. System.out.printf("mult(2, 8) should be 16 -> %d\n", mult(2, 8));
  230. System.out.printf("mult(4, -3) should be -12 -> %d\n", mult(4, -3));
  231. System.out.printf("mult(-3, 4) should be -12 -> %d\n", mult(-3, 4));
  232. System.out.println();
  233.  
  234. /*
  235. System.out.println("Testing out the exponent method");
  236. System.out.printf("expt(2, 5) should be 32 -> %d\n", expt(2, 5));
  237. System.out.printf("expt(5, 2) should be 25 -> %d\n", expt(5, 2));
  238. System.out.println();
  239.  
  240. System.out.println("Testing out the palindrome method");
  241. System.out.printf("isPalindrome(\"a\") should be true -> %b\n", isPalindrome("a"));
  242. System.out.printf("isPalindrome(\"Aibohphobia\") should be true -> %b\n", isPalindrome("Aibohphobia"));
  243. System.out.printf("isPalindrome(\"noon\") should be true -> %b\n", isPalindrome("noon"));
  244. System.out.printf("isPalindrome(\"Recursion\") should be false -> %b\n", isPalindrome("Recursion"));
  245. System.out.println();
  246.  
  247. System.out.println("Testing out the longestWordLength method\n");
  248. String quoteOne =
  249. "Grit, one of the keys to success. The person who perseveres is the one who\n" +
  250. "will surely win. Success does not come from giving up, it comes from believing\n" +
  251. "in yourself and continuously working towards the realization of a worthy ideal.\n" +
  252. "Do not ever give up on what you want most. You know what you truly want.\n" +
  253. "Believe in your dreams and goals and take daily consistent action in order to\n" +
  254. "make your dreams a reality.";
  255. System.out.printf("The longest word in the following quote:\n%s\nshould be 12 -> %d\n\n", quoteOne, longestWordLength(quoteOne));
  256. String quoteTwo = "Try to be like the turtle – at ease in your own shell.";
  257. System.out.printf("The longest word in the following quote:\n%s\nshould be 6 -> %d\n\n", quoteTwo, longestWordLength(quoteTwo));
  258. System.out.println();
  259.  
  260. System.out.println("Testing the dedupeChars method");
  261. System.out.printf("dedupeChars(\"a\") should be a -> %s\n", dedupeChars("a"));
  262. System.out.printf("dedupeChars(\"aa\") should be a -> %s\n", dedupeChars("aa"));
  263. System.out.printf("dedupeChars(\"MiSsisSiPpi\") should be MiSisiPi -> %s\n", dedupeChars("MiSsisSiPpi"));
  264. System.out.printf("dedupeChars(\"swimMmMming\") should be swiming -> %s\n", dedupeChars("swimMmMming"));
  265. */
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement