Created
December 13, 2021 18:22
-
-
Save vasyharan/d5d9d4fba010492daa3609707d3ce682 to your computer and use it in GitHub Desktop.
particle-drawers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a program that maintains a wardrobe where N is the number of drawers in the wardrobe and M is the capacity of each drawer. | |
# Each garment has a name, type, color, and the capacity it takes up | |
# Type of garment can be Shirt, Sweater, Shorts, Pants, or Dress | |
# Each drawer should only hold one type of garment | |
# Garments can be added to the wardrobe one at a time | |
# Once a drawer is full, an empty drawer can be filled with that same garment type | |
# Garments should be able to be removed based on color or a combination of color and type | |
from dataclasses import dataclass | |
@dataclass | |
class Garment: | |
name: str | |
garment_type: str | |
color: str | |
capacity: int | |
class Wardrobe: | |
def __init__(self, number_of_drawers: int, capacity_of_drawer: int): | |
self.number_of_drawers = number_of_drawers | |
self.capacity_of_drawer = capacity_of_drawer | |
self.drawers = [] | |
for _ in range(0, number_of_drawers): | |
self.drawers.append([]) | |
def add(self, garment): | |
for contents in self.drawers: | |
# try to re-use a drawer | |
if contents: | |
contents_type = contents[0].garment_type | |
if contents_type == garment.garment_type: | |
# check for space in drawers | |
current_use = sum([c.capacity for c in contents]) | |
if current_use + garment.capacity <= self.capacity_of_drawer: | |
# add to this drawer | |
contents.append(garment) | |
return | |
# if we are here, cannot re-use a drawer | |
for contents in self.drawers: | |
# find an empty drawer | |
if not contents: | |
contents.append(garment) | |
return | |
raise RuntimeError("Unable to add garment") | |
def remove(self, garment_color, garment_type=None): | |
garments = [] | |
for contents in self.drawers: | |
for garment in contents: | |
if garment_color == garment.color and (garment_type is None or garment.garment_type == garment_type): | |
contents.remove(garment) | |
garments.append(garment) | |
return garments | |
w = Wardrobe(3, 10) | |
print(w.remove("Green")) | |
w.add(Garment("", "Shirt", "Green", 1)) | |
w.add(Garment("", "Pants", "Green", 3)) | |
w.add(Garment("", "Dress", "Red", 2)) | |
w.add(Garment("", "Shirt", "Red", 2)) | |
w.add(Garment("", "Pants", "Black", 3)) | |
w.add(Garment("", "Pants", "White", 3)) | |
# w.add(Garment("", "Pants", "Blue", 3)) | |
# w.add(Garment("", "Shorts", "Black", 3)) | |
print(w.remove("Green")) | |
print(w.remove("Black", "Pants")) | |
print(w.remove("Black", "Pants")) | |
print(w.drawers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment