Question
USE PYTHON3; DO NOT IMPORT ANY PACKAGES Please do all of number 1,and do not add or remove any parameters. Thank you! 1. a) The
USE PYTHON3; DO NOT IMPORT ANY PACKAGES
Please do all of number 1,and do not add or remove any parameters. Thank you!
1.
a) The Course class is used to represent a course at UCSD. It has the following four instance attributes:
-
course_code (str): the code associated with the course, e.g. "DSC20"
-
name (str): the name of the course, e.g. "Intro to Programming"
-
instructor (str): the name of the instructor, e.g. "Marina Langlois"
-
enrollment_limit (int): the maximum number of students allowed to enroll in the course
-
is_upperdiv (boolean): either True or False, indicates whether the course is an upper division course
In this question, you will implement the string representation function __str__(), and object representation function __repr__() of the Course class. We have implemented the constructor for you.
Let's say that we have initialized a Course object:
>>> dsc20 = Course("DSC20", "Intro to Programming", "Marina Langlois", 150, False)
-
After implementing __str__(), we can use print() to print out the string representation of a Course object in a format like this:
>>> print(dsc20)
DSC20: Intro to Programming, a lower-division course instructed by Marina Langlois, allows 150 students to enroll.
Note that when is_upperdiv is True, it's "an upper-division" in the string representation, rather than "a lower-division".
-
After implementing __repr__(), simply typing the object variable name in the command line will give you a piece of code that initializes a Course object:
>>> dsc20
Course('DSC20', 'Intro to Programming', 'Marina Langlois', 150, False)
Please see the doctests for more examples.
Hint:
str.format() is a really helpful function for formatting strings.
def course_doctests():
""" Doctests for Course. >>> dsc20 = Course("DSC20", "Intro to Programming", "Marina Langlois", ... 150, False) >>> print(dsc20) DSC20: Intro to Programming, a lower-division course instructed by Marina Langlois, allows 150 students to enroll. >>> dsc20 Course('DSC20', 'Intro to Programming', 'Marina Langlois', 150, False)
>>> dsc106 = Course("DSC106", "Intro to Data Visualization", ... "Thomas Powell", 100, True) >>> print(dsc106) DSC106: Intro to Data Visualization, an upper-division course instructed by Thomas Powell, allows 100 students to enroll. >>> dsc106 Course('DSC106', 'Intro to Data Visualization', 'Thomas Powell', 100, True)
# TODO: Create three additional Course instances, and for each object create # a set of doctests similar to the above ones. """ return
class Course: """ # TODO: Add class description # """
def __init__(self, course_code, name, instructor, \ enrollment_limit, is_upperdiv): """ Constructor of Course class. DO NOT CHANGE THIS. """ self.course_code = course_code self.name = name self.instructor = instructor self.enrollment_limit = enrollment_limit self.is_upperdiv = is_upperdiv
def __str__(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def __repr__(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
b)
Write a recursive function that takes two sequences of numeric values (main and sub), and calculate two kinds of sum of the main sequence:
-
Sum of intersection: the sum of all numbers in the main sequence that also appear in the sub sequence.
-
Sum of differences: the sum of all numbers in the main sequence that do not appear in the sub sequence.
This function should return a tuple of (sum_of_intersection, sum_of_difference), if the main sequence is empty, return (0, 0).
You CANNOT use sum(), range(), or any loops/list comprehensions.
def find_two_sums_rec(main, sub):
"""
>>> main_seq = [0, 1, 1, 2, 3, 3, 4, 5, 5]
>>> find_two_sums_rec(main_seq, [])
(0, 24)
>>> find_two_sums_rec(main_seq, [1, 2])
(4, 20)
>>> find_two_sums_rec(main_seq, [3, 4, 5])
(20, 4)
"""
# YOUR CODE GOES HERE #
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