Palindromes Complete the function palindrome(String text). Return true if text is a palindrome; if the word is identical forward and backward. Otherwise, return false. Example: palindrome(“racecar”) == true palindrome(“kayaks”) == false See main() for more examples. You can use text.charAt(i) to find the character at index i of text. Use recursion. palindrome() should call itself. No loops allowed. b. Complete the function nestedBrackets(String s). This function should find properly nested brackets within s. Return true if brackets are opened and closed correctly, and false otherwise. Single pairs and nested pairs are valid. For example: z[z]z, [zz[zz]z], [z[[z]zzz]], zzzzz Incorrect pairings and multiple separate pairs are not valid. For example: zz[zz, ]zzz[, [zzz][z], [z[z][z]z] See main() for more examples. nestedBrackets() should call firstIndexOf() and lastIndexOf() to help you solve the problem. Use recursion. nestedBrackets() should call itself. No loops allowed inside this function. c. Collatz For this problem, trace the execution of your program on paper as part of your programming process. See the tracing template at the end of this document. Write whatever notes you feel are useful for solving the problem. You will upload an image and description of your tracing notes after completing the exercise. Complete the function collatz(int n, int len). Find the length of the Collatz sequence starting from some n > 0. The numbers of the sequence are computed as follows: If n is even, the next number is n / 2 If n is odd, the next number is 3n+1 When n reaches 1, the sequence is over. For example: Start = 10 : Sequence: 10, 5, 16, 8, 4, 2, 1 Length: 7 Return the length of the sequence when n reaches 1. The sequence includes the starting number and the final 1. Use recursion. collatz() should call itself. No loops allowed.
1.
a.Palindromes
Complete the function palindrome(String text). Return true if text is a palindrome; if the word is identical forward and backward. Otherwise, return false. Example: palindrome(“racecar”) == true palindrome(“kayaks”) == false See main() for more examples. You can use text.charAt(i) to find the character at index i of text. Use recursion. palindrome() should call itself. No loops allowed. |
b.
Complete the function nestedBrackets(String s). This function should find properly nested brackets within s. Return true if brackets are opened and closed correctly, and false otherwise. Single pairs and nested pairs are valid. For example: z[z]z, [zz[zz]z], [z[[z]zzz]], zzzzz Incorrect pairings and multiple separate pairs are not valid. For example: zz[zz, ]zzz[, [zzz][z], [z[z][z]z] See main() for more examples. nestedBrackets() should call firstIndexOf() and lastIndexOf() to help you solve the problem. Use recursion. nestedBrackets() should call itself. No loops allowed inside this function. |
c. Collatz
For this problem, trace the execution of your program on paper as part of your programming process. See the tracing template at the end of this document. Write whatever notes you feel are useful for solving the problem. You will upload an image and description of your tracing notes after completing the exercise.
Complete the function collatz(int n, int len). Find the length of the Collatz sequence starting from some n > 0. The numbers of the sequence are computed as follows: If n is even, the next number is n / 2 If n is odd, the next number is 3n+1 When n reaches 1, the sequence is over. For example: Start = 10 : Sequence: 10, 5, 16, 8, 4, 2, 1 Length: 7 Return the length of the sequence when n reaches 1. The sequence includes the starting number and the final 1. Use recursion. collatz() should call itself. No loops allowed. |
Step by step
Solved in 3 steps with 3 images