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!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started