Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# 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!!

image text in transcribed

{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

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

Evaluate the following limits. lim x e3x 3ex + 5

Answered: 1 week ago