Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
|||
# Input: an array representing a stack, with .[-1] being its top.
|
||||
# Output: the updated array after applying `op`
|
||||
def rpn(op):
|
||||
def two: .[-2:];
|
||||
def update($x): (.[:-2] + [$x]);
|
||||
if length<=1 then .
|
||||
elif op == "+" then update(two | add)
|
||||
elif op == "*" then update(two | (.[0] * .[1]))
|
||||
elif op == "/" then update(two | (.[0] / .[1]))
|
||||
elif op == "-" then update(two | (.[0] - .[1]))
|
||||
elif op == "^" then update(two | (pow(.[0]; .[1])))
|
||||
else ("ignoring unrecognized op \(op)" | debug) as $debug | .
|
||||
end;
|
||||
|
||||
def eval:
|
||||
foreach .[] as $item ([];
|
||||
if ($item | type) == "number" then . + [$item]
|
||||
else rpn($item)
|
||||
end;
|
||||
"\($item) => \(.)" ) ;
|
||||
|
||||
"3 4 2 * 1 5 - 2 3 ^ ^ / +"
|
||||
| split(" ") | map( (try tonumber) // .)
|
||||
| eval
|
||||
Loading…
Add table
Add a link
Reference in a new issue