minmax.xreate
No OneTemporary

File Metadata

Created
Mon, Aug 24, 6:07 PM

minmax.xreate

El = type {idx:: int, val:: int}.
PairT = type(X) {X, X}.
Pair = type PairT(int).
PairEl = type PairT(El).
min = function(x:: int, y:: int):: int
{
if (x < y):: int { x } else { y }
}
max = function(x:: int, y:: int):: int
{
if (x > y):: int { x } else { y }
}
minEl = function(e1:: El, e2:: El):: El
{
if (e1["val"] < e2["val"]):: El {e1} else {e2}
}
maxEl = function(e1:: El, e2:: El):: El
{
if (e1["val"] > e2["val"]):: El {e1} else {e2}
}
minElIdx = function(x:: El, y:: El):: El
{
if (x["idx"] < y["idx"]):: El {x} else {y}
}
maxElIdx = function(x:: El, y:: El):: El
{
if (x["idx"] > y["idx"]):: El {x} else {y}
}
minLstEl = function(x:: El, y:: El):: El
{
if (x["val"] <= y["val"]):: El {x} else {y}
}
maxLstEl = function(x:: El, y:: El):: El
{
if (x["val"] >= y["val"]):: El {x} else {y}
}
fn-minmax1 = function:: {min:: int, max:: int}
{
arr = {25, 37, 12, 6, 5, 19, 3, 20}:: [int].
loop fold(arr->el:: int, {1000, 0}->state):: {min:: int, max:: int}
{
{
min(el, state["min"]),
max(el, state["max"])
}:: {min:: int, max:: int}
}
}
fn-minmax2 = function:: int
{
arr = {
{1, 72}, {3, 8}, {2, 5}, {3, 4}, {11, 1}, {6, 5}, {9, 2}
} :: [{int, int}].
loop fold(arr->rect:: {int, int}, 1000->squareMin):: int
{
square = rect[0] * rect[1]:: int.
min(square, squareMin)
}
}
fn-minmax3 = function:: int
{
arr = {
{1, 72}, {3, 8}, {2, 5}, {3, 4}, {11, 1}, {6, 5}, {9, 2}
} :: [{int, int}].
loop fold(arr->rect:: {int, int}, 0->perMax):: int
{
per = 2 * (rect[0] + rect[1]):: int.
max(per, perMax)
}
}
fn-minmax4 = function:: int
{
arr = {25, 37, 12, 6, 5, 19, 3, 20}:: [int].
minInit = {0, 25}:: El.
result = loop fold(arr->el:: int, {0, minInit}->state):: {idx:: int, minEl:: El}
{
elCur = {state["idx"], el}:: El.
idxNext = state["idx"] + 1:: int.
{idxNext, minEl(elCur, state["minEl"])}:: {idx:: int, minEl:: El}
}.
result["minEl", "idx"]
}
fn-minmax5 = function:: El
{
input = {
{1, 72}, {3, 8}, {2, 5}, {3, 4}, {11, 1}, {6, 5}, {9, 2}
} :: [{int, int}].
resultInit = {0, 0}:: El.
result = loop fold(input->el:: Pair, {0, resultInit}->acc):: {idx:: int, max:: El}
{
density = el[0] / el[1] :: int.
idxNext = acc["idx"] + 1:: int.
elCur = {acc["idx"], density}:: El.
{idxNext, maxEl(elCur, acc["max"])}:: {idx:: int, max:: El}
}.
result["max"]
}
fn-minmax6 = function:: Pair
{
arr = {25, 37, 1, 1, 6, 5, 19, 3, 37, 20}:: [int].
init = {0, {{1000, 1000}, {0, 0}}}:: {int, PairEl}.
result = loop fold(arr->val:: int, init->acc):: {int, PairEl}
{
el = {acc[0], val}:: El.
idx = acc[0]+1 :: int.
min = minEl(el, acc[1, 0]) :: El.
max = maxLstEl(el, acc[1, 1]):: El.
{idx, ({min, max}:: PairEl)}:: {int, PairEl}
}.
{result[1][0]["idx"], result[1][1]["idx"]}
}
fn-minmax7 = function:: Pair
{
arr = {25, 37, 1, 1, 6, 5, 19, 3, 37, 20}:: [int].
init = {0, {{0, 0}, {1000, 1000}}}:: {int, PairEl}.
result = loop fold(arr->val:: int, init->acc):: {int, PairEl}
{
el = {acc[0], val}:: El.
idx = acc[0]+1 :: int.
max = maxEl(el, acc[1, 0]) :: El.
min = minLstEl(el, acc[1, 1]):: El.
{idx, ({max, min}:: PairEl)}:: {int, PairEl}
}.
{result[1][0]["idx"], result[1][1]["idx"]}
}
fn-minmax8 = function:: Pair
{
input = {25, 37, 1, 1, 6, 5, 19, 3, 37, 20}:: [int].
init = {0, {{1000, 1000}, {1000, 1000}}}:: {int, PairEl}.
result = loop fold(input->val:: int, init->acc):: {int, PairEl}
{
el = {acc[0], val}:: El.
idx = acc[0]+1 :: int.
minF = minEl(el, acc[1, 0]) :: El.
minL = minLstEl(el, acc[1, 1]):: El.
{idx, {minF, minL}}:: {int, PairEl}
}.
{result[1, 0, "idx"], result[1, 1, "idx"]}
}
fn-minmax9 = function:: Pair
{
input = {25, 37, 1, 1, 6, 5, 19, 3, 37, 20}:: [int].
init = {0, {{0, 0}, {0, 0}}}:: {int, PairEl}.
result = loop fold(input->val:: int, init->acc):: {int, PairEl}
{
el = {acc[0], val}:: El.
idx = acc[0]+1 :: int.
maxF = maxEl(el, acc[1, 0]) :: El.
maxL = maxLstEl(el, acc[1, 1]):: El.
{idx, {maxF, maxL}}:: {int, PairEl}
}.
{result[1, 0, "idx"], result[1, 1, "idx"]}
}
fn-minmax10 = function:: int
{
input = {25, 37, 1, 1, 6, 5, 19, 3, 37, 20}:: [int].
init = {0, {{1000, 1000}, {0, 0}}}:: {int, PairEl}.
result = loop fold(input->val:: int, init->acc):: {int, PairEl}
{
el = {acc[0], val}:: El.
idx = acc[0]+1 :: int.
minF = minEl(el, acc[1, 0]) :: El.
maxF = maxEl(el, acc[1, 1]):: El.
{idx, {minF, maxF}}:: {int, PairEl}
}.
(minElIdx(result[1, 0], result[1, 1])::El)["idx"]
}
fn-minmax11 = function:: int
{
input = {25, 37, 1, 1, 6, 5, 19, 3, 37, 20}:: [int].
ctxInit = {0, {{1000, 1000}, {0, 0}}}:: {int, PairEl}.
result = loop fold(input-> val:: int, ctxInit->ctx):: {int, PairEl}
{
elCur = {ctx[0], val}:: El.
elMinL = minLstEl(elCur, ctx[1, 0]):: El.
elMaxL = maxLstEl(elCur, ctx[1, 1]):: El.
{ctx[0]+1, {elMinL, elMaxL}}:: {int, PairEl}
}.
(maxElIdx(result[1, 0], result[1, 1])::El)["idx"]
}
fn-minmax12 = function:: int
{
input = {-8, 16, 3, -5, 2, -11, -905, -54}:: [int].
result = loop fold(input->val:: int, {1000, false} -> state):: {minPstv:: int, flagPstvFound:: bool}
{
if (val > 0):: int { {min(val, state["minPstv"]), true}:: {minPstv:: int, flagPstvFound:: bool} } else { state }
}.
if (result["flagPstvFound"])::int {result["minPstv"]} else { 0 }
}
OptionalVal = type {flag:: bool, val:: int}.
fn-minmax13 = function::int
{
input = {-8, 16, 3, -5, 2, -11, -905, -54}:: [int].
result = loop fold(input->x:: int, {false, -1000} -> state):: OptionalVal
{
if (x % 2 == 1):: OptionalVal {{true, max(x, state["val"])}:: OptionalVal } else { state }
}.
result["val"]
}
OptionalEl = type {el:: El, exists:: bool}.
fn-minmax14 = function:: El
{
input = { -8, 16, 3, -5, 2, -11, -905, -54 }:: [int].
threshold = 2:: int.
result = loop fold (input -> x:: int, {0, {el = { 0, 1000 }, exists = false }} -> result):: {int , OptionalEl}
{
xEl = {result[0], x}:: El.
if(x > threshold):: {int , OptionalEl}
{
{ result[0] + 1,
{ el = minEl(xEl, result[1, "el"]), exists = true }
}:: {int , OptionalEl} }
else
{
result : {[0]= result[0] + 1}
}
}.
if (result[1, "exists"]):: El { result[1, "el"] } else { {0, 0}:: El }
}

Event Timeline