Skip to content

Update list-1.py #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

rdavid7121
Copy link
Contributor

Update about checking in len of a and b

@@ -27,7 +27,7 @@ def common_end(a, b):
element or they have the same last element. Both arrays will be length 1
or more.
"""
return a[0] == b[0] or a[-1] == b[-1]
return (len(a) and len(b))>0 and a[0] == b[0] or a[-1] == b[-1]
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for submitting a PR, I wrote these a long long time ago so I bet there's mistakes and un-pythonic idioms here and there.

The problem already states both arrays will contain one or more items, so the check added is not necessary.

More importantly, your addition works but it's not logically sound and depends on int-to-bool casting logic. When you use and on an int (what len returns), you are casting said it to a boolean (which will always be True, unless the number is 0, which won't happen in this case). So you are basically doing (True and True) > 0, which will always be true. You'd also need to put the second part of the conditions (a[0] == b[0] or a[-1] == b[-1]) in between parenthesis in order to get the and operator you added to apply to both.

For a more reasonable check, you could consider the immediate: (len(a) > 0 and len(b) > 0), or something more convoluted like min([len(a), len(b)]) > 0

So: return (len(a) > 0 and len(b) > 0) and (a[0] == b[0] or a[-1] == b[-1]) would work great if we needed to check for array length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh great explanation sir, I knew my mistakes

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.

2 participants