-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Non-pythonic code #3
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
Comments
Cant agree more, even from a newbie like me ,still can tell these code can write much better. |
We all are learners, if you think something is wrong please send a pull request and make this more efficient. |
Agree with @dynamitechetan |
Yeah exactly you can think of it as an Open Source book |
I made selection sort more pythonic, please merge it #5 Later I can make more but now I haven't got enough time for it |
@dynamitechetan, no offence, but I think there is something wrong in every line in this repository. Somehow this repository ended up in my favorite weekly newsletter, so I'm assuming this was posted somewhere as a resource for Python novices. Unfortunately, this "open source book" isn't ready to be published and teaches many bad habits. Looking at binsearch, flake8 output:
It looks like there is a warning every second line, including those empty. It's not good.
Using this, we can easily write another function, which returns
Voilà, done. Side-effects free, clean, concise and pythonic. Add some docstrings, examples of usage, Big difference, IMO. |
@miedzinski I will try to fix it asap. |
@dynamitechetan, Pycoder's Weekly, issue #227. |
Made binary_search algo usual way. Also binary search method used stdlib added (suggested to @miedzinski comment) - |
@SergeyTsaplin Merged! |
@SergeyTsaplin, much better. But you introduced one, very bad, thing to with new implementation. Your code is |
Guys, don't name files such way please:
First example is absolutely amateurish. Second - is not pythonic |
@SergeyTsaplin Thank you for suggestion. Actually it's my mistake, and I just solved it. |
@SergeyTsaplin I've included test cases in all cipher scripts and this tests are running well locally as shown in below image: But, when I commit and push the same, Travis says that build failed. |
Read the build log. It's explained there. I am closing this. Clearly some changes to code were made. |
Consider a word, and a copy of that word, but with the last letter repeating twice. (e.g., ["ABC", "ABCC"]) When adding the second word's last letter, it only compares the previous word's prefix—the last letter of the word already in the Radix Tree: 'C'—and the letter to be added—the last letter of the word we're currently adding: 'C'. So it wrongly passes the "Case 1" check, marks the current node as a leaf node when it already was, then returns when there's still one more letter to add. The issue arises because `prefix` includes the letter of the node itself. (e.g., `nodes: {'C' : RadixNode()}, is_leaf: True, prefix: 'C'`) It can be easily fixed by simply adding the `is_leaf` check, asking if there are more letters to be added. - Test Case: `"A AA AAA AAAA"` - Fixed correct output: ``` Words: ['A', 'AA', 'AAA', 'AAAA'] Tree: - A (leaf) -- A (leaf) --- A (leaf) ---- A (leaf) ``` - Current incorrect output: ``` Words: ['A', 'AA', 'AAA', 'AAAA'] Tree: - A (leaf) -- AA (leaf) --- A (leaf) ``` *N.B.* This passed test cases for [Croatian Open Competition in Informatics 2012/2013 Contest TheAlgorithms#3 Task 5 HERKABE](https://hsin.hr/coci/archive/2012_2013/)
* Fix insertion fail in ["*X", "*XX"] cases Consider a word, and a copy of that word, but with the last letter repeating twice. (e.g., ["ABC", "ABCC"]) When adding the second word's last letter, it only compares the previous word's prefix—the last letter of the word already in the Radix Tree: 'C'—and the letter to be added—the last letter of the word we're currently adding: 'C'. So it wrongly passes the "Case 1" check, marks the current node as a leaf node when it already was, then returns when there's still one more letter to add. The issue arises because `prefix` includes the letter of the node itself. (e.g., `nodes: {'C' : RadixNode()}, is_leaf: True, prefix: 'C'`) It can be easily fixed by simply adding the `is_leaf` check, asking if there are more letters to be added. - Test Case: `"A AA AAA AAAA"` - Fixed correct output: ``` Words: ['A', 'AA', 'AAA', 'AAAA'] Tree: - A (leaf) -- A (leaf) --- A (leaf) ---- A (leaf) ``` - Current incorrect output: ``` Words: ['A', 'AA', 'AAA', 'AAAA'] Tree: - A (leaf) -- AA (leaf) --- A (leaf) ``` *N.B.* This passed test cases for [Croatian Open Competition in Informatics 2012/2013 Contest #3 Task 5 HERKABE](https://hsin.hr/coci/archive/2012_2013/) * Add a doctest for previous fix * improve doctest readability
Code shown in this repository is mostly non-pythonic. It doesn't follow any conventions from PEP8 and all algorithms have a lot of anti-patterns. For example, binary search as shown in Python docs -- https://docs.python.org/3/library/bisect.html#searching-sorted-lists.
I don't know the exact purpose of this repository, but it certainly doesn't show well-implemented basic algorithms in Python.
The text was updated successfully, but these errors were encountered: