Skip to content

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

Closed
miedzinski opened this issue Jul 28, 2016 · 16 comments
Closed

Non-pythonic code #3

miedzinski opened this issue Jul 28, 2016 · 16 comments
Labels
enhancement This PR modified some existing files

Comments

@miedzinski
Copy link

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.

@topaz1874
Copy link

Cant agree more, even from a newbie like me ,still can tell these code can write much better.

@dynamitechetan
Copy link
Member

We all are learners, if you think something is wrong please send a pull request and make this more efficient.

@harshildarji
Copy link
Member

Agree with @dynamitechetan
If you've found any problem in our code, then I request you to make a pull request.

@AnupKumarPanwar
Copy link
Member

Yeah exactly you can think of it as an Open Source book

@SergeyTsaplin
Copy link
Contributor

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

@miedzinski
Copy link
Author

miedzinski commented Jul 29, 2016

@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:

30:1: W391 blank line at end of file
2:1: W293 blank line contains whitespace
6:1: W293 blank line contains whitespace
8:1: W293 blank line contains whitespace
14:1: W293 blank line contains whitespace
16:1: W293 blank line contains whitespace
20:1: W293 blank line contains whitespace
28:7: E225 missing whitespace around operator
28:11: E201 whitespace after '('
11:14: E111 indentation is not a multiple of four
12:14: E111 indentation is not a multiple of four
7:16: E225 missing whitespace around operator
21:18: E712 comparison to False should be 'if cond is False:' or 'if not cond:'

It looks like there is a warning every second line, including those empty. It's not good.
Regarding efficiency, I don't think it is that important when showing Python to newcomers. Code should be friendly and clean. Anyway, this could be rewritten using bultin modules and we would achieve two things for free ("C speed" and reducing line count). Copy-pasting from Python docs:

import bisect


def binary_search(sequence, value):
    index = bisect.bisect_left(sequence, value)
    if index != len(sequence) and sequence[index] == value:
        return index
    raise ValueError

Using this, we can easily write another function, which returns True/False depending on presence of item in sequence (just like current implementation as of 4c2c3c5)

def bin_exists(sequence, value):
    try:
        binary_search(sequence, value)
    except ValueError:
        return False
    else:
        return True

Voilà, done. Side-effects free, clean, concise and pythonic.

Add some docstrings, examples of usage, if __name__ == '__main__' idiom for testing from terminal and it's ready for PR.

Big difference, IMO.

@dynamitechetan
Copy link
Member

@miedzinski I will try to fix it asap.
And I would also like to know which weekly newsletter this repo got posted.
Thank you for your help!

@miedzinski
Copy link
Author

@dynamitechetan, Pycoder's Weekly, issue #227.

@SergeyTsaplin
Copy link
Contributor

SergeyTsaplin commented Jul 29, 2016

Made binary_search algo usual way. Also binary search method used stdlib added (suggested to @miedzinski comment) - binary_search - pure implementation binary_search_std_lib - implementation using stdlib's bisect
Just merge #6

@harshildarji
Copy link
Member

@SergeyTsaplin Merged!

@miedzinski
Copy link
Author

@SergeyTsaplin, much better. But you introduced one, very bad, thing to with new implementation. Your code is O(nlogn), because of assertion which sorts the sequence. You could do it O(n), but in the same way you could implement linear search, which beats the purpose of doing binary search in first place. Just drop it.

@dynamitechetan dynamitechetan added the enhancement This PR modified some existing files label Jul 30, 2016
@SergeyTsaplin
Copy link
Contributor

Guys, don't name files such way please:

  • Simple Substitution Cipher.py
  • BubbleSort.py etc

First example is absolutely amateurish. Second - is not pythonic
If you see such pull request - just reject it. Also it's very useful to use some CI-service to prevent bad code

@harshildarji
Copy link
Member

@SergeyTsaplin Thank you for suggestion. Actually it's my mistake, and I just solved it.

@miedzinski
Copy link
Author

I recommend reading this and then this.

@harshildarji
Copy link
Member

@SergeyTsaplin I've included test cases in all cipher scripts and this tests are running well locally as shown in below image:
capture

But, when I commit and push the same, Travis says that build failed.
Can you please take a look at this?

@miedzinski
Copy link
Author

Read the build log. It's explained there.


I am closing this. Clearly some changes to code were made.

harshildarji pushed a commit that referenced this issue Oct 30, 2017
archaengel added a commit to archaengel/Python that referenced this issue Oct 6, 2020
archaengel added a commit to archaengel/Python that referenced this issue Oct 6, 2020
archaengel added a commit to archaengel/Python that referenced this issue Oct 6, 2020
gitbook-com bot pushed a commit to imanghotbi-python/Python that referenced this issue Nov 13, 2022
furthermares added a commit to furthermares/Python that referenced this issue Jul 16, 2023
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/)
cclauss pushed a commit that referenced this issue Jul 24, 2023
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

No branches or pull requests

6 participants