QuestionQuestion

Transcribed TextTranscribed Text

List Indexing This part ol the lab introduces indexing lists You should not use any loops your functions Though they would useful generalize your code (which doina later lab). goal this lab exercise isto understand the mechanics of using lists so you should focus only that In the poly directory create a file named poly py. Place your test cases in poly tests PY (the file template provided). You must provide at least three test cases for each of these functions This part will be executed with python poly tests. py Polynomial Arithmetic For this part the lab you will develop two functions that perform basic arithmetic on plynomials. polynomial will be represented list The values in the list represent the coefficients of the terms whereas the indices will represent the exponents for the terms This means that the polynomial .xx² 3.1x- will be represented by the following list Notice that term with exponent firsti the while the term with exponent is last (i.e., terms the areir reverse order they typically written mathematics; thisi done so that an element's index represents that term's exponent). poly poly polv poiv append This list can also be created directly as follows. This convenient when for instance, writing cases poly 3.1, You ray think this mapping of polynomial to a list bit odd. fact attributing meaning indices (and not the values within the list)isa pretty important skill that allows list be usedas more than justa substitution for bunch variables poly add2 In poly py. develop the bolt add2 function This function takes two polynomials degree two (lists c length three) arg. uments This function must return new list (i.e. do not modify the contents the input lists) representing the sum of the input polynomials Though the testing framework does work with lists does not support an almost equal check on the contents list Inthe provided testing file you will find assert Itcanb used ina testing function, as follows def test poly(self): 1.0] polys [1.2, 2.1, -3.21 poly3 poly poly add2 (polyl, poly2) self [3.5, 6.9, -2.21) poly_mult2 Develop the function mult2. This function take of degreetwo and compute the product the two polynomials Polynomial multiplication nota simple multiplication values the same index instead, think the distributive law (of which the FOIL method is a special simple case). Note carefully: The polynomial from multiplication will general be degree greater than the arau ment polynomials. In this case, the result can of at most degree your resul list (checked against your test cases) may be larger than initially expected For example:(2x+3)(x-1)=2x2+x-3 Again, though the use loops would allow one to generalize this function for this lab you cannot any loops. Think carefully about howt compute the product polynomials and how that relates the representation polynomials this lab. Iteration Patterns This part of the lab requires solving number of relatively simple problems using specific iteration patterns You will likely notice that the code the functions for each pattern very. very similar That's because you will be using patterns Patterns are good; they allow immers 1 think something like that with differen values with different operations and then do by changing the parts that are actually different You must provide at least three test cases for each of the following functions Map Pattern Develop these functions the map directory files map PY and map tests PY Each the functions for this part the labisto be implemented using the map pattern In this pattern, each value the result listis determined computation on the value in the corresponding position (i.e. at the same index) in the input list. Note: You must implement one of these functions using list comprehension one using for loop. andone using while loop. square The square all function computes and returns list of the square of each value the input list. add_n_all The add all function (taking two parameters) computes and returns list with each element set the sum parameter and the corresponding value the input list even_or_odd_al The even or odd all function takes inputa list integers and computes and returns list containing True/False representing whether each corresponding number in the input list seven. Filter Pattern Develop these functions the liter directory files filter py and filter tests PY Each of the functions for this part the labisto be implemented usino the filter pattern In pattern. the valuesi the result list are determined bv conditional tast each valuei the input list. Only those values that satisfy the condition lbe copied tothe result list It is conventional for the values in the result list tobe n the same relative order as in the input list Note: You must implement one of these functions using list comprehension one using a for loop. andone using while loop. are _positive The are _posit ive function returns list illpositive values in the input list The are greater than function (taking two parameters) returns list of all values the input that are greater than the parameter are_divisible_bv_n The are dividable by function (taking two parameters) returns all integers in the input list that are divisible by the parameter Demonstration Demonstrate the test cases from each part of the lab to your instructor have this lab recordeda completed In addition be prepared show the source code to your instructor Submission Demo the Lab5 and submit all py files the PolyLearn

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import unittest
import filter

class TestCases(unittest.TestCase):
   def assertListAlmostEqual(self, l1, l2):
      self.assertEqual(len(l1), len(l2))
      for el1, el2 in zip(l1, l2):
         self.assertAlmostEqual(el1, el2)
   
   def test_are_positive_1(self):
      L = [1,9,-2,9,-12,3]
      L_are_positive = filter.are_positive(L)
      self.assertListAlmostEqual(L_are_positive, [1,9,9,3])
      
   def test_are_positive_2(self):
      L = [-1,-4,-6]
      L_are_positive = filter.are_positive(L)
      self.assertListAlmostEqual(L_are_positive, [])

   def test_are_positive_3(self):
      L = [2,4,5,1,2]
      L_are_positive = filter.are_positive(L)
      self.assertListAlmostEqual(L_are_positive, [2,4,5,1,2])

   def test_are_greater_than_n_1(self):
      L = [1,9,-2,9,-12,3]
      L_are_greater_than_n = filter.are_greater_than_n(L,2)
      self.assertListAlmostEqual(L_are_greater_than_n, [9,9,3])

   def test_are_greater_than_n_2(self):
      L = [10,12,19,13,2]
      L_are_greater_than_n = filter.are_greater_than_n(L,5)
      self.assertListAlmostEqual(L_are_greater_than_n, [10,12,19,13])

   def test_are_greater_than_n_3(self):
      L = [1,-12, 3, -8, -17]
      L_are_greater_than_n = filter.are_greater_than_n(L,-10)
      self.assertListAlmostEqual(L_are_greater_than_n, [1,3,-8])
                           
   def test_are_dividable_by_1(self):
      L = [1,9,-2,9,-12,3]
      L_are_dividable_by_n = filter.are_dividable_by_n(L,3)
      self.assertListAlmostEqual(L_are_dividable_by_n, [9,9,-12,3])

   def test_are_dividable_by_1(self):
      L = [1,9,-2,9,-12,3]
      L_are_dividable_by_n = filter.are_dividable_by_n(L,3)
      self.assertListAlmostEqual(L_are_dividable_by_n, [9,9,-12,3])

   def test_are_dividable_by_2(self):
      L = [11,56,14,8]
      L_are_dividable_by_n = filter.are_dividable_by_n(L,3)
      self.assertListAlmostEqual(L_are_dividable_by_n, [])...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$35.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats