Write a recursive method triangle that takes a non-negative
int n and returns the nth
triangular number: the sum of all the whole numbers from
1 to n. So triangle(4) is
10, i.e. 1 + 2 + 3 + 4, and
triangle(0) is 0.
Write a recursive method largestDigit that takes a
non-negative int and returns its largest digit. So
largestDigit(2714) is 7. Recall that
n % 10 is the last digit of n and
n / 10 is all the digits but the last.
In Pascal’s triangle every row starts and ends with 1 and
every other number is the sum of the two numbers above it: the one
directly above and the one above and to the left.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Write a recursive method pascal that takes two
ints, row and col, and returns
the number at that position in the triangle. Rows and columns are both
numbered from 0, so pascal(4, 2) is
6 and pascal(3, 0) is 1. You can
assume col is between 0 and row.
Like fibonacci, this one makes two recursive calls.
Write a recursive method toBinary that takes a non-negative
int and returns a String of 0s
and 1s that is the number written in binary, with no extra
leading zeros. So toBinary(6) is "110",
toBinary(1) is "1", and
toBinary(0) is "0". Think about how
n % 2 and n / 2 relate to the binary digits of
n. Or, if you want to be fancy, think
about & and >>>.
Write a recursive method doubleChars that takes a
String and returns a new String with every
character doubled. So doubleChars("cat") is
"ccaatt".
Write a recursive method interleave that takes two
Strings and returns a new String made by
taking characters alternately from the two strings, starting with the
first. When one string runs out, the rest of the other one goes on the
end. So interleave("abc", "123") is "a1b2c3"
and interleave("ab", "12345") is "a1b2345".
Write a recursive method countOccurrences that takes two
Strings, s and sub, and returns
the number of times sub occurs in s, not
counting overlapping occurrences. So
countOccurrences("banana", "an") is 2 and
countOccurrences("aaaaa", "aa") is also 2:
once you’ve found an occurrence, none of its characters can be part of
another. You can assume sub is not empty. Unlike most
recursive methods on strings, this one doesn’t always shorten the string
by the same amount.
Write a recursive method isSubsequence that takes two
Strings, sub and s, and returns a
boolean indicating whether sub is a
subsequence of s, meaning all the characters of
sub appear in s, in the same order, but not
necessarily next to each other. So
isSubsequence("ace", "abcde") is true but
isSubsequence("aec", "abcde") is false. The
empty string is a subsequence of every string.
Write a recursive method last that takes a non-empty
List<Integer> and returns the last number in it. The
base case is a list with one element.
Write a recursive method isSorted that takes a
List<Integer> and returns a boolean
indicating whether the numbers are in order from smallest to largest.
Equal numbers next to each other are in order, so
[1, 2, 2, 5] is sorted. Think about how long a list has to
be before it can possibly be out of order.
Write a recursive method evens that takes a
List<Integer> and returns a new
List<Integer> containing just the even numbers from
the original list, in the same order. So given
[1, 2, 3, 4, 6] it returns [2, 4, 6]. The
cons helper is how you build the new list and
new ArrayList<>() makes an empty list. Don’t change
the list you were given.
Write a recursive method merge that takes two
List<Integer>s, each of which is already sorted, and
returns a sorted List<Integer> containing all the
numbers from both lists. So merging [1, 4, 9] and
[2, 3, 10] gives [1, 2, 3, 4, 9, 10]. The
smallest number of all has to be the first element of one of the two
lists. Don’t change the lists you were given.
A dozen more recursive methods: four that recurse on an int,
four on Strings, and four on Lists. They are all
written the same way: a base case for the smallest version of the problem,
and a recursive case that gets the answer to a smaller version of the
problem and does something with it.
The starter code provides helpers for taking strings and lists apart:
first returns the first character of a string (as a
one-character String) or the first element of a list, and
rest returns everything after it. Neither can be called on an
empty string or list. There is also a helper for putting a list together:
cons(x, list) returns a new list that is x
followed by everything in list. You may also use
String methods like isEmpty,
length, equals, substring, and
startsWith and List methods like
isEmpty and size.
No loops and no streams; the point is the recursion. Also no reaching for the library method that already does the job.