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...
The use of list or any other data type not allowed only string and string methods...
The use of list or any other data type not allowed only string and string methods .Hint: Depending on how you plan to solve this problem, accumulator variable initialized as an empty string may help in this question. Write a function called weaveop, that takes a single string parameter (s) and returns a string. This function considers every pair of consecutive characters in s. It returns a string with the letters o and p inserted between every pair of consecutive...
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...
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
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...
Exercise Define a function that generates a random code. The functions takes in a string colors...
Exercise Define a function that generates a random code. The functions takes in a string colors whose characters represent distinct colors to choose from, and a positive integer code_length representing the length of the code. For instance, get_code('ROYGBP',4) returns a code of 4 code pegs randomly with colors chosen from 'R'ed, 'O'range, 'Y'ellow, 'G'reen, 'B'lue, and 'P'urple. One possible outcome is 'ROYY'. Following is my python code, and I don't know how to continue, can someone help me to finished...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT