Here's some more code from the night class I'm teaching, invisibly in the sense it's all virtual, but in real time, different from my usual asynchronous gig based on evaluating student work.
About twenty login and we have our private session, company tech support listening in for Q/A. Complementary synchronous work, with students off the hook but welcome and often eager to dive in (I don't see their desktops usually, though they're free to share). The code below started out as a Lab where I just suggested using trial by division (I described the approach) in the context of an iterator. We're just at that point where "iterator" (versus iterable) is coming together with these new grammatical constructs, these new underscore names. We're also tackling decorators and context managers around this same time. # -*- coding: utf-8 -*- """ Created on Tue Dec 8 17:39:27 2015 @author: Kirby Urner, 4Dsolutions.net (copyleft) MIT License, 2015 Two models of iterator that do the same thing. All primes found so far will be a list of all primes <= n without any missing. Algorithm: trial by division. """ def next_prime(): """ creates an iterator that spits out successive prime numbers """ so_far = [2] yield 2 n = 2 while True: n += 1 for divisor in so_far: if divmod(n, divisor)[1] == 0: # goes evenly break if divisor ** 2 > n: yield n so_far.append(n) break class Primes: """ creates an iterator that spits out successive prime numbers """ def __init__(self): self.so_far = [2] self.n = 2 def __iter__(self): return self def __next__(self): if self.n == 2: self.n += 1 return 2 while True: for divisor in self.so_far: if divmod(self.n, divisor)[1] == 0: # goes evenly break if divisor ** 2 > self.n: self.so_far.append(self.n) self.n += 1 return self.so_far[-1] self.n += 1 There's a license on it not because I think I'm being all that original i.e. these is not genius math or anything. I've been told people are more comfortable sharing if there's at least some explicit attention to sharing rights. The point for purposes of my class was to show two ways to write an iterator, using function generator syntax or a class with the __next__, __iter__ protocol. The latter is a refactoring of the former, or vice versa. However, putting on my "used to be a high school math teacher" hat, I really like this naive ramping up into Primeville. As an algorithm it sputters if your goal is working with uber-primes ala RSA or whatever, but this is not about dissing algorithms its about learning what "algorithm" means. Also the difference between prime and composite. I found myself bringing up the Fundamental Theorem of Arithmetic again, just like the old days (I haven't been a full time high school math teach since the early 1980s, spent a lot more years coding in FoxPro :-D). >>> g = next_prime() >>> [next(g) for _ in range(20)] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71] >>> g = Primes() >>> [next(g) for _ in range(20)] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71] Other news from my angle... We're enjoying lots of action on python-cuba (a workgroup) -- some planning conference coming up on the 12th. Public archive, check it out. https://mail.python.org/mailman/listinfo/python-cuba Also I'm sometimes chiming in on the Chipy listserv, one of my favorites. Portland's is quiet by contrast (I'm in Portland -- born in Chicago though). Kirby
_______________________________________________ Edu-sig mailing list Edu-sig@python.org https://mail.python.org/mailman/listinfo/edu-sig