Minimal examples of data structures and algorithms in Python
github.com
github.com
if __name__ == "__main__":
import doctest
doctest.testmod()
https://docs.python.org/3.5/library/doctest.htmlI know that the title says "minimal examples" and the goal is not make it "production ready", but as a learning resource you should always make sure to provide good examples, because that's what the students will use and write later on.
[1] https://docs.python.org/3/library/collections.abc.html#colle...
The point I was making, albeit poorly, was open-source is awesome. git, github and pull requests make it even better.
When someone contributes something that may be half-baked and someone else comes along to critique it. A pull-request that demonstrates the solution can be an equally efficient means of communicating the same criticism. Meanwhile, the whole community reaps the benefit of that effort.
TLDR; OSS devs should favor, "showing the way" and github makes that easy.
(switch to the branch master if you don't want to see the solution and want to give it a try by yourself).
In any case, if OP is the owner of the repo, good job compiling all that code together.
Interface.py follows a pattern Ive seen in Java but not in Python. Am I simply too much of a Python noob?
The code in the repo looks pythonic to me. This one looks pythonic to me. http://stackoverflow.com/a/283630/1424242
It seems to imply the Python has the final solution in how to write code and therefore everything should be written in the way that Python elders have decided.
Main example: I don't really like list comprehensions. They're pretty much the same thing as map/filter but less understandable or composable, nevermind you fix (with your choice of brackets in multiple places) your choice of list vs generator vs "pushing source" (not sure the right word) which you wouldn't have to with transducers.
http://stackoverflow.com/questions/952914/making-a-flat-list...
I'd rather read code where someone wrote "flatten".
I have theory that, python was introduced out of frustration with Perl. As we all know, Perl had the principle of TIMTOWTDI.
Another far more tangential and biased theory here: As Python imposes strict programming principles, it resembles Java in forcing the programmer to abide by the styles set out by the language designers. And maybe that's why schools prefer to teach Python instead of Ruby as a scripting language.
PS: I have nothing against Java and I love Python. My theory is based upon Guido's insistence on not opening the language fully to functional programming. I'd like to see better lambdas, functional programming without itertools etc. But I know Guido has his reasons, pythonic code uses list comprehensions instead of FP.
With minimal code like this, I'm a little torn between declaring a class with members and using an anonymous data structure. A tree for example can be a tuple in python, and doesn't need to be declared. How big do you let it get before taking the time to declare a class?
Re: primes_sieve_of_eratosthenes.py, after doing more primes problems on Project Euler than I care to admit, I ended up with this pretty minimal sieve. The repo's indexing and .append in the loop slow it down.
sieve = [True] * n
for i in xrange(2,int(n**0.5)+1):
if sieve[i]: sieve[i*i::i] = [False] * ((n-i*i-1)/(i)+1)
And total tangent but have you guys seen the amazing and extremely memory efficient postponed sieve generator? http://ideone.com/WFv4fhttp://numba.pydata.org/numba-doc/0.18.1/developer/generator...
def tree(): return defaultdict(tree)
More here: https://gist.github.com/hrldcpr/2012250
The wiki (https://wiki.python.org/moin/BitwiseOperators) says:
Of course, Python doesn't use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011".