September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,11 @@
# Given an m x n matrix,
# produce a stream of the matrix elements (taken row-wise)
# up to but excluding the first occurrence of $max
def stream($max):
. as $matrix
| length as $m
| (.[0] | length) as $n
| label $ok
| {i: range(0;$m), j: range(0;$n)}
| $matrix[.i][.j] as $m
| if $m == $max then break $ok else $m end ;

View file

@ -0,0 +1,9 @@
# Create an array of arrays by using the items in the stream, s,
# to create successive rows, each row having at most n items.
def reshape(s; n):
reduce s as $s ({i:0, j:0, matrix: []};
.matrix[.i][.j] = $s
| if .j + 1 == n then .i += 1 | .j = 0
else .j += 1
end)
| .matrix;

View file

@ -0,0 +1,10 @@
# Create an m x n matrix filled with numbers in [1 .. max]
def randomMatrix(m; n; max):
reshape(limit(m * n; rand(max) + 1); n);
# Present the matrix up to but excluding the first occurrence of $max
def show($m; $n; $max):
reshape( randomMatrix($m; $n; $max) | stream($max); $n)[] ;
# Main program for the problem at hand.
show(20; 4; 20)

View file

@ -0,0 +1,17 @@
# LCG::Microsoft generates 15-bit integers using the same formula
# as rand() from the Microsoft C Runtime.
# Input: [ count, state, random ]
def next_rand_Microsoft:
.[0] as $count
| ((214013 * .[1]) + 2531011) % 2147483648 # mod 2^31
| [$count+1 , ., (. / 65536 | floor) ];
def rand_Microsoft(seed):
[0,seed]
| next_rand_Microsoft # the seed is not so random
| recurse( next_rand_Microsoft )
| .[2];
# A random integer in [0 ... (n-1)]:
# rand_Microsoft returns an integer in 0 .. 32767
def rand(n): n * (rand_Microsoft($seed|tonumber) / 32768) | trunc;