point.py: import json class Point: def init(self, x=0, y=0): self.x = x self.y = y…
- User
point.py:
import json
class Point: def init(self, x=0, y=0): self.x = x self.y = y
@classmethod def from_string(cls, s): x, y = map(float, s.split(‘,’)) return cls(x, y) def to_json(self): return json.dumps(self.dict) @classmethod def from_json(cls, json_str): data = json.loads(json_str) return cls(data) def repr(self): return f"Point({self.x}, {self.y})“vector.py:
from point import Point import json import math
class Vector: def init(self, start=None, end=None): self.start = start if start is not None else Point() self.end = end if end is not None else Point()
def length(self): return math.sqrt((self.end.x - self.start.x) 2 + (self.end.y - self.start.y) ** 2) def to_json(self): return json.dumps({‘start’: self.start.dict, ‘end’: self.end.dict}) @classmethod def from_json(cls, json_str): data = json.loads(json_str) start = Point(**data[‘start’]) end = Point(**data[‘end’]) return cls(start, end) def add(self, other): new_start = self.start new_end = Point(self.end.x + other.end.x - other.start.x, self.end.y + other.end.y - other.start.y) return Vector(new_start, new_end) def repr(self): return f"Vector({self.start}, {self.end})” # … other methods …container.py:
import json
class Container: def init(self): self.items = []
def add_item(self, item): self.items.append(item) def remove_item(self, item): self.items.remove(item) def to_json(self): return json.dumps([item.to_json() for item in self.items]) @classmethod def from_json(cls, json_str): items_data = json.loads(json_str) container = cls() for item_data in items_data: item = Vector.from_json(item_data) container.add_item(item) return container # … other methods …init.py:
from .point import Point from .vector import Vector from .container import Container
- Assistant
- User
Реализуйте класс, описывающий точку и класс вектор. Предусмотрите: метод создания класса из строки; метод сохранения/загрузки из json; другие методы или свойства (не менее 4-х). Добавьте класс контейнер, для которого реализуйте методы: добавления/удаления элементов, сохранения/загрузки из json.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.