Due Tuesday 20-Feb, at 9:00pm
hw6
hw6.py
(We are not providing a starter file this week.)hw6.py
and build the functions as described below.hw6.py
to Gradescope. You may submit up to 15 times, but only your last submission counts.Do not use sets, dictionaries, try/except, classes, or recursion this week. The autograder (or a manual CA review later) will reject your submission entirely if you do.
Like in the previous assignment, we will be grading your code based on whether it follows the 15-112 style guide. We may deduct up to 10 points from your overall grade for style errors. We highly recommend that you try to write clean code with good style all along, rather than fixing your style issues at the end. Good style helps you code faster and with fewer bugs. It is totally worth it. In any case, style grading already started, so please use good style from now on!
There is no skeleton file provided this week, meaning that we also are not providing testcases. You should write testcases for each function. (You can find some nice ways to test in the write-up below, but you will need to translate those to actual testcases.)
We can represent a polynomial as a list of its
coefficients. For example, we will represent the polynomial
2x3 + 3x2 + 4 with the list
[2, 3, 0, 4]
.
With this in mind, write the function
multiplyPolynomials(p1, p2)
, which takes two lists
representing polynomials as described above, and returns the list
representing the polynomial that is the product of p1
and p2
.
For example, multiplyPolynomials([2, 3, 0, 4], [1, 5])
represents the problem (2x3 + 3x2 + 4)(x + 5).
This equals
2x4 + 13x3 + 15x2 + 4x + 20. So the
function would return [2, 13, 15, 4, 20]
.
How did we find that answer? To multiply two polynomials
p1
and p2
, you do this:
p1
by each term in
p2
, thenx
.This exercise consists of a list of rules that specify which positive numbers can be included in the result. For example:
The first 6 integers that follow this rule are: [3, 6, 9, 12, 15, 18]
.
We can add a second rule to the list:
The first 6 integers that follow these rules are: [3, 6, 12, 15, 21, 24]
.
We can add two more rules:
The first 6 integers that follow these rules are: [6, 12, 30, 42, 48, 60]
.
The rules will always have 3 parts (always in this order), each with 2 options:
'x'
or 'x%N'
, where N
is a positive
integer.
'must be'
or 'must not be'
.
'a multiple of M'
or 'equal to M'
,
where M
is a non-negative integer.
We will only test your code on valid rules.
With this in mind, first write the helper function
isAcceptedValue(x, rules)
, which takes a positive
integer x
and a list of rules, and returns True
if
x
follows all the rules and False
otherwise.
Next, write the function firstNAcceptedValues(n, rules)
,
which takes a positive integer n
and a non-empty list of rules, and
returns a list containing the first n
positive integers that follow all
the given rules.
Hints:
x
or
x%n
, just check if '%'
is
anywhere in the rule.
'must be'
or
'must not be'
, just check if 'not'
is anywhere in the rule.
'a multiple of'
or
'equal to'
, just check if 'equal'
is anywhere in the rule.
.split()
return lists,
which you now know how to index into.