Question

In: Computer Science

a. Define a function less of type (String, List) -> List so that less(e, L) is...

a. Define a function less of type (String, List) -> List so that less(e, L) is a list of all the strings in L that are shorter than e.

b. Define a function more of type (String, List) -> List so that more(e, L) is a list of all the strings in L that are longer than e.

c. Replace the above functions with a function compare of type (String, List, (String, String) -> Boolean) such that depending on the lambda passed it could perform both as less or more. Kindly note that solutions are expected to use single value variables and no iteration.

Part a is done!

fun less(e:String, L:List<String>): List<String>
{
if (L.isEmpty()) return listOf()
if (L[0].length < e.length) return (listOf(L[0]) + less(e,L.subList(1,L.size)))
else return (less(e,L.subList(1,L.size)))
}
less("no", listOf("not","yes","a","hello"))

Answer In Kotlin Please.

Solutions

Expert Solution

fun less(e:String, L:List<String>): List<String>
{
    if (L.isEmpty()) return listOf()
    return if (L[0].length < e.length)
        (listOf(L[0]) + less(e,L.subList(1,L.size)))
    else
        (less(e,L.subList(1,L.size)))
}

fun more(e:String, L:List<String>): List<String>
{
    if (L.isEmpty()) return listOf()
    return if (L[0].length > e.length)
        (listOf(L[0]) + more(e,L.subList(1,L.size)))
    else
        (more(e,L.subList(1,L.size)))
}

fun compare(e:String, L: List<String>, f: (String, String) -> Boolean):List<String>{
    if (L.isEmpty()) return listOf()
    return if (f(e,L[0]))
        (listOf(L[0]) + compare(e,L.subList(1,L.size),f))
    else
        (compare(e,L.subList(1,L.size),f))
}


Related Solutions

Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
list and define function of the five phyletic characteristics of the Chordata. Then define larval and...
list and define function of the five phyletic characteristics of the Chordata. Then define larval and adult form in regard to the presence of four of the phyletic characteristics in larva vs adult form of the Ascidacea. Define neoteny and the evolutionary origin of the Vertebrata.
since firstN is a string it can't compare to an ArrayList * list so how am i suppose to write these parts comparing the string to the list[mid]
c++ question:int ArrayList::binarySearchID(string firstN) const{   int first = 0;         int last = length - 1;         int mid = (first + last) / 2;   bool found = false;   int index;   while (!found && last >= first)   {       if (firstN == list[mid])       {           found = true;       }       else if (firstN > list[mid])       {           first = mid - 1;       }       else if (firstN < list[mid])       {           last...
You are given a List L of generic type <T> , and another List P, which...
You are given a List L of generic type <T> , and another List P, which contains integers sorted in ascending order. The operation printLots(L,P) will print the elements in L that are in positions specified by P. For instance, if P = [1,3,4,6], the elements in positions 1,3,4, and 6 in L are printed. Write the procedure printLots(L,P). The code you provide should be the Java method itself (not pseudocode), the containing class is not necessary. You may use...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string...
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string and remove the characters so it becomes a mirrored word. example: Input: qswwbi Output: 4 remove q s b i "wow" is a mirror word. output : 0 Input : "if" output : 1 remove either I or f because it's two words. don't use 3rd parties library or java.util.
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT