| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
I don't think these qualify as magic values, and I think we should remove all reference to it. There are better places/exercises to show the dangers of magic values to a student. We can leave them off here.
You tell me 😄 Have you run timeit or another benchmarking tool against both solutions with a good variance of test data (NOT the same input over and over)? If you haven't, you should not assert that one approach is faster than another. You can take a look at some of the approaches that include performance articles to see what others have done ... but also be careful to make sure you have good test data. Ideally, you'd use various combinations of what the tests use, or other combinations that call out where, for example indexing would be slow or str conversion would be slow.
I haven't dug into all the details yet, but will make suggestions when I do.
Simpler doesn't mean equivalent, as you can see from Isaacs comments above. 🙂 However, he is incorrect that string conversion in general is fundamentally flawed. It's not the string conversion that is a problem here -- its how the string representation is made. Simply doing str(list_one) won't accurately represent empty lists. str(list_one).strip("[]") + ",", fails to represent nested single and double quotes. However ','.join(str(item) for item in list_one) does accurately represent both. There is another problem with the stated string solution. It assumes that if something is not a SUBLIST, it is automagically a SUPERLIST. But that's not the case. So you have to check each scenario separately: def sublist(list_one, list_two):
first = ','.join(str(item) for item in list_one)
second = ','.join(str(item) for item in list_two)
if list_one == list_two:
return EQUAL
if len(list_one) < len(list_two):
if first in second:
return SUBLIST
else:
if second in first:
return SUPERLIST
return UNEQUALThe code above passes all the tests on the site as well as the two additional tests Isaac notes above. You can walk through this solution as well as my simplified one on Python Tutor Now, there may be more corner cases that then fail - and I think we should note that that could be the case, and caution that using this methodology could be brittle -- but I think we should include the edited version as a solution that does pass all the tests (plus a few extra). |
Sorry, something went wrong.
There was a problem hiding this comment.
@safwansamsudeen -- Please see comments above. I want the string approach included (with edits) as valid. Thanks.
Sorry, something went wrong.
|
Any string encoding has the issue that you can encode one list and add that as an element to the other list. Using ','.join(str(item) for item in list_x), >>> sublist(["a", "b"],["a,b"])
'SUPERLIST'More generally, if you have: first = str_encode(list_one)
second = str_encode(list_two)
if second in first:
return SUPERLISTThen you can do sublist(["a", str_encode(list), "b"], list) and get back SUPERLIST. Another issue with ",".join(str(i) for i in list) is that it ignores types, so sublist([1, 2], ["1"]) will return SUPERLIST. |
Sorry, something went wrong.
|
Why was this merged? Should I open another PR with the suggested changes - or is the string based approach wrong as Isaac says? |
Sorry, something went wrong.
|
@safwansamsudeen -- I've given up arguing. We can leave it as wrong. It doesn't generalize to all possibilities, so we can let Isaac have this one. If you think there should be more additions/changes you can submit a new PR. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Few points on my this:
Hope I haven't made any formatting issues this time, and thanks!