Skip to content

Commit d446740

Browse files
committed
Updating code and notebooks
1 parent fe4036a commit d446740

File tree

5 files changed

+1369
-34
lines changed

5 files changed

+1369
-34
lines changed

nb/Cell2D.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,40 @@ def draw(self, **options):
5151
"""Draws the array."""
5252
draw_array(self.array, **options)
5353

54-
def animate(self, frames, interval=None, step=None):
54+
def animate(self, frames, interval=0.001, step=None):
5555
"""Animate the automaton.
5656
5757
frames: number of frames to draw
5858
interval: time between frames in seconds
59-
iters: number of steps between frames
59+
step: function to call between frames
6060
"""
6161
if step is None:
6262
step = self.step
6363

64+
plt.ion()
6465
plt.figure()
6566
try:
6667
for _ in range(frames - 1):
6768
self.draw()
68-
plt.show()
69-
if interval:
70-
sleep(interval)
69+
plt.pause(interval)
7170
step()
7271
clear_output(wait=True)
7372
self.draw()
7473
plt.show()
7574
except KeyboardInterrupt:
7675
pass
76+
finally:
77+
plt.ioff()
7778

7879

7980
def draw_array(array, **options):
80-
"""Draws the cells."""
81+
"""Draws the cells.
82+
83+
array: numpy array
84+
options: passed to plt.imshow
85+
86+
returns: plt.imshow object
87+
"""
8188
n, m = array.shape
8289
options = underride(
8390
options,

nb/Life.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def main(script, *args):
5858
life.add_cells(n // 2 + 12, col, *lwss)
5959
life.add_cells(n // 2 + 26, col, *lwss)
6060
life.add_cells(n // 2 + 19, col, *bhep)
61-
life.animate(frames=100)
61+
life.animate(frames=1000)
6262
plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)
6363
plt.show()
6464

nb/LifeRabbits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def main(script, *args):
2525
m = 600
2626
life = Life(n, m)
2727
life.add_cells(n // 2, m // 2, *rabbits)
28-
life.animate(frames=100, interval=1)
28+
life.animate(frames=1000)
2929
plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)
3030
plt.show()
3131

nb/appA.ipynb

Lines changed: 1307 additions & 0 deletions
Large diffs are not rendered by default.

nb/chap06.ipynb

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"Copyright 2016 Allen Downey, [MIT License](http://opensource.org/licenses/MIT)"
1212
]
1313
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"%load_ext autoreload\n",
21+
"%autoreload 2"
22+
]
23+
},
1424
{
1525
"cell_type": "code",
1626
"execution_count": 2,
@@ -192,7 +202,7 @@
192202
"outputs": [],
193203
"source": [
194204
"life = make_life(4, 4, 1, 0, '0111', '1110')\n",
195-
"life.animate(10, 0.5)"
205+
"anim = life.animate(10, 0.5)"
196206
]
197207
},
198208
{
@@ -231,7 +241,7 @@
231241
},
232242
{
233243
"cell_type": "code",
234-
"execution_count": 13,
244+
"execution_count": 15,
235245
"metadata": {},
236246
"outputs": [],
237247
"source": [
@@ -255,7 +265,7 @@
255265
},
256266
{
257267
"cell_type": "code",
258-
"execution_count": 14,
268+
"execution_count": 21,
259269
"metadata": {},
260270
"outputs": [],
261271
"source": [
@@ -275,8 +285,10 @@
275285
},
276286
{
277287
"cell_type": "code",
278-
"execution_count": 15,
279-
"metadata": {},
288+
"execution_count": 22,
289+
"metadata": {
290+
"scrolled": true
291+
},
280292
"outputs": [],
281293
"source": [
282294
"# r pentomino\n",
@@ -294,7 +306,7 @@
294306
},
295307
{
296308
"cell_type": "code",
297-
"execution_count": 16,
309+
"execution_count": 23,
298310
"metadata": {},
299311
"outputs": [],
300312
"source": [
@@ -322,7 +334,7 @@
322334
},
323335
{
324336
"cell_type": "code",
325-
"execution_count": 17,
337+
"execution_count": 24,
326338
"metadata": {},
327339
"outputs": [],
328340
"source": [
@@ -428,7 +440,7 @@
428440
},
429441
{
430442
"cell_type": "code",
431-
"execution_count": 17,
443+
"execution_count": 28,
432444
"metadata": {},
433445
"outputs": [],
434446
"source": [
@@ -445,7 +457,7 @@
445457
},
446458
{
447459
"cell_type": "code",
448-
"execution_count": 18,
460+
"execution_count": 29,
449461
"metadata": {},
450462
"outputs": [],
451463
"source": [
@@ -475,7 +487,7 @@
475487
},
476488
{
477489
"cell_type": "code",
478-
"execution_count": 19,
490+
"execution_count": 30,
479491
"metadata": {},
480492
"outputs": [],
481493
"source": [
@@ -500,7 +512,7 @@
500512
},
501513
{
502514
"cell_type": "code",
503-
"execution_count": 20,
515+
"execution_count": 31,
504516
"metadata": {},
505517
"outputs": [],
506518
"source": [
@@ -523,7 +535,7 @@
523535
},
524536
{
525537
"cell_type": "code",
526-
"execution_count": 21,
538+
"execution_count": 32,
527539
"metadata": {},
528540
"outputs": [],
529541
"source": [
@@ -545,7 +557,7 @@
545557
},
546558
{
547559
"cell_type": "code",
548-
"execution_count": 28,
560+
"execution_count": 33,
549561
"metadata": {},
550562
"outputs": [],
551563
"source": [
@@ -554,7 +566,7 @@
554566
},
555567
{
556568
"cell_type": "code",
557-
"execution_count": 29,
569+
"execution_count": 34,
558570
"metadata": {},
559571
"outputs": [],
560572
"source": [
@@ -563,7 +575,16 @@
563575
},
564576
{
565577
"cell_type": "code",
566-
"execution_count": 31,
578+
"execution_count": 35,
579+
"metadata": {},
580+
"outputs": [],
581+
"source": [
582+
"# Solution goes here"
583+
]
584+
},
585+
{
586+
"cell_type": "code",
587+
"execution_count": 36,
567588
"metadata": {},
568589
"outputs": [],
569590
"source": [
@@ -587,7 +608,7 @@
587608
},
588609
{
589610
"cell_type": "code",
590-
"execution_count": 26,
611+
"execution_count": 37,
591612
"metadata": {},
592613
"outputs": [],
593614
"source": [
@@ -609,7 +630,7 @@
609630
},
610631
{
611632
"cell_type": "code",
612-
"execution_count": 27,
633+
"execution_count": 38,
613634
"metadata": {},
614635
"outputs": [],
615636
"source": [
@@ -633,7 +654,7 @@
633654
},
634655
{
635656
"cell_type": "code",
636-
"execution_count": 28,
657+
"execution_count": 39,
637658
"metadata": {},
638659
"outputs": [],
639660
"source": [
@@ -678,7 +699,7 @@
678699
},
679700
{
680701
"cell_type": "code",
681-
"execution_count": 32,
702+
"execution_count": 41,
682703
"metadata": {},
683704
"outputs": [],
684705
"source": [
@@ -687,7 +708,7 @@
687708
},
688709
{
689710
"cell_type": "code",
690-
"execution_count": 33,
711+
"execution_count": 42,
691712
"metadata": {},
692713
"outputs": [],
693714
"source": [
@@ -698,7 +719,7 @@
698719
},
699720
{
700721
"cell_type": "code",
701-
"execution_count": 34,
722+
"execution_count": 43,
702723
"metadata": {},
703724
"outputs": [],
704725
"source": [
@@ -708,7 +729,7 @@
708729
},
709730
{
710731
"cell_type": "code",
711-
"execution_count": 35,
732+
"execution_count": 51,
712733
"metadata": {},
713734
"outputs": [],
714735
"source": [
@@ -717,19 +738,19 @@
717738
},
718739
{
719740
"cell_type": "code",
720-
"execution_count": 36,
741+
"execution_count": 50,
721742
"metadata": {},
722743
"outputs": [],
723744
"source": [
724745
"# And a larger version with 1000 steps\n",
725746
"\n",
726747
"turmite = Turmite(n=30)\n",
727-
"anim = turmite.animate(frames=1000)"
748+
"turmite.animate(frames=1000)"
728749
]
729750
},
730751
{
731752
"cell_type": "code",
732-
"execution_count": 34,
753+
"execution_count": null,
733754
"metadata": {},
734755
"outputs": [],
735756
"source": []
@@ -751,7 +772,7 @@
751772
"name": "python",
752773
"nbconvert_exporter": "python",
753774
"pygments_lexer": "ipython3",
754-
"version": "3.7.11"
775+
"version": "3.10.14"
755776
}
756777
},
757778
"nbformat": 4,

0 commit comments

Comments
 (0)