exB: move slides into the exercise folder
A symlink is provided for compatibility.
This commit is contained in:
parent
8b58bbdef9
commit
ca3cf86e77
40
exercises/exerciseB/map_example_codealong.py
Normal file
40
exercises/exerciseB/map_example_codealong.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# Computer scientists love the Fibonacci sequence
|
||||||
|
# [https://en.wikipedia.org/wiki/Fibonacci_sequence], possibly
|
||||||
|
# described by Acharya Pingala in छन्दःशास्त्र (Chhandaḥśāstra).
|
||||||
|
#
|
||||||
|
# The basic definition is that the next item in the sequence
|
||||||
|
# if the sum of the two preceding items, and the two initial
|
||||||
|
# items and 0 and 1.
|
||||||
|
#
|
||||||
|
# We end up with [0, 1, 1, 2, 3, 5, 8, 13, ...]
|
||||||
|
|
||||||
|
def fibbo(n):
|
||||||
|
match n:
|
||||||
|
case 0 | 1:
|
||||||
|
return n
|
||||||
|
case _:
|
||||||
|
return fibbo(n-1) + fibbo(n-2)
|
||||||
|
|
||||||
|
# For example: the first few items in the sequence:
|
||||||
|
for n in range(36):
|
||||||
|
print(n, fibbo(n))
|
||||||
|
|
||||||
|
# A (nowadays) less commonly used way to construct the mapping
|
||||||
|
# from n to fibbo(n) is to use the map function.
|
||||||
|
# map returns a generator, and we call list to collect the numbers:
|
||||||
|
sequence = list(map(fibbo, range(36)))
|
||||||
|
print(sequence)
|
||||||
|
|
||||||
|
# To split the work between multiple Python processes, we can
|
||||||
|
# use the multiprocessing module:
|
||||||
|
import multiprocessing
|
||||||
|
|
||||||
|
pool = multiprocessing.Pool()
|
||||||
|
sequence = pool.map(fibbo, range(36))
|
||||||
|
print(sequence)
|
||||||
|
|
||||||
|
# We may use 'with' to clean up the pool after we're done with
|
||||||
|
# the workers:
|
||||||
|
with multiprocessing.Pool(10) as pool:
|
||||||
|
sequence = pool.map(fibbo, range(36))
|
||||||
|
print(sequence)
|
|
@ -1,40 +0,0 @@
|
||||||
# Computer scientists love the Fibonacci sequence
|
|
||||||
# [https://en.wikipedia.org/wiki/Fibonacci_sequence], possibly
|
|
||||||
# described by Acharya Pingala in छन्दःशास्त्र (Chhandaḥśāstra).
|
|
||||||
#
|
|
||||||
# The basic definition is that the next item in the sequence
|
|
||||||
# if the sum of the two preceding items, and the two initial
|
|
||||||
# items and 0 and 1.
|
|
||||||
#
|
|
||||||
# We end up with [0, 1, 1, 2, 3, 5, 8, 13, ...]
|
|
||||||
|
|
||||||
def fibbo(n):
|
|
||||||
match n:
|
|
||||||
case 0 | 1:
|
|
||||||
return n
|
|
||||||
case _:
|
|
||||||
return fibbo(n-1) + fibbo(n-2)
|
|
||||||
|
|
||||||
# For example: the first few items in the sequence:
|
|
||||||
for n in range(36):
|
|
||||||
print(n, fibbo(n))
|
|
||||||
|
|
||||||
# A (nowadays) less commonly used way to construct the mapping
|
|
||||||
# from n to fibbo(n) is to use the map function.
|
|
||||||
# map returns a generator, and we call list to collect the numbers:
|
|
||||||
sequence = list(map(fibbo, range(36)))
|
|
||||||
print(sequence)
|
|
||||||
|
|
||||||
# To split the work between multiple Python processes, we can
|
|
||||||
# use the multiprocessing module:
|
|
||||||
import multiprocessing
|
|
||||||
|
|
||||||
pool = multiprocessing.Pool()
|
|
||||||
sequence = pool.map(fibbo, range(36))
|
|
||||||
print(sequence)
|
|
||||||
|
|
||||||
# We may use 'with' to clean up the pool after we're done with
|
|
||||||
# the workers:
|
|
||||||
with multiprocessing.Pool(10) as pool:
|
|
||||||
sequence = pool.map(fibbo, range(36))
|
|
||||||
print(sequence)
|
|
1
slides/map_example_codealong.py
Symbolic link
1
slides/map_example_codealong.py
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../exercises/exerciseB/map_example_codealong.py
|
Loading…
Reference in a new issue