
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
![Part 5. Operator Overload:
add
Next, you will add the ability to use the addition operator (+) in conjunction with Simpy objects and floats.
You will implement
add
such that the left-hand side operand of an addition expression can be either a simpy object or a float value
using a Union type. The
add_method should return a new Simpy object and should not mutate the object the method is called on.
When the right-hand side of an addition expression is also a simpy object, you should assert that both objects' values attributes have equal
lengths. Then, you should produce a new simpy object where each item in its values attribute corresponds to the items of the original
Simpy objects at the same index added together. For example:
a = Simpy([1.0, 1.0, 1.0])
b = Simpy([2.0, 3.0, 4.0])
c = a + b
print(c)
# Output: Simpy([3.0, 4.0, 5.0])
When the right-hand side of an addition expression is a float value, you should produce a new simpy object where each item corresponds to
the item at the same index in the left-hand side simpy object added to the float. For example:
a = Simpy( [1.0, 2.0, 3.0])
b = a + 10.0
print (b)
# Output: Simpy([11.0, 12.0, 13.0])
The signature of the
add
function should be:
def_add_(self, rhs: Union[float, Simpy]) -> Simpy:
# This cell tests a Simpy + Simpy
a = Simpy([1.0, 1.0, 1.0])
b = Simpy([2.0, 3.0, 4.0])
C = a + b
print("Actual: ", c, " - Expected: Simpy ([3.0, 4.0, 5.0])")
print("Actual: ", a + a,
- Expected: Simpy([2.0, 2.0, 2.0])")
- Expected: Simpy([4.0, 6.0, 8.0])")
print("Actual: ", b + b,
Python
# This cell tests a Simpy + float
a = Simpy([1.0, 2.0, 3.0])
b 3D а + 10.0
print("Actual: ", b, "
print("Actual:
Expected: Simру ([11.0, 12.0, 13.0])")
Expected: Simpy ([2.0, 3.0, 4.0])")
a + 1, "
Python](https://content.bartleby.com/qna-images/question/1a1589f6-cc44-4dd7-b562-69c962033b31/ee8dc800-d8d8-4d91-8517-c4c48b8bbcfd/jv35i5g_thumbnail.png)
Transcribed Image Text:Part 5. Operator Overload:
add
Next, you will add the ability to use the addition operator (+) in conjunction with Simpy objects and floats.
You will implement
add
such that the left-hand side operand of an addition expression can be either a simpy object or a float value
using a Union type. The
add_method should return a new Simpy object and should not mutate the object the method is called on.
When the right-hand side of an addition expression is also a simpy object, you should assert that both objects' values attributes have equal
lengths. Then, you should produce a new simpy object where each item in its values attribute corresponds to the items of the original
Simpy objects at the same index added together. For example:
a = Simpy([1.0, 1.0, 1.0])
b = Simpy([2.0, 3.0, 4.0])
c = a + b
print(c)
# Output: Simpy([3.0, 4.0, 5.0])
When the right-hand side of an addition expression is a float value, you should produce a new simpy object where each item corresponds to
the item at the same index in the left-hand side simpy object added to the float. For example:
a = Simpy( [1.0, 2.0, 3.0])
b = a + 10.0
print (b)
# Output: Simpy([11.0, 12.0, 13.0])
The signature of the
add
function should be:
def_add_(self, rhs: Union[float, Simpy]) -> Simpy:
# This cell tests a Simpy + Simpy
a = Simpy([1.0, 1.0, 1.0])
b = Simpy([2.0, 3.0, 4.0])
C = a + b
print("Actual: ", c, " - Expected: Simpy ([3.0, 4.0, 5.0])")
print("Actual: ", a + a,
- Expected: Simpy([2.0, 2.0, 2.0])")
- Expected: Simpy([4.0, 6.0, 8.0])")
print("Actual: ", b + b,
Python
# This cell tests a Simpy + float
a = Simpy([1.0, 2.0, 3.0])
b 3D а + 10.0
print("Actual: ", b, "
print("Actual:
Expected: Simру ([11.0, 12.0, 13.0])")
Expected: Simpy ([2.0, 3.0, 4.0])")
a + 1, "
Python
![class Simpy:
values: list[float]
# TODO: Your constructor and methods will go here.
def
_init_(self, list) -> None:
"""initialize values""
self.values = list
def _str_(self) -> str:
"""Print value as a string.'
II II
return str(self.values)
def fill(self, full: float, f: int) -> None:
II II II
"""Fill the values with a specific repeating value."
self.values
[full] * f
def arange(self, start: float, stop: float, step: float = 1.0):
"""Fill in values attribute with range of values."
assert step != 0.0
r = start
while r != stop:
self.values.append(r)
r += step
def sum(self) -> float:
return sum(self.values)](https://content.bartleby.com/qna-images/question/1a1589f6-cc44-4dd7-b562-69c962033b31/ee8dc800-d8d8-4d91-8517-c4c48b8bbcfd/eapa9j9_thumbnail.png)
Transcribed Image Text:class Simpy:
values: list[float]
# TODO: Your constructor and methods will go here.
def
_init_(self, list) -> None:
"""initialize values""
self.values = list
def _str_(self) -> str:
"""Print value as a string.'
II II
return str(self.values)
def fill(self, full: float, f: int) -> None:
II II II
"""Fill the values with a specific repeating value."
self.values
[full] * f
def arange(self, start: float, stop: float, step: float = 1.0):
"""Fill in values attribute with range of values."
assert step != 0.0
r = start
while r != stop:
self.values.append(r)
r += step
def sum(self) -> float:
return sum(self.values)
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 1 images

Knowledge Booster
Recommended textbooks for you
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY

Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON

Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education

Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY