-
Notifications
You must be signed in to change notification settings - Fork 71
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
Scissors - Stella #63
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from swap_meet.item import Item | ||
|
||
#Wave 5 | ||
class Clothing(Item): | ||
def __init__(self,condition = 0, age =0): | ||
super().__init__("Clothing", condition,age) | ||
|
||
|
||
def __str__(self): | ||
return "The finest clothing you could wear." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from swap_meet.item import Item | ||
|
||
#Wave 5: | ||
class Decor(Item): | ||
def __init__(self,condition = 0, age =0): | ||
super().__init__("Decor", condition, age) | ||
|
||
def __str__(self): | ||
return "Something to decorate your space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from swap_meet.item import Item | ||
|
||
#Wave 5: | ||
class Electronics(Item): | ||
def __init__(self,condition=0, age = 0): | ||
super().__init__("Electronics",condition, age) | ||
|
||
def __str__(self): | ||
return "A gadget full of buttons and secrets." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#Wave 2: | ||
class Item: | ||
|
||
def __init__(self, category = "", condition = 0, age =0): | ||
self.category= category | ||
self.condition = condition | ||
self.age = age | ||
|
||
#Wave 3: | ||
def __str__(self): | ||
return "Hello World!" | ||
|
||
#Wave 5: | ||
def condition_description(self): | ||
condition_note = "" | ||
if self.condition >=0 and self.condition <= 2: | ||
condition_note = "oh no! please throw" | ||
elif self.condition >2 and self.condition <= 3.5: | ||
condition_note = "used for a bit but can keep going..." | ||
elif self.condition >3.5 and self.condition <= 5: | ||
condition_note = "fresh and new!" | ||
return condition_note |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||||||||||||||||||
from swap_meet.item import Item | ||||||||||||||||||||||
|
||||||||||||||||||||||
#Wave 1 | ||||||||||||||||||||||
class Vendor: | ||||||||||||||||||||||
def __init__(self, inventory = None): | ||||||||||||||||||||||
if inventory == None: | ||||||||||||||||||||||
self.inventory = [] | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
self.inventory = inventory | ||||||||||||||||||||||
|
||||||||||||||||||||||
def add(self,item): | ||||||||||||||||||||||
self.inventory.append(item) | ||||||||||||||||||||||
return item | ||||||||||||||||||||||
|
||||||||||||||||||||||
def remove(self,item): | ||||||||||||||||||||||
if item in self.inventory: | ||||||||||||||||||||||
self.inventory.remove(item) | ||||||||||||||||||||||
return item | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
Comment on lines
+16
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the pattern where you check the edge case first:
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
#Wave 2 | ||||||||||||||||||||||
def get_by_category(self,category): | ||||||||||||||||||||||
category_list = [] | ||||||||||||||||||||||
|
||||||||||||||||||||||
for item in self.inventory: | ||||||||||||||||||||||
if item.category == category: | ||||||||||||||||||||||
category_list.append(item) | ||||||||||||||||||||||
return category_list | ||||||||||||||||||||||
|
||||||||||||||||||||||
#Wave 3: | ||||||||||||||||||||||
def swap_items(self,friend, my_item, their_item): | ||||||||||||||||||||||
#if len(self.inventory) == 0 or len(friend.inventory) ==0: | ||||||||||||||||||||||
#return False | ||||||||||||||||||||||
if my_item in self.inventory and their_item in friend.inventory: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider refactoring so you check the condition where you return |
||||||||||||||||||||||
self.inventory.remove(my_item) | ||||||||||||||||||||||
friend.inventory.append(my_item) | ||||||||||||||||||||||
friend.inventory.remove(their_item) | ||||||||||||||||||||||
self.inventory.append(their_item) | ||||||||||||||||||||||
return True | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
|
||||||||||||||||||||||
#Wave 4 | ||||||||||||||||||||||
def swap_first_item(self,friend): | ||||||||||||||||||||||
if len(self.inventory) == 0 or len(friend.inventory) ==0: | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
return self.swap_items(friend,self.inventory[0],friend.inventory[0]) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can move this line our of the |
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
#Wave 6 | ||||||||||||||||||||||
def get_best_by_category(self,category): | ||||||||||||||||||||||
best_item = None | ||||||||||||||||||||||
best_condition = 0 | ||||||||||||||||||||||
category_items = self.get_by_category(category) | ||||||||||||||||||||||
if len(category_items) != 0: | ||||||||||||||||||||||
for item in category_items: | ||||||||||||||||||||||
if item.condition > best_condition: | ||||||||||||||||||||||
best_item = item | ||||||||||||||||||||||
best_condition = item.condition | ||||||||||||||||||||||
return best_item | ||||||||||||||||||||||
|
||||||||||||||||||||||
def swap_best_by_category(self,other,my_priority,their_priority): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow! So efficient. Good use of the previous methods as helper functions. |
||||||||||||||||||||||
their_offer = other.get_best_by_category(my_priority) | ||||||||||||||||||||||
my_offer = self.get_best_by_category(their_priority) | ||||||||||||||||||||||
swap_result = self.swap_items(other,my_offer,their_offer) | ||||||||||||||||||||||
return swap_result | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
#Wave 7 - Optional Enhancement | ||||||||||||||||||||||
#Helper function - get item with the min age | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great work implementing this optional part. |
||||||||||||||||||||||
def get_min_age(self): | ||||||||||||||||||||||
newest_item = None | ||||||||||||||||||||||
min_age = 100 | ||||||||||||||||||||||
|
||||||||||||||||||||||
for item in self.inventory: | ||||||||||||||||||||||
if item.age < min_age: | ||||||||||||||||||||||
newest_item = item | ||||||||||||||||||||||
min_age = item.age | ||||||||||||||||||||||
return newest_item | ||||||||||||||||||||||
|
||||||||||||||||||||||
def swap_by_newest(self,other): | ||||||||||||||||||||||
my_newest = self.get_min_age() | ||||||||||||||||||||||
their_newest = other.get_min_age() | ||||||||||||||||||||||
return self.swap_items(other,my_newest,their_newest) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
''' | ||||||||||||||||||||||
#Swap the newest items | ||||||||||||||||||||||
def swap_by_newest(self,other,my_item,their_item): | ||||||||||||||||||||||
my_newest = self.get_min_age(their_item) | ||||||||||||||||||||||
their_newest = other.get_min_age(my_item) | ||||||||||||||||||||||
return self.swap_items(other,my_newest,their_newest) | ||||||||||||||||||||||
''' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
from swap_meet.vendor import Vendor | ||
from swap_meet.item import Item | ||
from swap_meet.clothing import Clothing | ||
from swap_meet.decor import Decor | ||
from swap_meet.electronics import Electronics | ||
|
||
|
||
def test_get_min_age(): | ||
item_a = Clothing(age = 1) | ||
item_b = Clothing(age = 4) | ||
item_c = Decor(age = 2) | ||
item_d = Decor(age = 3.5) | ||
tai = Vendor(inventory = [item_a,item_b,item_c, item_d]) | ||
newest_item = tai.get_min_age() | ||
|
||
assert newest_item.age == 1 | ||
|
||
def test_swap_by_newest(): | ||
item_a = Item(age=2) | ||
item_b = Item(age=4) | ||
tai = Vendor(inventory=[item_a, item_b]) | ||
|
||
item_d = Item(age=2) | ||
item_e = Item(age=4) | ||
jesse = Vendor(inventory=[item_d, item_e]) | ||
result = tai.swap_by_newest(jesse) | ||
|
||
assert result is True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good work writing your own tests! It's good practice to add a few more assertions here to make sure that the swap actually happened. Check that |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of super/inheritance.