This note contains questions/puzzle and algorithms to answer these problems. Most of the codes in this type of note are in Python.

## Make chunkers / windows

Split a sequence into windows with size and slide.

``````def chunker(seq, size, slide=None, exact_size=False):
if slide is None:
slide = size
if exact_size:
return [seq[pos:pos + size] for pos in range(0, len(seq), slide) if len(seq[pos:pos + size]) == size]
else:
return [seq[pos:pos + size] for pos in range(0, len(seq), slide)]
``````
``````seq = [i for i in range(10)]

chunker(seq, 3)
chunker(seq, 3, exact_size=True)
chunker(seq, 3, slide=2)
``````
``````[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[[0, 1, 2], [3, 4, 5], [6, 7, 8], ]
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
[[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8], [8, 9]]
``````

Make all arrays equal in size, filled with `np.nan`,

``````def chunker2(seq, size, slide=None):
if slide is None:
slide = size
return [np.append( seq[pos:pos + size], np.repeat(np.nan, size-len(seq[pos:pos + size])) )
for pos in range(0, len(seq), slide)]
``````
``````seq = [i for i in range(10)]

chunker2(seq, size=4, slide=2)
``````
``````[array([0., 1., 2., 3.]),
array([2., 3., 4., 5.]),
array([4., 5., 6., 7.]),
array([6., 7., 8., 9.]),
array([ 8.,  9., nan, nan])]
``````

## Plot a grid of squares

Plot a square of `NxN` small other squares. Each one has a probability `Pxi/N` of being coloured, where `i` is the line where it stands.

``````N = 100
P = 0.5
im_size = 5

image = np.repeat(np.array(range(1,N+1)).reshape(N, 1), N, axis=1)

# LESS understandable but executing FASTER
image = (P/N * image) <= np.random.rand(N,N)

# MORE understandable but executing SLOWER
def bernoulli(num, P, N):
return 1-np.random.binomial(1, P*num/N)
vfunc = np.vectorize(bernoulli)
image = vfunc(image, P, N)

plt.figure(figsize=(im_size, im_size))
plt.imshow(image, cmap='gray')
plt.show()
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.53840315,"math_prob":0.9996531,"size":1793,"snap":"2020-45-2020-50","text_gpt3_token_len":649,"char_repetition_ratio":0.116825044,"word_repetition_ratio":0.1423611,"special_character_ratio":0.40044618,"punctuation_ratio":0.29531568,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99931693,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-21T12:57:15Z\",\"WARC-Record-ID\":\"<urn:uuid:52aa954c-8625-4eae-9b3f-90e51fdfc7da>\",\"Content-Length\":\"21038\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a8ed467d-948d-451b-aac8-e182479b6928>\",\"WARC-Concurrent-To\":\"<urn:uuid:f751b94e-5f29-4e2d-bd29-2992ef748d9e>\",\"WARC-IP-Address\":\"104.27.172.202\",\"WARC-Target-URI\":\"https://dinhanhthi.com/algorithm-1\",\"WARC-Payload-Digest\":\"sha1:C675S34KYOTJDIW7MOJ4SYM3GFFFSK62\",\"WARC-Block-Digest\":\"sha1:WYWSHCOCR5REHG2B773JORK6XCS5YHLC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107876500.43_warc_CC-MAIN-20201021122208-20201021152208-00368.warc.gz\"}"}