Phoenix_C22_Beenish_Alejandra#22
Conversation
| my_item_exists = self.get_by_id(my_item.id) | ||
| thier_item_exists = other_vendor.get_by_id(thier_item.id) |
There was a problem hiding this comment.
So now I understand that we don't need these two lines and is using up time that we don't need.
Instead in line 66 we could have made our if statement:
if my_item in self.inventory and their_ item in other_vendor.inventory:
There was a problem hiding this comment.
We could operate directly on the two inventories, but there's no significant time penalty to your having used the helper function instead. Either approach is O(n).
|
So I just realized that we misspelled all instances of their (we spelled it thier). It doesn't affect the code but it's not correct English! Sorry! |
| return f"An object of type {self.get_category()} with id {self.id}." | ||
|
|
||
| def condition_description(self): | ||
| if self.condition == 5: |
There was a problem hiding this comment.
for big 0 -- we should have stored the condition and descriptions in a dictionary. Right now it is having to go through and check is self.condition the same as 5, 4, 3, 2, 1, 0 (making it o(n)) but if it was in a dictionary it could just look it up and it would be o(1).
If I am understanding this correctly.
There was a problem hiding this comment.
Nice analysis of this. On the one hand, we might say that this is just hardcoded logic and is constant time, but you're exactly right that in the general case, there could be an arbitrary number of conditions, and we would have to write more code to cover those conditions, increasing the number of instructions. In this case, the fact that we had 5 conditions is really an arbitrary limit, and the general case would be to have n conditions.
This same argument can apply to the space complexity. More code is required to handle more cases, leading to linear space complexity (for the general case, even though this specific case looks constant). If we move the data to a dictionary or list, the size of the code would remain constant as we add more conditions, but the size of the data structure would grow.
To get the maximum benefit for the time complexity, we would want to declare the data structure outside the function, so that it only gets initialized a single time, rather than every time the function is called.
There was a problem hiding this comment.
While we don't have a test that tries to get the condition description for a floating point condition, we do check that we can make an Item with floating point conditions. What would this logic do if we tried to get the description for an Item having a condition of 3.5?
I use the VS Code extension Code Spell Checker to help with this. It understands that snake_case or camelCase has multiple words in a single name, so it even spellchecks parts of names. |
anselrognlie
left a comment
There was a problem hiding this comment.
Looks good! Please review my comments, and let me know if you have any questions via Slack, our next 1:1, or office hours. Nice job!
| - If there is no matching item in the `inventory`, the method should explicitly return `None` | ||
|
|
||
| ### Wave 3 | ||
| ### Wave 3d |
There was a problem hiding this comment.
Keep an eye out for unintentional changes to files. Remember to run git status before git add . and undo any changes we might have accidentally made.
| return f"An object of type {self.get_category()} with id {self.id}." | ||
|
|
||
| def condition_description(self): | ||
| if self.condition == 5: |
There was a problem hiding this comment.
Nice analysis of this. On the one hand, we might say that this is just hardcoded logic and is constant time, but you're exactly right that in the general case, there could be an arbitrary number of conditions, and we would have to write more code to cover those conditions, increasing the number of instructions. In this case, the fact that we had 5 conditions is really an arbitrary limit, and the general case would be to have n conditions.
This same argument can apply to the space complexity. More code is required to handle more cases, leading to linear space complexity (for the general case, even though this specific case looks constant). If we move the data to a dictionary or list, the size of the code would remain constant as we add more conditions, but the size of the data structure would grow.
To get the maximum benefit for the time complexity, we would want to declare the data structure outside the function, so that it only gets initialized a single time, rather than every time the function is called.
| my_item_exists = self.get_by_id(my_item.id) | ||
| thier_item_exists = other_vendor.get_by_id(thier_item.id) |
There was a problem hiding this comment.
We could operate directly on the two inventories, but there's no significant time penalty to your having used the helper function instead. Either approach is O(n).
There was a problem hiding this comment.
Nice to see how you were trying out cases while working, though usually we wouldn't check in a file like this. A file like this would usually reside outside of the package, and we would import modules from within the package. So I'd expect to see this in the project root folder, rather than in the swap_meet folder.
To prevent sample code like this from being checked in, we can add its path to the .gitignore file, which will make this file invisible to git.
| # ********************************************************************* | ||
| # ****** Complete Assert Portion of this test ********** | ||
| # ********************************************************************* | ||
| assert result == False No newline at end of file |
There was a problem hiding this comment.
👍 This is the expected result, but we might also include checks to make sure that the vendor's inventory wasn't modified.
| return best_in_category | ||
| # another implementation | ||
| # try: | ||
| # return max(items_in_category, key=lambda item: item.condition) |
There was a problem hiding this comment.
Rather than doing this with expection logic, the max function also accepts a default value to return if the collection is empty, so we could have max return None for us.
| def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
| best_item_my_priority = other_vendor.get_best_by_category(my_priority) | ||
| best_item_their_priority = self.get_best_by_category(their_priority) | ||
| if best_item_my_priority and best_item_their_priority: |
There was a problem hiding this comment.
If our swap function did some of the safety checks internally, we might not even need this check here, since None wouldn't be found as a value in either list.
| if best_item_my_priority and best_item_their_priority: | ||
| return self.swap_items_helper(other_vendor, best_item_their_priority, best_item_my_priority) | ||
| else: | ||
| return False |
There was a problem hiding this comment.
If we keep this check, consider rewriting it like this to emphasize the main logic.
if not best_item_my_priority or not best_item_their_priority:
return False
return self.swap_items_helper(other_vendor, best_item_their_priority, best_item_my_priority)|
|
||
| def swap_by_newest(self, other_vendor): | ||
| try: | ||
| my_newest_item = min(self.inventory, key=lambda item: item.age) |
There was a problem hiding this comment.
Consider making a helper get_by_newest to avoid needing to repeat the min expression here.
| return False | ||
| return self.swap_items_helper(other_vendor, my_first_item, thier_first_item) | ||
|
|
||
| def swap_by_newest(self, other_vendor): |
There was a problem hiding this comment.
👍 Nice implementation of your custom swap_by_newest.
No description provided.