Skip to content

Commit 02ea4b9

Browse files
committed
updated primes solution
1 parent 4e2c273 commit 02ea4b9

File tree

5 files changed

+85
-17
lines changed

5 files changed

+85
-17
lines changed

source/class_schedule/session_2_01.rst

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
1-
#########################################
2-
Advanced Programming in Python: Session 1
3-
#########################################
1+
##############################################
2+
Session 1: Iterators and Generators, Packaging
3+
##############################################
44

5-
lambda
65

7-
closures
6+
* Iterators and iterables
87

8+
* The iteration protocol
99

10-
Pre-class prep
11-
==============
10+
* Iterable classes
1211

13-
:ref:`anonymous_functions`
12+
* Generators
1413

15-
:ref:`closures`
14+
* Packages and packaging
1615

16+
Reading
17+
=======
18+
19+
:ref:`iterators_generators`
20+
21+
:ref:`packaging`
1722

1823
Supplemental Readings
1924
=====================
2025

21-
http://www.blog.pythonlibrary.org/2015/10/28/python-101-lambda-basics/
26+
Iterators and Generators
27+
........................
28+
29+
*Fluent Python:* Chapter 14: Iterables, Iterators, and Generators
30+
31+
Packages and Packaging
32+
......................
33+
34+
https://packaging.python.org/
35+
36+
http://pythonchb.github.io/PythonTopics/where_to_put_your_code.html
2237

23-
https://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
2438

2539
Functools
2640
.........

source/modules/Packaging.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ But ``distutils`` is missing some key features:
147147

148148
Now it's pretty stable: pip+setuptools+wheel: use them.
149149

150+
**warning** -- setuptools still provides easy_install, but it hss mostly been deprecated, so you really want to use pip. And sometimes setuptools will invoke it for you under the hood by accident :-(
151+
150152

151153
Installing Packages
152154
-------------------
@@ -159,21 +161,25 @@ But getting better, and the mess is *almost* cleaned up.
159161
Current State of Packaging
160162
--------------------------
161163

162-
To build packages: distutils
164+
To build packages: setuptools
165+
.............................
166+
167+
* https://pythonhosted.org/setuptools/
163168

164-
* http://docs.python.org/3/distutils/
169+
setuptools provides extensions to the build-in distutils:
165170

166-
For more features: setuptools
171+
https://docs.python.org/3/library/distutils.html
167172

168-
* https://pythonhosted.org/setuptools/
173+
But there are a couple of those extensions that you really do need, so most folks use setuptools for everything. In fact, pip itself requires setuptools.
169174

170-
(note that setuptools still can also install -- but don't let it)
171175

172176
To install packages: pip
177+
........................
173178

174179
* https://pip.pypa.io/en/latest/installing.html
175180

176181
For binary packages: wheels
182+
...........................
177183

178184
* http://www.python.org/dev/peps/pep-0427/
179185

source/solutions/iterators_generators/generators.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,39 @@ def prime(): # 2, 3, 5, 7, 11, 13, 17, 19, 23...
5555
if a % x == 0:
5656
p = False # ...unless it isn't
5757
break
58+
59+
def prime(): # 2, 3, 5, 7, 11, 13, 17, 19, 23...
60+
""" note that this is NOT a very efficient way to compute primes!"""
61+
n = 0
62+
a = 2
63+
while True:
64+
yield a
65+
p = False
66+
while not p: # while not prime
67+
a += 1 # try the next integer
68+
p = True # assume it is prime...
69+
for x in range(2, int(math.floor(math.sqrt(a))) + 1):
70+
if a % x == 0:
71+
p = False # ...unless it isn't
72+
break
73+
74+
def prime2(): # 2, 3, 5, 7, 11, 13, 17, 19, 23...
75+
""" This is a bit more efficient"""
76+
# keep a list of the primes already generated
77+
primes = []
78+
a = 2
79+
while True:
80+
# print("adding %s to the list"%a)
81+
primes.append(a)
82+
yield a
83+
p = False
84+
while not p: # while not prime
85+
# try the next non-even integer
86+
a += (2 if (a % 2) else 1)
87+
p = True # assume it is prime...
88+
# test if it is divisible by any of the smaller primes
89+
for x in primes:
90+
if a % x == 0:
91+
p = False # ...unless it isn't
92+
break
93+

source/solutions/iterators_generators/sum67.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
A student wondered if this exercise could be done with a generator.
55
6-
Indeed is can
6+
Indeed it can
77
88
Return the sum of the numbers in the array, except ignore sections of
99
numbers starting with a 6 and extending to the next 7 (every 6 will be

source/solutions/iterators_generators/test_generators.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ def test_prime():
3737
g = gs.prime()
3838
for val in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]:
3939
assert next(g) == val
40+
41+
42+
def test_prime2():
43+
""" same test as above, but a better algorithm """
44+
g = gs.prime2()
45+
for val in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]:
46+
p = next(g)
47+
print("p is:", p)
48+
assert p == val
49+
# assert False
50+
51+

0 commit comments

Comments
 (0)