hw05: Backpack packing
- Due before the beginning of class on Wednesday, January 21
The scenario
Some travellers are going for an adventure and they will each bring a backpack. They want to add items to their backpacks, each of which has a name, a weight, and a value which indicates how rare it is. Each backpack also has a maximum weight that it can hold.
The task
You are given an Item class in a file item.py, and you should
not change how it’s written. You need to write the Backpack class
in the file backpack.py so that code like the following will work:
from item import Item
from backpack import Backpack
big_pack = Backpack(20.0)
small_pack = Backpack(3.0)
sword = Item("Iron Sword", 5.5, 60)
spells = Item("Book of Spells", 1.5, 90)
potion = Item("Health Potion", 0.5, 10)
idol = Item("Golden Idol", 12.0, 95)
gem = Item("Precious Gem", 1.4, 99)
big_pack.add(sword) # adding [Rare] Iron Sword (5.5kg)
small_pack.add(potion) # adding [Common] Health Potion (0.5kg)
small_pack.add(sword) # OVER CAPACITY
big_pack.add(idol) # adding [Legendary] Golden Idol (12.0kg)
big_pack.add(spells) # adding [Legendary] Book of Spells (1.5kg)
small_pack.add(gem) # adding [Legendary] Precious Gem (1.4kg)
print(f"Small pack weight: {small_pack.total_weight()}")
print(f"Big pack weight: {big_pack.total_weight()}")
print("Big pack legendary items:")
bigleg = big_pack.get_by_rarity("Legendary")
for item in bigleg:
print(item)
If your Backpack class is implemented correctly, then running this
code will produce the following output:
adding [Rare] Iron Sword (5.5kg)
adding [Common] Health Potion (0.5kg)
OVER CAPACITY
adding [Legendary] Golden Idol (12.0kg)
adding [Legendary] Book of Spells (1.5kg)
adding [Legendary] Precious Gem (1.4kg)
Small pack weight: 1.9
Big pack weight: 19.0
Big pack legendary items:
[Legendary] Golden Idol (12.0kg)
[Legendary] Book of Spells (1.5kg)
Specifically, your Backpack class needs to have:
A constructor that sets up an empty backpack with the given weight capacity
A method
add(item)to put something in the backpack. It should either print out a line that it is adding the item, or an error messageOVER CAPACITYif the item weighs too much to fit with the other things in the backpack.A method
total_weight()to return the combined weight of what items have been successfully added to this backpack.A method
get_by_rarity(rarity)that returns a list of items with the given rarity value (either “Common”, “Rare”, or “Legendary”).
The code
Go to your sd212 directory and run
git pull
You should see a new folder created for this lab, with the files
item.py (containing the Item class), packit.py (the test program
you see above), and backpack.py. You need to fill in backpack.py
with your Backpack class and submit that.
If something goes weird with the git command, you should contact your instructor, but as a temporary work-around, you can always download the files directly from github here: https://github.com/sd212usna/sd212-folder
Submit command
To submit files for this homework, run one of these commands:
submit -c=sd212 -p=hw05 backpack.py
club -csd212 -phw05 backpack.py