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

feature(C08_P06): added oops solution #225

Closed
wants to merge 8 commits into from

Conversation

HOD101s
Copy link
Contributor

@HOD101s HOD101s commented Apr 15, 2022

Added a more structured oops based approach. No modification in logic.

@HOD101s
Copy link
Contributor Author

HOD101s commented Apr 17, 2022

https://github.com/careercup/CtCI-6th-Edition-Python/runs/6045332210?check_suite_focus=true
Error: ./chapter_08/p06_towers_of_hanoi.py:40:28: N805 first argument of a method should be named 'self'
The first catch here for not having self in the decorator function shouldn't be a problem. Any ideas on how we can circumvent this check?

Copy link
Collaborator

@brycedrennan brycedrennan left a comment

Choose a reason for hiding this comment

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

Great work. Love the direction you're heading in. I've added some feedback you might find useful.

Also if you rebase off the master branch I fixed the autoformatter issue.

@brycedrennan
Copy link
Collaborator

Also to directly answer your question, the decorator might work if you add @staticmethod to it. not sure.

@HOD101s
Copy link
Contributor Author

HOD101s commented May 3, 2022

Also to directly answer your question, the decorator might work if you add @staticmethod to it. not sure.

Tried this. Was getting some errors. Went ahead with your examples (Option B).

Thanks for the detailed review. Really learnt a lot here.

@brycedrennan
Copy link
Collaborator

I see these extra commits got added. Probably as you attempted to rebase, which can be tricky to get right. I'll just delete them when this gets merged.

Copy link
Collaborator

@brycedrennan brycedrennan left a comment

Choose a reason for hiding this comment

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

Great work. Here is a little more feedback

def pop(self):
if len(self._stack):
return self._stack.pop()
raise Exception("Stack Empty")
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you just

def pop(self):
    return self._stack.pop()

then it would return a more properly typed error message. If it was me I'd just leave that default error (IndexError).

If you do want to return your own custom error then I'd suggest following the EAFP coding style that is preferred in python.

def pop(self):
    try:
        return self._stack.pop()
    except IndexError:
        raise IndexError("pop from an empty stack")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it. adding this pattern for the Stack class exceptions


def push(self, val):
if len(self._stack) == self.stack_size:
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Somewhat subjective but since pop throws an exception if the stack is too small it feels like this should throw an exception if the stack is too big.

class StackTooBigError(Exception):
    pass

def push(self, val):
        if len(self._stack) == self.stack_size:
            raise StackTooBigError("stack has reached max size")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Implemented.


def get_stack(self, stack_num):
if 0 > stack_num >= self.num_stacks:
raise Exception("stack_num invalid")
Copy link
Collaborator

Choose a reason for hiding this comment

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

In general one should avoid raising generic Exceptions. IndexError would probably be a better fit here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Implemented.

return self._stack[-1]
raise Exception("Stack Empty")

def is_empty(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

is_empty is not used so I'd suggest deleting it.

raise Exception("Stack Empty")

def top(self):
if len(self._stack):
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment as previous.

@HOD101s
Copy link
Contributor Author

HOD101s commented May 7, 2022

Addressed all the above comments. Thanks again for the detailed review!

Edit: Yes those additional commits came in because of the master rebase.

@brycedrennan
Copy link
Collaborator

I merged your work but forgot to run the linter beforehand. I'll fix those now

@brycedrennan
Copy link
Collaborator

Your work and the items the linter found can be found here: 4334c6d
The fixes are found here: c94e337

Thanks for contributing!

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.

4 participants