diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index f8b3f3d..5a62396 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -995,6 +995,15 @@ k = 4 S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), np.arange(0, Z.shape[1], k), axis=1) print(S) + +# alternative solution: +# Author: Sebastian Wallkötter (@FirefoxMetzger) + +Z = np.ones((16,16)) +k = 4 + +windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) +S = windows[::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 99fea53..b332550 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -995,6 +995,15 @@ k = 4 S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), np.arange(0, Z.shape[1], k), axis=1) print(S) + +# alternative solution: +# Author: Sebastian Wallkötter (@FirefoxMetzger) + +Z = np.ones((16,16)) +k = 4 + +windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) +S = windows[::k, ::k, ...].sum(axis=(-2, -1)) ``` #### 88. How to implement the Game of Life using numpy arrays? (★★★) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index aaa394e..cba706d 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1243,6 +1243,15 @@ S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), np.arange(0, Z.shape[1], k), axis=1) print(S) +# alternative solution: +# Author: Sebastian Wallkötter (@FirefoxMetzger) + +Z = np.ones((16,16)) +k = 4 + +windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k)) +S = windows[::k, ::k, ...].sum(axis=(-2, -1)) + < q88 How to implement the Game of Life using numpy arrays? (★★★)