Question

In: Computer Science

Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int)...

Exercise: Recursive List Processing

Part 1: Write a recursive method that returns the length (an int) of the longest String in a List of Strings. For each recursive call, remove the first String from the list and return the greater of the length of the String you removed or the result of calling the method on the remainder of the list. The base case should be that the size of the list is 0. Write a driver to verify that your code is correct.

Part 2: Write a recursive method that takes a list of Strings as its only parameter and returns a list of Integers, which are the lengths of the Strings in the List. This part of this exercise will be significantly similar to the lecture example in which a list of ints is consumed, producing a list of the squares of the ints. Each recursive call should send the list, but without the first String; as the recursion unwinds, create the list of Integers and add the length of the String your stripped out in the current call to the *beginning* of the list. Write a driver to verify that your code is correct. (language is java for this)

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

public class Recursion {

    // PART 1
    public static int longestLength(String[] s) {

        if (s.length == 0) return 0;
        else if (s.length == 1) return s[0].length();

        else {
            String[] remaining = Arrays.copyOfRange(s, 1, s.length);
            int lengthRemaining = longestLength(remaining);
            if (lengthRemaining > s[0].length()) return lengthRemaining;
            else return s[0].length();
        }
    }


     // PART 2
    public static int[] lengthList(String[] s) {

        if (s.length == 0) return new int[]{0};
        else if (s.length == 1) return new int[]{s[0].length()};
        else {
            String[] remaining = Arrays.copyOfRange(s, 1, s.length);
            int[] lengths = lengthList(remaining);

            int[] newLengths = new int[1 + lengths.length];
            newLengths[0] = s[0].length();
            for (int i = 1; i <= lengths.length; i++) newLengths[i] = lengths[i - 1];
            return newLengths;

        }

    }


    public static void main(String[] args) {


        String[] s = {"1", "123", "12345", "123456789", "1234567890"};
        System.out.println(longestLength(s));
        
        int lengths[] = lengthList(s);
        System.out.println(java.util.Arrays.toString(lengths));
    }
}

=======================================================================


Related Solutions

write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT