Question: # please do this on scala, I appreciate your help A polynomial of the form 0+1+22+...+a0+a1x+a2x2+...+anxn is represented by a list of coefficients [a0, ...,

# please do this on scala, I appreciate your help

A polynomial of the form 0+1+22+...+a0+a1x+a2x2+...+anxn is represented by a list of coefficients [a0, ..., an]. We wish to implement a polynomial class in Scala. Complete the missing methods.

There are no restrictions in the use of loops/var in this problem. Also any list API function may be used.

Note: We will guarantee in our implementation that the highest coefficient an != 0. This will simplify things a lot.

(A, 5 points) Chop off suffix zeros

First implement a function chopOffSuffixZeros(lst: List[Double]): List[Double] that given a list of numbers, removes elements from the end that are equal to 0.0, ensuring that the returned list has a non zero element at its last position.

Examples:

chopOffSuffixZeros( List(1.0, 2.1, -1.0, 0.0, 0.0) ) should return List(1.0, 2.1, -1.0).

chopOffSuffixZeros( List(0.0, 0.1, 0.0, 0.0, 0.0) ) should return List(0.0, 0.1).

chopOffSuffixZeros( List(0.0, 0.0, 0.0, 0.0, 0.0, 1.0) ) should return List(0.0, 0.0, 0.0, 0.0, 0.0, 1.0).

chopOffSuffixZeros( Nil ) should return Nil (Nil stands for empty list in scala).

  • You may use for /while loops, var etc..
  • You can use list API functions such as slice, last, size, list concatenation, etc..
  • However, this is an example of a problem where the recursive solution is way easier than the one using for loop. Try it out!!

# please do this on scala, I appreciate your help A polynomial

{l def chopoffSuffixZeros (1st: List[Double]): List[Double] // YOUR CODE HERE ??? } testWithMessage( chopoffSuffixZeros( List(1.0, 2.1, -1.0, 0.0, 0.0) ) List(1.0, 2.1, -1.0), "1") Sa testWithMessage chopoffSuffixZeros ( List(1.0, 2.1, -1.0) ) List(1.0, 2.1, -1.0), "2") testWithMessage( chopoffSuffixZeros ( List(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1) ) == List(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1), "3") testWithMessage( chopoffSuffixZeros( List() ) List(), "4") testWithMessage ( chopoffSuffixZeros ( List(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) ) List(), "5") passed (5) {l def chopoffSuffixZeros (1st: List[Double]): List[Double] // YOUR CODE HERE ??? } testWithMessage( chopoffSuffixZeros( List(1.0, 2.1, -1.0, 0.0, 0.0) ) List(1.0, 2.1, -1.0), "1") Sa testWithMessage chopoffSuffixZeros ( List(1.0, 2.1, -1.0) ) List(1.0, 2.1, -1.0), "2") testWithMessage( chopoffSuffixZeros ( List(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1) ) == List(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1), "3") testWithMessage( chopoffSuffixZeros( List() ) List(), "4") testWithMessage ( chopoffSuffixZeros ( List(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) ) List(), "5") passed

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!