12
Overview
In this challenge, your task is to randomly generate a monotonic mathematical function between two sets.
Input
Your inputs are two positive integers s
and n
.
After getting these inputs, your program shall generate a random mathematical function f
from the set {0,1,...,s-1}n
to {0,1,...,s-1}
.
In other words, f
is a "rule" that takes in an n
-tuple of integers between 0
and s-1
, and returns one such integer.
Additionally, f
should be monotonic in the following sense.
If A
and B
are two n
-tuples such that A[i] ≥ B[i]
holds for every coordinate i
, then f(A) ≥ f(B)
.
The exact distribution of the monotonic functions f
doesn't matter, as long as each such function has a positive probability of being generated (assuming a perfect RNG).
Output
Your output shall be an enumeration of the inputs and outputs of f
.
It shall contain all n
-tuples of integers between 0
and s-1
in some order, each one being followed by the corresponding output of f
.
The exact output format is flexible (within reason).
Examples
The inputs s = 3
and n = 2
might produce the output
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 0
(1, 1) 1
(1, 2) 2
(2, 0) 1
(2, 1) 1
(2, 2) 2
It contains all the pairs over the set {0, 1, 2}
exactly once, and each one is followed by its f
-value.
The monotonicity condition is also satisfied.
The tuples are given here in lexicographical order, but this is not necessary.
As another example, s = 2
and n = 4
might produce
(0, 0, 0, 0) 0
(0, 0, 0, 1) 0
(0, 0, 1, 0) 0
(0, 0, 1, 1) 0
(0, 1, 0, 0) 1
(0, 1, 0, 1) 1
(0, 1, 1, 0) 1
(0, 1, 1, 1) 1
(1, 0, 0, 0) 0
(1, 0, 0, 1) 1
(1, 0, 1, 0) 0
(1, 0, 1, 1) 1
(1, 1, 0, 0) 1
(1, 1, 0, 1) 1
(1, 1, 1, 0) 1
(1, 1, 1, 1) 1
The following are all possible outputs for s = 2
and n = 2
(up to reordering the tuples); your program should randomly output one of them:
(0,0) 0
(0,1) 0
(1,0) 0
(1,1) 0
-------
(0,0) 0
(0,1) 0
(1,0) 0
(1,1) 1
-------
(0,0) 0
(0,1) 0
(1,0) 1
(1,1) 1
-------
(0,0) 0
(0,1) 1
(1,0) 0
(1,1) 1
-------
(0,0) 0
(0,1) 1
(1,0) 1
(1,1) 1
-------
(0,0) 1
(0,1) 1
(1,0) 1
(1,1) 1
Rules and Scoring
You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed. Code with explanation is preferred.
There are no restrictions on time complexity, but I'll give a bonus of -15 % if your solution is always guaranteed to finish in a certain amount of time (depending on the inputs, and assuming a perfect RNG that runs in constant time).
It might help if you completely enumerate all of the possible functions for a tiny case such as s=2 n=2. I had to read the description a few times to grasp how the randomness would come into play. – Sparr – 2015-09-23T18:47:43.787
@Sparr Good idea; edited. – Zgarb – 2015-09-23T18:53:39.973
is bounded runtime a requirement? I'm contemplating a solution that produces random functions until it finds a monotonic one. – Sparr – 2015-09-23T18:57:57.420
@Sparr I think I'll add a bonus for bounded runtime, so such a solution won't be disqualified. – Zgarb – 2015-09-23T19:00:16.893
@Zgarb - perhaps you should make a large bonus for solutions that are both bounded and likely to finish within an hour. – Glen O – 2015-09-24T06:46:39.997