Each problem gives the student from 1 to 10 points. There’s a class Problem that…
- User
Each problem gives the student from 1 to 10 points. There’s a class Problem that represents a coding task. It has member variables statement, example, points. Rewrite the class so that mentors can use sum() built-in function on its instances to calculate the full amount of points in the course.
class Problem: title = None statement = None points = None
codedef __init__(self, title, statement, points): self.title = title self.statement = statement self.points = pointsExample
problems = [ Problem( title='Cat breed', statement='You have an abstract class CatBase...', points=7 ), Problem( title='Singleton', statement='Singleton is a design pattern...', points=10 ), Problem( title='Sum', statement='Rewrite the class so that mentors can use sum...', points=9 ), ]
sum(problems) # 26
- Assistant
class Problem: statement = None example = None points = None
codedef __init__(self, statement, example, points): self.statement = statement self.example = example self.points = points def __int__(self): return self.points - User
Each problem gives the student from 1 to 10 points. There’s a class Problem that represents a coding task. It has member variables statement, example, points. Rewrite the class so that mentors can use sum() built-in function on its instances to calculate the full amount of points in the course.
class Problem: title = None statement = None points = None
def init(self, title, statement, points): self.title = title self.statement = statement self.points = points
Example
problems = [ Problem( title=‘Cat breed’, statement=‘You have an abstract class CatBase…’, points=7 ), Problem( title=‘Singleton’, statement=‘Singleton is a design pattern…’, points=10 ), Problem( title=‘Sum’, statement=‘Rewrite the class so that mentors can use sum…’, points=9 ), ]
sum(problems) # 26
To resolve the task, use radd magic method
- Assistant
class Problem: statement = None example = None points = None
codedef __init__(self, title, statement, points): self.title = title self.statement = statement self.points = points def __radd__(self, other): return self.points + other def __int__(self): return self.points def __repr__(self): return f'{self.points}'problems = [ Problem( title='Cat breed', statement='You have an abstract class CatBase...', points=7 ), Problem( title='Singleton', statement='Singleton is a design pattern...', points=10 ), Problem( title='Sum', statement='Rewrite the class so that mentors can use sum...', points=9 ), ]
print(sum(problems)) # Output: 26
- User
Statement In classroom, we worked with a singly linked list, a structure in which every node contains the reference to the next node. Now you are given a class OrderedLinkedList representing an ordered doubly linked list of integer values. In a doubly linked list, every node contains a link to a previous node, too. In OrderedLinkedList, we want the nodes arranged in ascending order of value. Implement insert method that adds a new node at the proper location to maintain the sort. Method init is already implemented and instantiates a list with one node. The first node of the sequence is stored in variable head. Example insert method should work like this: a = OrderedLinkedList(8) a.insert(1) # 1, 8 a.insert(5) # 1, 5, 8 a.insert(5) # 1, 5, 5, 8
class Node:
codedef __init__(self, value, next_node, prev_node): self.value = value self.next = next_node self.prev = prev_nodeclass OrderedLinkedList: head = None
codedef __init__(self, value): if value: self.head = Node(value, None, None) def insert(self): ... - Assistantpython
class Node: def __init__(self, value, next_node=None, prev_node=None): self.value = value self.next = next_node self.prev = prev_node class OrderedLinkedList: def __init__(self, value): if value: self.head = Node(value) def insert(self, value): if self.head.value is None: self.head = Node(value) elif value <= self.head.value: new_node = Node(value, self.head) self.head.prev = new_node self.head = new_node else: current = self.head while current.next and current.next.value < value: current = current.next new_node = Node(value, current.next, current) current.next = new_node if new_node.next: new_node.next.prev = new_node