Skip to content

Phoenix_C22_Beenish_Alejandra#22

Open
beenishali693 wants to merge 17 commits into
Ada-C22:mainfrom
beenishali693:main
Open

Phoenix_C22_Beenish_Alejandra#22
beenishali693 wants to merge 17 commits into
Ada-C22:mainfrom
beenishali693:main

Conversation

@beenishali693

Copy link
Copy Markdown

No description provided.

Comment thread swap_meet/vendor.py
Comment on lines +64 to +65
my_item_exists = self.get_by_id(my_item.id)
thier_item_exists = other_vendor.get_by_id(thier_item.id)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@beenishali693

Copy link
Copy Markdown
Author

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!

Comment thread swap_meet/item.py
return f"An object of type {self.get_category()} with id {self.id}."

def condition_description(self):
if self.condition == 5:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@anselrognlie

Copy link
Copy Markdown
Collaborator

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!

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 anselrognlie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread README.md
- If there is no matching item in the `inventory`, the method should explicitly return `None`

### Wave 3
### Wave 3d

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread swap_meet/item.py
return f"An object of type {self.get_category()} with id {self.id}."

def condition_description(self):
if self.condition == 5:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread swap_meet/vendor.py
Comment on lines +64 to +65
my_item_exists = self.get_by_id(my_item.id)
thier_item_exists = other_vendor.get_by_id(thier_item.id)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread swap_meet/trying.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This is the expected result, but we might also include checks to make sure that the vendor's inventory wasn't modified.

Comment thread swap_meet/vendor.py
return best_in_category
# another implementation
# try:
# return max(items_in_category, key=lambda item: item.condition)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread swap_meet/vendor.py
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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread swap_meet/vendor.py
Comment on lines +51 to +54
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread swap_meet/vendor.py

def swap_by_newest(self, other_vendor):
try:
my_newest_item = min(self.inventory, key=lambda item: item.age)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making a helper get_by_newest to avoid needing to repeat the min expression here.

Comment thread swap_meet/vendor.py
return False
return self.swap_items_helper(other_vendor, my_first_item, thier_first_item)

def swap_by_newest(self, other_vendor):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice implementation of your custom swap_by_newest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants