f | from fractions import Fraction | f | from fractions import Fraction |
| from math import floor | | from math import floor |
| | | |
| class Sausage: | | class Sausage: |
n | FULL_SIZE = 1 | n | UNIT_VOLUME = 1 |
| SLICE_SIZE = 12 | | FILLING_LENGTH = 12 |
| | | |
n | def __init__(self, stuffing='pork!', volume='1'): | n | def __init__(self, filling='pork!', size='1'): |
| self.stuffing = str(stuffing) | | self.filling = str(filling) |
| self.volume = Fraction(volume) if volume else Fraction(0) | | self.size = Fraction(size) if size else Fraction(0) |
| if self.volume < 0: | | if self.size < 0: |
| self.volume = Fraction(0) | | self.size = Fraction(0) |
| | | |
| def __repr__(self): | | def __repr__(self): |
n | if self.volume * self.SLICE_SIZE < 1: | n | if self.size * self.FILLING_LENGTH < 1: |
| return '/|\n||\n||\n||\n\\|' | | return '/|\n||\n||\n||\n\\|' |
n | full_sausages = floor(self.volume) | n | full_units = floor(self.size) |
| partial_sausage_length = int(self.volume % self.FULL_SIZE * self | | remainder_length = int(self.size % self.UNIT_VOLUME * self.FILLI |
| .SLICE_SIZE) | | NG_LENGTH) |
| sausage_parts = [] | | sausages = [] |
| for _ in range(full_sausages): | | for _ in range(full_units): |
| sausage_parts.append(self._create_full_sausage()) | | sausages.append(self._build_full_unit()) |
| if partial_sausage_length > 0: | | if remainder_length > 0: |
| sausage_parts.append(self._create_partial_sausage(partial_sa | | sausages.append(self._build_partial_unit(remainder_length)) |
| usage_length)) | | |
| return self._combine_sausages(sausage_parts) | | return self._merge_units(sausages) |
| | | |
n | def _create_full_sausage(self): | n | def _build_full_unit(self): |
| stuff = (self.stuffing * (self.SLICE_SIZE // len(self.stuffing) | | filling_content = (self.filling * (self.FILLING_LENGTH // len(se |
| + 1))[:self.SLICE_SIZE] | | lf.filling) + 1))[:self.FILLING_LENGTH] |
| return f'/------------\\\n|{stuff}|\n|{stuff}|\n|{stuff}|\n\\--- | | return f'/------------\\\n|{filling_content}|\n|{filling_content |
| ---------/' | | }|\n|{filling_content}|\n\\------------/' |
| | | |
n | def _create_partial_sausage(self, stuffing_length): | n | def _build_partial_unit(self, filling_length): |
| stuff = (self.stuffing * (stuffing_length // len(self.stuffing) | | filling_content = (self.filling * (filling_length // len(self.fi |
| + 1))[:stuffing_length] | | lling) + 1))[:filling_length] |
| return f'/{'-' * stuffing_length}|\n|{stuff}|\n|{stuff}|\n|{stuf | | return f'/{'-' * filling_length}|\n|{filling_content}|\n|{fillin |
| f}|\n\\{'-' * stuffing_length}|' | | g_content}|\n|{filling_content}|\n\\{'-' * filling_length}|' |
| | | |
n | def _combine_sausages(self, sausages): | n | def _merge_units(self, units): |
| rows = [sausage.split('\n') for sausage in sausages] | | rows = [unit.split('\n') for unit in units] |
| combined = [''.join(row) for row in zip(*rows)] | | merged = [''.join(row) for row in zip(*rows)] |
| return '\n'.join(combined) | | return '\n'.join(merged) |
| | | |
| def __abs__(self): | | def __abs__(self): |
n | return abs(self.volume) | n | return abs(self.size) |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
n | new_volume = self.volume + other.volume | n | combined_size = self.size + other.size |
| return Sausage(self.stuffing, max(new_volume, Fraction(0))) | | return Sausage(self.filling, max(combined_size, Fraction(0))) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
n | new_volume = self.volume - other.volume | n | reduced_size = self.size - other.size |
| return Sausage(self.stuffing, max(new_volume, Fraction(0))) | | return Sausage(self.filling, max(reduced_size, Fraction(0))) |
| | | |
n | def __mul__(self, factor): | n | def __mul__(self, multiplier): |
| if isinstance(factor, int) and factor >= 0: | | if isinstance(multiplier, int) and multiplier >= 0: |
| return Sausage(self.stuffing, self.volume * factor) | | return Sausage(self.filling, self.size * multiplier) |
| raise ValueError('Multiplication factor must be a non-negative i | | raise ValueError('Multiplication multiplier must be a non-negati |
| nteger.') | | ve integer.') |
| | | |
n | def __rmul__(self, factor): | n | def __rmul__(self, multiplier): |
| return self.__mul__(factor) | | return self.__mul__(multiplier) |
| | | |
| 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.stuffing, self.volume / divisor) | n | return Sausage(self.filling, self.size / divisor) |
| raise ValueError('Division divisor must be a positive integer.') | | raise ValueError('Divisor must be a positive integer.') |
| | | |
| def __bool__(self): | | def __bool__(self): |
t | return self.volume != 0 | t | return self.size != 0 |