f | from fractions import Fraction | f | from fractions import Fraction |
| from math import floor | | from math import floor |
| | | |
| class Sausage: | | class Sausage: |
n | FILLING_SIZE = 12 | n | FULL_SIZE = 1 |
| UNIT_VOLUME = 1 | | SLICE_SIZE = 12 |
| | | |
n | def __init__(self, filling='pork!', quantity='1'): | n | def __init__(self, stuffing='pork!', volume='1'): |
| self.filling = str(filling) | | self.stuffing = str(stuffing) |
| self.quantity = Fraction(quantity) if quantity else Fraction(0) | | self.volume = Fraction(volume) if volume else Fraction(0) |
| self.quantity = max(self.quantity, Fraction(0)) | | if self.volume < 0: |
| | | self.volume = Fraction(0) |
| | | |
| def __repr__(self): | | def __repr__(self): |
n | if self.quantity * self.FILLING_SIZE < 1: | n | if self.volume * self.SLICE_SIZE < 1: |
| return '/|\n||\n||\n||\n\\|' | | return '/|\n||\n||\n||\n\\|' |
n | full_buns = floor(self.quantity) | n | full_sausages = floor(self.volume) |
| partial_bun_length = int(self.quantity % self.UNIT_VOLUME * self | | partial_sausage_length = int(self.volume % self.FULL_SIZE * self |
| .FILLING_SIZE) | | .SLICE_SIZE) |
| buns = [] | | sausage_parts = [] |
| for _ in range(full_buns): | | for _ in range(full_sausages): |
| buns.append(self._generate_full_bun()) | | sausage_parts.append(self._create_full_sausage()) |
| if partial_bun_length > 0: | | if partial_sausage_length > 0: |
| buns.append(self._generate_partial_bun(partial_bun_length)) | | sausage_parts.append(self._create_partial_sausage(partial_sa |
| | | usage_length)) |
| return self._combine_buns(buns) | | return self._combine_sausages(sausage_parts) |
| | | |
n | def _generate_full_bun(self): | n | def _create_full_sausage(self): |
| filling_content = (self.filling * (self.FILLING_SIZE // len(self | | stuff = (self.stuffing * (self.SLICE_SIZE // len(self.stuffing) |
| .filling) + 1))[:self.FILLING_SIZE] | | + 1))[:self.SLICE_SIZE] |
| return f'/------------\\\n|{filling_content}|\n|{filling_content | | return f'/------------\\\n|{stuff}|\n|{stuff}|\n|{stuff}|\n\\--- |
| }|\n|{filling_content}|\n\\------------/' | | ---------/' |
| | | |
n | def _generate_partial_bun(self, bun_length): | n | def _create_partial_sausage(self, stuffing_length): |
| filling_content = (self.filling * (bun_length // len(self.fillin | | stuff = (self.stuffing * (stuffing_length // len(self.stuffing) |
| g) + 1))[:bun_length] | | + 1))[:stuffing_length] |
| return f'/{'-' * bun_length}|\n|{filling_content}|\n|{filling_co | | return f'/{'-' * stuffing_length}|\n|{stuff}|\n|{stuff}|\n|{stuf |
| ntent}|\n|{filling_content}|\n\\{'-' * bun_length}|' | | f}|\n\\{'-' * stuffing_length}|' |
| | | |
n | def _combine_buns(self, buns): | n | def _combine_sausages(self, sausages): |
| rows = [bun.split('\n') for bun in buns] | | rows = [sausage.split('\n') for sausage in sausages] |
| combined_rows = [''.join(row) for row in zip(*rows)] | | combined = [''.join(row) for row in zip(*rows)] |
| return '\n'.join(combined_rows) | | return '\n'.join(combined) |
| | | |
| def __abs__(self): | | def __abs__(self): |
n | return abs(self.quantity) | n | return abs(self.volume) |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
n | total_size = self.quantity + other.quantity | n | new_volume = self.volume + other.volume |
| return Sausage(self.filling, max(total_size, Fraction(0))) | | return Sausage(self.stuffing, max(new_volume, Fraction(0))) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
n | reduced_size = self.quantity - other.quantity | n | new_volume = self.volume - other.volume |
| return Sausage(self.filling, max(reduced_size, Fraction(0))) | | return Sausage(self.stuffing, max(new_volume, Fraction(0))) |
| | | |
n | def __mul__(self, multiplier): | n | def __mul__(self, factor): |
| if isinstance(multiplier, int) and multiplier >= 0: | | if isinstance(factor, int) and factor >= 0: |
| return Sausage(self.filling, self.quantity * multiplier) | | return Sausage(self.stuffing, self.volume * factor) |
| raise ValueError('Multiplier must be a non-negative integer.') | | raise ValueError('Multiplication factor must be a non-negative i |
| | | nteger.') |
| | | |
n | def __rmul__(self, multiplier): | n | def __rmul__(self, factor): |
| return self.__mul__(multiplier) | | return self.__mul__(factor) |
| | | |
| def __truediv__(self, divisor): | | def __truediv__(self, divisor): |
| if isinstance(divisor, int) and divisor > 0: | | if isinstance(divisor, int) and divisor > 0: |
n | return Sausage(self.filling, self.quantity / divisor) | n | return Sausage(self.stuffing, self.volume / divisor) |
| raise ValueError('Divisor must be a positive integer.') | | raise ValueError('Division divisor must be a positive integer.') |
| | | |
| def __bool__(self): | | def __bool__(self): |
t | return self.quantity != 0 | t | return self.volume != 0 |