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