在一个神奇的国度Apple王国,有许多的苹果,同时有一个大胃王和一个小胃王,他们都喜欢吃苹果,大胃王只吃重量最大的苹果,小胃王只吃重量最小的苹果,为了帮助大小胃王吃掉相应的苹果,作为Apple王国的苹果管理员的你需要完成class Warehouse中的TODO来管理苹果:
时间: 2023-11-12 11:07:37 浏览: 40
好的,让我看一下要完成什么任务。
首先,我们需要定义一个苹果类Apple,包括苹果的属性:重量weight和颜色color。
```python
class Apple:
def __init__(self, weight, color):
self.weight = weight
self.color = color
```
接下来,我们需要完成Warehouse类中的TODO。首先是添加苹果到仓库中。
```python
class Warehouse:
def __init__(self):
self.apples = []
def add_apple(self, apple):
self.apples.append(apple)
```
然后是为大胃王和小胃王分别找到符合他们要求的苹果。
```python
class Warehouse:
def __init__(self):
self.apples = []
def add_apple(self, apple):
self.apples.append(apple)
def find_heaviest_apple(self):
if not self.apples:
return None
heaviest_apple = self.apples[0]
for apple in self.apples:
if apple.weight > heaviest_apple.weight:
heaviest_apple = apple
return heaviest_apple
def find_lightest_apple(self):
if not self.apples:
return None
lightest_apple = self.apples[0]
for apple in self.apples:
if apple.weight < lightest_apple.weight:
lightest_apple = apple
return lightest_apple
```
现在Warehouse类已经可以管理苹果并为大小胃王找到符合他们要求的苹果了。