Give another solution of problem #66, and fix the old solution as #142 did

This commit is contained in:
Fisher Wang 2021-03-09 21:42:18 +08:00
parent 7a830af4b7
commit d931b008a3
3 changed files with 31 additions and 2 deletions

View File

@ -682,9 +682,19 @@ print(F)
w,h = 16,16
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]
# The parenthesis around 256*256 ensure the dtpye promoted to uint32 properly.Otherwise the first term will overflow.
F = I[...,0]*(256*256) + I[...,1]*256 +I[...,2]
n = len(np.unique(F))
print(np.unique(I))
# Another solution
# Author: Fisher Wang
w, h = 16, 16
img = np.random.randint(0, 256, (w, h, 3)).astype(np.ubyte)
colors = np.unique(img.reshape(-1, 3), axis=0)
n = len(colors)
print(n)
```
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
`hint: sum(axis=(-2,-1))`

View File

@ -686,6 +686,15 @@ I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = I[...,0]*(256*256) + I[...,1]*256 +I[...,2]
n = len(np.unique(F))
print(np.unique(I))
# Another solution
# Author: Fisher Wang
w, h = 16, 16
img = np.random.randint(0, 256, (w, h, 3)).astype(np.ubyte)
colors = np.unique(img.reshape(-1, 3), axis=0)
n = len(colors)
print(n)
```
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)

View File

@ -865,10 +865,20 @@ hint: np.unique
w,h = 16,16
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]
# The parenthesis around 256*256 ensure the dtpye promoted to uint32 properly.Otherwise the first term will overflow.
F = I[...,0]*(256*256) + I[...,1]*256 +I[...,2]
n = len(np.unique(F))
print(np.unique(I))
# Another solution
# Author: Fisher Wang
w, h = 16, 16
img = np.random.randint(0, 256, (w, h, 3)).astype(np.ubyte)
colors = np.unique(img.reshape(-1, 3), axis=0)
n = len(colors)
print(n)
< q67
Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)