f | from fractions import Fraction | f | from fractions import Fraction |
| from math import floor | | from math import floor |
| | | |
| class Sausage: | | class Sausage: |
n | FULL_UNIT = 1 | n | FULL_SIZE = 1 |
| SLICE_LENGTH = 12 | | SLICE_SIZE = 12 |
| | | |
| def __init__(self, stuffing='pork!', volume='1'): | | def __init__(self, stuffing='pork!', volume='1'): |
| self.stuffing = str(stuffing) | | self.stuffing = str(stuffing) |
| self.volume = Fraction(volume) if volume else Fraction(0) | | self.volume = Fraction(volume) if volume else Fraction(0) |
| if self.volume < 0: | | if self.volume < 0: |
| self.volume = Fraction(0) | | self.volume = Fraction(0) |
| | | |
| def __repr__(self): | | def __repr__(self): |
n | if self.volume * self.SLICE_LENGTH < 1: | n | if self.volume * self.SLICE_SIZE < 1: |
| return '/|\n||\n||\n||\n\\|' | | return '/|\n||\n||\n||\n\\|' |
n | full_sausages_count = floor(self.volume) | n | full_sausages = floor(self.volume) |
| remaining_sausage_length = int(self.volume % self.FULL_UNIT * se | | partial_sausage_length = int(self.volume % self.FULL_SIZE * self |
| lf.SLICE_LENGTH) | | .SLICE_SIZE) |
| sausage_parts = [] | | sausage_parts = [] |
n | for _ in range(full_sausages_count): | n | for _ in range(full_sausages): |
| sausage_parts.append(self._create_full_baton()) | | sausage_parts.append(self._create_full_sausage()) |
| if remaining_sausage_length > 0: | | if partial_sausage_length > 0: |
| sausage_parts.append(self._create_partial_baton(remaining_sa | | sausage_parts.append(self._create_partial_sausage(partial_sa |
| usage_length)) | | usage_length)) |
| return self._combine_parts(sausage_parts) | | return self._combine_sausages(sausage_parts) |
| | | |
n | def _create_full_baton(self): | n | def _create_full_sausage(self): |
| stuffing_repeat = (self.stuffing * (self.SLICE_LENGTH // len(sel | | stuff = (self.stuffing * (self.SLICE_SIZE // len(self.stuffing) |
| f.stuffing) + 1))[:self.SLICE_LENGTH] | | + 1))[:self.SLICE_SIZE] |
| return f'/------------\\\n|{stuffing_repeat}|\n|{stuffing_repeat | | return f'/------------\\\n|{stuff}|\n|{stuff}|\n|{stuff}|\n\\--- |
| }|\n|{stuffing_repeat}|\n\\------------/' | | ---------/' |
| | | |
n | def _create_partial_baton(self, stuffing_length): | n | def _create_partial_sausage(self, stuffing_length): |
| stuffing_repeat = (self.stuffing * (stuffing_length // len(self. | | stuff = (self.stuffing * (stuffing_length // len(self.stuffing) |
| stuffing) + 1))[:stuffing_length] | | + 1))[:stuffing_length] |
| return f'/{'-' * stuffing_length}|\n|{stuffing_repeat}|\n|{stuff | | return f'/{'-' * stuffing_length}|\n|{stuff}|\n|{stuff}|\n|{stuf |
| ing_repeat}|\n|{stuffing_repeat}|\n\\{'-' * stuffing_length}|' | | f}|\n\\{'-' * stuffing_length}|' |
| | | |
t | def _combine_parts(self, parts): | t | def _combine_sausages(self, sausages): |
| rows = [part.split('\n') for part in parts] | | rows = [sausage.split('\n') for sausage in sausages] |
| combined = [''.join(row) for row in zip(*rows)] | | combined = [''.join(row) for row in zip(*rows)] |
| return '\n'.join(combined) | | return '\n'.join(combined) |
| | | |
| def __abs__(self): | | def __abs__(self): |
| return abs(self.volume) | | return abs(self.volume) |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| new_volume = self.volume + other.volume | | new_volume = self.volume + other.volume |
| return Sausage(self.stuffing, max(new_volume, Fraction(0))) | | return Sausage(self.stuffing, max(new_volume, Fraction(0))) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
| new_volume = self.volume - other.volume | | new_volume = self.volume - other.volume |
| return Sausage(self.stuffing, max(new_volume, Fraction(0))) | | return Sausage(self.stuffing, max(new_volume, Fraction(0))) |
| | | |
| def __mul__(self, factor): | | def __mul__(self, factor): |
| if isinstance(factor, int) and factor >= 0: | | if isinstance(factor, int) and factor >= 0: |
| return Sausage(self.stuffing, self.volume * factor) | | return Sausage(self.stuffing, self.volume * factor) |
| raise ValueError('Multiplication factor must be a non-negative i | | raise ValueError('Multiplication factor must be a non-negative i |
| nteger.') | | nteger.') |
| | | |
| def __rmul__(self, factor): | | def __rmul__(self, factor): |
| return self.__mul__(factor) | | 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: |
| return Sausage(self.stuffing, self.volume / divisor) | | return Sausage(self.stuffing, self.volume / divisor) |
| raise ValueError('Division divisor must be a positive integer.') | | raise ValueError('Division divisor must be a positive integer.') |
| | | |
| def __bool__(self): | | def __bool__(self): |
| return self.volume != 0 | | return self.volume != 0 |