In [74]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
In [75]:
import antipackage
import github.ellisonbg.misc.vizarray as va
Write a Python function that creates a square (size,size) 2d Numpy array with the values 0.0 and 1.0:
size.0,0 element should be 1.0.dtype should be float.
In [103]:
def checkerboard(size):
a = np.zeros((size,size), dtype = np.float)
b = 2
if size % 2 != 0:
for element in np.nditer(a, op_flags=['readwrite']):
if size % 2 != 0:
if b % 2 == 0:
element[...] = element + 1.0
b += 1
else:
b += 1
return a
else:
c = [1,0]
d = [0,1]
e = []
f = size / 2
g = list(range(1, size + 1))
for item in g:
if item % 2 != 0:
e.append(c * f)
else:
e.append(d * f)
h = np.array(e, dtype = np.float)
return h
print checkerboard(4)
Use vizarray to visualize a checkerboard of size=20 with a block size of 10px.
In [104]:
va.set_block_size(10)
va.vizarray(checkerboard(20))
Out[104]:
In [105]:
assert True
Use vizarray to visualize a checkerboard of size=27 with a block size of 5px.
In [106]:
va.set_block_size(5)
va.vizarray(checkerboard(27))
Out[106]:
In [107]:
assert True
In [108]:
a = checkerboard(4)
assert a[0,0]==1.0
assert a.sum()==8.0
assert a.dtype==np.dtype(float)
assert np.all(a[0,0:5:2]==1.0)
assert np.all(a[1,0:5:2]==0.0)
b = checkerboard(5)
assert b[0,0]==1.0
assert b.sum()==13.0
assert np.all(b.ravel()[0:26:2]==1.0)
assert np.all(b.ravel()[1:25:2]==0.0)