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.
This commit is contained in:
@@ -1086,11 +1086,8 @@ k = 4
|
|||||||
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
||||||
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
||||||
|
|
||||||
# Author: Jeff Luo (@Jeff1999)
|
# alternative solution (by @Gattocrucco)
|
||||||
|
S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3))
|
||||||
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? (★★★)
|
#### 88. How to implement the Game of Life using numpy arrays? (★★★)
|
||||||
`No hints provided...`
|
`No hints provided...`
|
||||||
|
|||||||
@@ -1086,11 +1086,8 @@ k = 4
|
|||||||
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
||||||
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
||||||
|
|
||||||
# Author: Jeff Luo (@Jeff1999)
|
# alternative solution (by @Gattocrucco)
|
||||||
|
S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3))
|
||||||
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? (★★★)
|
#### 88. How to implement the Game of Life using numpy arrays? (★★★)
|
||||||
|
|
||||||
|
|||||||
@@ -1333,11 +1333,8 @@ k = 4
|
|||||||
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
|
||||||
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
|
||||||
|
|
||||||
# Author: Jeff Luo (@Jeff1999)
|
# alternative solution (by @Gattocrucco)
|
||||||
|
S = Z.reshape(4, 4, 4, 4).swapaxes(1, 2).sum((2, 3))
|
||||||
Z = np.ones((16, 16))
|
|
||||||
k = 4
|
|
||||||
print(sliding_window_view(Z, window_shape=(k, k))[::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? (★★★)
|
||||||
|
|||||||
Reference in New Issue
Block a user