From 59a403da664431f6600b662d32e6b773422f12a2 Mon Sep 17 00:00:00 2001 From: Gattocrucco Date: Mon, 25 Aug 2025 18:13:30 +0200 Subject: [PATCH 1/3] alternative solution to exercise 87 Added a more elegant (but more cryptic) solution with reshape and swapaxes. Removed one of the existing alternative solutions because it was a duplicate of a previous alternative solution. --- 100_Numpy_exercises_with_hints_with_solutions.md | 7 ++----- 100_Numpy_exercises_with_solutions.md | 7 ++----- source/exercises100.ktx | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 409c00f4..d3ddde2e 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -1086,11 +1086,8 @@ k = 4 windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) -# Author: Jeff Luo (@Jeff1999) - -Z = np.ones((16, 16)) -k = 4 -print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1))) +# alternative solution (by @Gattocrucco) +S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3)) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★) `No hints provided...` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index c7285d23..f3662088 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -1086,11 +1086,8 @@ k = 4 windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) -# Author: Jeff Luo (@Jeff1999) - -Z = np.ones((16, 16)) -k = 4 -print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1))) +# alternative solution (by @Gattocrucco) +S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3)) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index d1457aa1..3f71c5dc 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1333,11 +1333,8 @@ k = 4 windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) -# Author: Jeff Luo (@Jeff1999) - -Z = np.ones((16, 16)) -k = 4 -print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1))) +# alternative solution (by @Gattocrucco) +S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3)) < q88 How to implement the Game of Life using numpy arrays? (★★★) From 301e0eb61e41a1c000736901a0b3d11e3b0fb6fe Mon Sep 17 00:00:00 2001 From: Gattocrucco Date: Tue, 26 Aug 2025 00:16:34 +0200 Subject: [PATCH 2/3] further simplification to the alt solution of 87 The initial formulation contained a redundant `swapaxes`. --- 100_Numpy_exercises_with_hints_with_solutions.md | 2 +- 100_Numpy_exercises_with_solutions.md | 2 +- source/exercises100.ktx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index d3ddde2e..29f4f71b 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -1087,7 +1087,7 @@ windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) # alternative solution (by @Gattocrucco) -S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3)) +S = Z.reshape(4, 4, 4, 4).sum((1, 3)) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★) `No hints provided...` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index f3662088..af8413c0 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -1087,7 +1087,7 @@ windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) # alternative solution (by @Gattocrucco) -S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3)) +S = Z.reshape(4, 4, 4, 4).sum((1, 3)) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 3f71c5dc..a0725c6b 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1334,7 +1334,7 @@ windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) # alternative solution (by @Gattocrucco) -S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3)) +S = Z.reshape(4, 4, 4, 4).sum((1, 3)) < q88 How to implement the Game of Life using numpy arrays? (★★★) From fc726931602f2b5d22c2ab75d951979707ed1929 Mon Sep 17 00:00:00 2001 From: Gattocrucco Date: Tue, 26 Aug 2025 10:56:10 +0200 Subject: [PATCH 3/3] reset derived files to original state --- 100_Numpy_exercises_with_hints_with_solutions.md | 7 +++++-- 100_Numpy_exercises_with_solutions.md | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 29f4f71b..409c00f4 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -1086,8 +1086,11 @@ k = 4 windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) -# alternative solution (by @Gattocrucco) -S = Z.reshape(4, 4, 4, 4).sum((1, 3)) +# Author: Jeff Luo (@Jeff1999) + +Z = np.ones((16, 16)) +k = 4 +print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1))) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★) `No hints provided...` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index af8413c0..c7285d23 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -1086,8 +1086,11 @@ k = 4 windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) S = windows[::k, ::k, ...].sum(axis=(-2, -1)) -# alternative solution (by @Gattocrucco) -S = Z.reshape(4, 4, 4, 4).sum((1, 3)) +# Author: Jeff Luo (@Jeff1999) + +Z = np.ones((16, 16)) +k = 4 +print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1))) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★)