From 48260cbfff11ca9dd44a3f1304411f24af729eed Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Thu, 10 Oct 2019 00:30:42 -0500 Subject: [PATCH] Finished all the problems. The only one I looked up was the last one --- .gitignore | 2 ++ RecursiveMethods.java | 71 +++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..061d337 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.class +/meghanadaFormatter.xml diff --git a/RecursiveMethods.java b/RecursiveMethods.java index e79c0a5..4a3c633 100644 --- a/RecursiveMethods.java +++ b/RecursiveMethods.java @@ -1,46 +1,59 @@ // The RecursiveMethods class that implements several recursive solutions // Your name here -public class RecursiveMethods -{ - // This method calls the sumSquareRec method and returns the sum of the +public class RecursiveMethods { + // This method calls the sumSquareRec method and returns the sum of the // squares of the elements in the array. - public int sumSquares(int[] A) - { + public int sumSquares(int[] A) { // Do not make any changes to this method! return sumSquaresRec(A, 0); - } + } - // This method takes an integer array as well as an integer (the starting + // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. - public int sumSquaresRec(int[] A, int pos) - { - // TODO: implement this method - - return -1; // replace this statement with your own return - } + public int sumSquaresRec(int[] A, int pos) { + + int current_int = A[pos]; + if (pos + 1 == A.length) + return current_int * current_int; + return current_int * current_int + sumSquaresRec(A, pos + 1); + } // This method takes a character stack and converts all lower case letters // to upper case ones. - public void upperStackRec(CharStack s) - { - // TODO: implement this method + public void upperStackRec(CharStack s) { + if (s.isEmpty()) + return; + else { + char top = Character.toUpperCase(s.pop()); + upperStackRec(s); + s.push(top); + } } - + // This method reads a string and returns the string in the reversed order. - public String reverseStringRec(String s) - { - // TODO: implement this method - - return "dummy string"; // replace this statement with your own return + public String reverseStringRec(String s) { + + if (s.equals("")) + return s; + return reverseStringRec(s.substring(1)) + s.substring(0, 1); + } - + // This method takes a reference to the head of a linked list. // It returns the reference to the head of the linked list in the reversed order. - public LNode reverseListRec(LNode head) - { - // TODO: implement this method - - return new LNode(-1); // replace this statement with your own return + public LNode reverseListRec(LNode head) { + if (head == null) + return head; + if (head.getLink() == null) + return head; + + LNode holder = reverseListRec(head.getLink()); + LNode next = head.getLink(); + + next.setLink(head); + head.setLink(null); + return holder; + } -} \ No newline at end of file +}