alternative solution for no. 87

This commit is contained in:
Sebastian Wallkötter 2021-06-13 16:20:39 +02:00
parent 0087eccf20
commit 72fc6399b2
3 changed files with 27 additions and 0 deletions

View File

@ -995,6 +995,15 @@ k = 4
S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), 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) np.arange(0, Z.shape[1], k), axis=1)
print(S) 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? (★★★) #### 88. How to implement the Game of Life using numpy arrays? (★★★)
`No hints provided...` `No hints provided...`

View File

@ -995,6 +995,15 @@ k = 4
S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), 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) np.arange(0, Z.shape[1], k), axis=1)
print(S) 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? (★★★) #### 88. How to implement the Game of Life using numpy arrays? (★★★)

View File

@ -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) np.arange(0, Z.shape[1], k), axis=1)
print(S) 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 < q88
How to implement the Game of Life using numpy arrays? (★★★) How to implement the Game of Life using numpy arrays? (★★★)