Question

Write a fucntion which checks that numbers in provided list are correctly sorted.Please use tail recursion(IN...

Write a fucntion which checks that numbers in provided list are correctly sorted.Please use tail recursion(IN SCALA LANGUAGE)
Thanks in advance

Homework Answers

Answer #1

A tail recursive function is a one which has recursive call as the last command of the function. Nothing after the recursive call should be present in the function.

Kindly find below the scala code for given problem:

def arraySortedOrNot(list: List[Int]): Int = {
        // list has one or no element or the rest are already checked and approved.
        if (list.length == 1 || list.length == 0)
            true
 
        // Unsorted pair found (Equal values allowed)
        var last = list.last
        //removes last element in the list
        list.dropRight(1)
        if (last < list.last)
            false
 
        // Last pair was sorted, Keep on checking
        arraySortedOrNot(list);
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!
(PYTHON- NO LOOPS) Complete the body of the following function filter_odd(L) which uses structural recursion, consumes...
(PYTHON- NO LOOPS) Complete the body of the following function filter_odd(L) which uses structural recursion, consumes a list of natural numbers, and returns a list of numbers that contains only the odd numbers from the original list. Do not use abstract list functions. if L == []: return [] else: if L[0] % 2 != 0:    else:
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most factors (divisors without remainder). For example: >>> most_factors([5,10,16,20,25]) 20 # because 20 has the most factors of any of these numbers # 6 factors, i.e., [1, 2, 4, 5, 10, 20] >>> most_factors([1, 2, 3, 4, 5]) 4 # because 4 has the most factors of any of these numbers # 3 factors, i.e., [1, 2, 4] Hints: For each element in numbers, call...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
Use the list of sequence ["ATATCCG", "TCCG", "ATGTACTG", "ATGGCTG", "ATCA"] as an example, write a program...
Use the list of sequence ["ATATCCG", "TCCG", "ATGTACTG", "ATGGCTG", "ATCA"] as an example, write a program that finds the sequence with the highest GC content (the percentage of G and C nucleotides) among a list of sequences. The program prints the sequence and its GC content. Using python, thanks!
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
Use the Design Recipe to write a function count_evens_NxN,  that consumes a nested list representing a matrix...
Use the Design Recipe to write a function count_evens_NxN,  that consumes a nested list representing a matrix of size NxN. The function should return the number of even numbers in the matrix. For this function, 0 is considered an even number.  Include a Docstring! Note: You may assume the list argument passed to the function is a nested list of integers. Write 3 assert_equal statements to test your function.
Write a script to display numbers from 1 to n, where n is an integer provided...
Write a script to display numbers from 1 to n, where n is an integer provided by users (if not, default to 10). Hint: use “read” command to accept user input. a) Display the source code in an editor (#4-9) b) Execute your script in the terminal, and display the command and the result (#4-10)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT