f | from fractions import Fraction | f | from fractions import Fraction |
| | | |
| class Sausage: | | class Sausage: |
| | | |
n | def __init__(self, ground='pork!', volume=1): | n | def __init__(self, stuffing='pork!', volume=1): |
| self.ground = ground | | self.stuffing = stuffing |
| self.volume = Fraction(volume) | | self.volume = Fraction(volume) |
| | | |
| def __abs__(self): | | def __abs__(self): |
| return self.volume | | return self.volume |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
n | return Sausage(self.ground, self.volume + other.volume) | n | return Sausage(self.stuffing, self.volume + other.volume) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
n | return Sausage(self.ground, max(self.volume - other.volume, 0)) | n | return Sausage(self.stuffing, max(self.volume - other.volume, 0) |
| | | ) |
| | | |
| def __mul__(self, factor): | | def __mul__(self, factor): |
n | return Sausage(self.ground, self.volume * factor) | n | return Sausage(self.stuffing, self.volume * factor) |
| | | |
| def __rmul__(self, factor): | | def __rmul__(self, factor): |
| return self * factor | | return self * factor |
| | | |
| def __truediv__(self, divisor): | | def __truediv__(self, divisor): |
n | return Sausage(self.ground, self.volume / divisor) | n | return Sausage(self.stuffing, self.volume / divisor) |
| | | |
| def __bool__(self): | | def __bool__(self): |
| return self.volume > 0 | | return self.volume > 0 |
| | | |
| def __str__(self): | | def __str__(self): |
| if self.volume == 0: | | if self.volume == 0: |
| return '/|\n||\n||\n||\n\\|' | | return '/|\n||\n||\n||\n\\|' |
| full_loafs = int(self.volume) | | full_loafs = int(self.volume) |
n | remaining_fraction = self.volume - full_loafs | n | remainder = self.volume - full_loafs |
| top_border, ground_line, bottom_border = ('', '', '') | | str1, str2, str3 = ('', '', '') |
| for _ in range(full_loafs): | | for _ in range(full_loafs): |
n | a, b, c = self._create_full_loaf() | n | a, b, c = self._full_loaf() |
| top_border += a | | str1 += a |
| ground_line += b | | str2 += b |
| bottom_border += c | | str3 += c |
| if remaining_fraction > 0: | | if remainder > 0: |
| a, b, c = self._create_partial_loaf(remaining_fraction) | | a, b, c = self._partial_loaf(remainder) |
| top_border += a | | str1 += a |
| ground_line += b | | str2 += b |
| bottom_border += c | | str3 += c |
| return top_border + '\n' + (ground_line + '\n') * 3 + bottom_bor | | return str1 + '\n' + (str2 + '\n') * 3 + str3 |
| der | | |
| | | |
n | def _create_full_loaf(self): | n | def _full_loaf(self): |
| ground_remainder = 12 % len(self.ground) | | remain = 12 % len(self.stuffing) |
| ground_main = 12 // len(self.ground) | | main = 12 // len(self.stuffing) |
| top_border = '/------------\\' | | border1 = '/------------\\' |
| ground_line = f'|{self.ground * ground_main + self.ground[:groun | | filling = f'|{self.stuffing * main + self.stuffing[:remain]}|' |
| d_remainder]}|' | | |
| bottom_border = '\\------------/' | | border2 = '\\------------/' |
| return (top_border, ground_line, bottom_border) | | return (border1, filling, border2) |
| | | |
t | def _create_partial_loaf(self, fraction): | t | def _partial_loaf(self, fraction): |
| total_ground_length = fraction.numerator * (12 // fraction.denom | | len_of_remain = fraction.numerator * (12 // fraction.denominator |
| inator) | | ) |
| ground_count = total_ground_length // len(self.ground) | | count_of_word = len_of_remain // len(self.stuffing) |
| leftover_ground_length = total_ground_length - ground_count * le | | len_of_rest_word = len_of_remain - count_of_word * len(self.stuf |
| n(self.ground) | | fing) |
| top_border = '/' + '-' * total_ground_length + '|' | | border1 = '/' + '-' * len_of_remain + '|' |
| ground_line = f'|{self.ground * ground_count + self.ground[:left | | filling = f'|{self.stuffing * count_of_word + self.stuffing[:len |
| over_ground_length]}|' | | _of_rest_word]}|' |
| bottom_border = '\\' + '-' * total_ground_length + '|' | | border2 = '\\' + '-' * len_of_remain + '|' |
| return (top_border, ground_line, bottom_border) | | return (border1, filling, border2) |