Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need SCALA HELP: ## Problem 1A (5 points) Write a function `countZeros` that inputs a list of numbers `lst` and counts how many elements of

Need SCALA HELP:

## Problem 1A (5 points)

Write a function `countZeros` that inputs a list of numbers `lst` and counts how many elements of `lst` are equal to zero.

### Restrictions - Your function must be recursive (not necessary that it be tail recursive) - You __cannot__ use loops (for-loops, while loops etc..), and __no__ mutables (var). - You __cannot__ use return statement in your function. - List Operations allowed: - list.length - length of a list - list.head - extract the first element of a list - list.tail - extract the sublist from the second element to last element of list. - No other list API functions or API functions of any other datastructure allowed. Eg., do not look to convert your list into an array and use some Array API function.

## Problem 1B (5 points)

Write a tail recursive version of the `countZero` function described in Problem 1A. Call your tail recursive function `countZeroTail`.

Place a `@tailrec` decorator in font of your function. Some scala versions (Scala < 2.12?) may require you to declare your function with the `final` keyword in front.

~~~ final def countZeroTail(lst:List[Int],....): Int = { ... } ~~~

### Restrictions

Same restrictions as problem 1A.

Additionally your function must be tail recursive.

Calling `countZero` from inside your `countZeroTail` function is not allowed.

## Problem 1 C (10 points)
 
Write a tail recursive function `extractSubList` that inputs three arguments `lst` of integers, index `i` and length `j`.
 
Given a list of integers `lst`, an index `i` into the list and a length `j`, extract all the elements in the original list `lst(i), ..., lst(i+j-1)` into a new list. Handle corner cases by returning an empty list when (a) `j <= 0` or (b) `i+j` is greater than the length of the list `lst`.
 
__Hint:__ Write a function `extractSubList` that handles all the _corner_ cases above and calls a "helper" function `extractSubListHelper` for all the _non corner_ cases. Make sure that `extractSubListHelper` is itself tail recursive. Make sure that the `@tailrec` decorator is applied to the helper function.
 
### Restrictions
 
 - Same restrictions as problem 1A.
 - Additionally your helper function must be tail recursive.
 - You __can__ use `list.head` and `list.tail` functions. Read about them here: https://www.tutorialspoint.com/scala/scala_lists.htm
 - You __can__ use `list.length`
 - You __can__ use list cons operator `elt::list` to append an element in front of a list, and also use `:+` operator to append to the back of the list. You __can__ also use `++` operator (or `:::` operator) to append two lists to each other.
 - You __can__ reverse a list by calling the List API function `list.reverse`.
 - You __cannot__ access list elements using their indices: Eg., `lst(i)` to directly access the ith element is forbidden.
 - No other list API functions allowed.

[ ]:

import scala.annotation.tailrec
 
 
@tailrec
// YOUR CODE HERE
???

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions