Assignment 3
Use left and right arrow keys to adjust the split region size
描述
描述
Use left and right arrow keys to adjust the split region size
multi_containers.csv
357804.pty
multi_containers.py
802289.pty
858824.pty
1
2
3
4
5
6
Name, Containers
Maxim's multicat carrybag, A carrybag for pets, A carrybag for pets, A carrybag for pets
A coles shopping cart, A coles shopping bag, A backpack, A woolworths shopping bag, A coles shopping bag
A lab coat, A small pocket, A medium pocket, A small pocket
A camping bag, A small pouch, A large pouch, A small pouch
Cargo pants, A small pocket, A medium pocket, A medium pocket, A small pocket, A medium pocket, A medium pocket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Initialised 52 items including 20 containers.
Enter the name of the container: A coles shopping cart
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
1
Enter the name of the item: Tan's Tamagotchi Support Group
Success! Item "Tan's Tamagotchi Support Group" stored in container "A coles shopping cart".
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
1
Enter the name of the item: A rock
Success! Item "A rock" stored in container "A coles shopping cart".
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
2
A coles shopping cart (total weight: 454, empty weight: 43, capacity: 0/0)
A coles shopping bag (total weight: 412, empty weight: 1, capacity: 411/1000)
Tan's Tamagotchi Support Group (weight: 410)
A rock (weight: 1)
A backpack (total weight: 40, empty weight: 40, capacity: 0/5000)
A woolworths shopping bag (total weight: 1, empty weight: 1, capacity: 0/1200)
A coles shopping bag (total weight: 1, empty weight: 1, capacity: 0/1000)
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
1
Enter the name of the item: Tan's Tamagotchi Support Group
Success! Item "Tan's Tamagotchi Support Group" stored in container "A coles shopping cart".
==================================
Enter your choice:
1. Loot item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import csv
class Item:
def __init__(self, name, weight):
self.name = name
self.weight = weight
def __str__(self):
return f"{self.name} (weight: {self.weight})"
class Container(Item):
def __init__(self, name, empty_weight, weight_capacity):
super().__init__(name, empty_weight)
self.empty_weight = empty_weight
self.weight_capacity = weight_capacity
self.current_weight = empty_weight
self.contents = []
def add_item(self, item):
if self.current_weight - self.empty_weight + item.weight <= self.weight_capacity:
self.contents.append(item)
self.current_weight += item.weight
return True
return False
def __str__(self):
return (f"{self.name} (total weight: {self.current_weight}, "
f"empty weight: {self.empty_weight}, "
f"capacity: {self.current_weight - self.empty_weight}/{self.weight_capacity})")
class MultiContainer(Container):
def __init__(self, name, compartments):
self.name = name
self.compartments = compartments
self.empty_weight = sum(comp.empty_weight for comp in compartments)
self.current_weight = self.empty_weight
def add_item(self, item):
for compartment in self.compartments:
if compartment.add_item(item):
self.current_weight += item.weight
return True
return False
def __str__(self):
result = [f"{self.name} (total weight: {self.current_weight}, empty weight: {self.empty_weight}, capacity: {self.current_weight - self.empty_weight}/0)"]
for compartment in self.compartments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Initialised 52 items including 20 containers.
Enter the name of the container: A lab coat
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
2
A lab coat (total weight: 0, empty weight: 0, capacity: 0/0)
A small pocket (total weight: 0, empty weight: 0, capacity: 0/100)
A medium pocket (total weight: 0, empty weight: 0, capacity: 0/200)
A small pocket (total weight: 0, empty weight: 0, capacity: 0/100)
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
1
Enter the name of the item: Crimpy's destroyed cat toys
Success! Item "Crimpy's destroyed cat toys" stored in container "A lab coat".
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
2
A lab coat (total weight: 27, empty weight: 0, capacity: 0/0)
A small pocket (total weight: 27, empty weight: 0, capacity: 27/100)
Crimpy's destroyed cat toys (weight: 27)
A medium pocket (total weight: 0, empty weight: 0, capacity: 0/200)
A small pocket (total weight: 0, empty weight: 0, capacity: 0/100)
==================================
Enter your choice:
1. Loot item.
2. List looted items.
0. Quit.
==================================
1
Enter the name of the item: Pierre's meme collection
Failure! Item "Pierre's meme collection" NOT stored in container "A lab coat".
==================================
Enter your choice:
1. Loot item.
1
Initialised 52 items including 20 containers.
/home/multi_containers.py 已保存
Use up and down arrow keys to adjust the split region size
运行您的代码后,控制台将在此处显示
点击这里激活终端
将文件拖放到这里上传
Workspace