Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample solution for demo #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

class Clothing:
def __init__(self, condition=0):
self.category = "Clothing"
self.condition = condition

def __str__(self):
return "The finest clothing you could wear."

def __repr__(self):
return f"{self.category} in {self.condition} condition"
8 changes: 8 additions & 0 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Decor:
def __init__(self, condition = 0):
self.category = "Decor"
self.condition = condition

def __str__(self):
return "Something to decorate your space."

7 changes: 7 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Electronics:
def __init__(self, condition = 0):
self.category = "Electronics"
self.condition = condition

def __str__(self):
return "A gadget full of buttons and secrets."
8 changes: 8 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class Item:
def __init__(self, category=""):
self.category = category


def __str__(self):
return "Hello World!"
58 changes: 58 additions & 0 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Vendor:

def __init__(self, inventory=[]):
self.inventory = inventory

def add(self, item):
self.inventory.append(item)
return item

def remove(self, item):
try:
self.inventory.remove(item)
return item
except ValueError as ve:
return False

def get_by_category(self, category):
return [item for item in self.inventory if item.category == category]

def swap_first_item(self, other_vendor):
if len(self.inventory) == 0 or len(other_vendor.inventory) == 0:
return False

my_first = self.inventory[0]
other_first = other_vendor.inventory[0]
self.remove(my_first)
other_vendor.remove(other_first)
self.add(other_first)
other_vendor.add(my_first)
return True

def get_best_by_category(self, category):
inv_by_category = [item for item in self.inventory if item.category == category]

if len(inv_by_category) == 0:
return None

print(inv_by_category)

max = inv_by_category[0]
for item in inv_by_category:
if item.condition > max.condition:
max = item

return max

def swap_best_by_category(self, other: 'Vendor', my_priority: str, their_priority: str):
my_best = self.get_best_by_category(their_priority)
their_best = other.get_best_by_category(my_priority)

if my_best is None or their_best is None:
return False

self.remove(my_best)
other.remove(their_best)
self.add(their_best)
other.add(my_best)
return True
Empty file modified test.sh
100644 → 100755
Empty file.