Last active
June 12, 2021 18:40
-
-
Save line-o/c4868b2f8e003c73eb671daf34663b79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
(: | |
two possible solutions to @cassidoo's interview question from: | |
https://buttondown.email/cassidoo/archive/it-is-absolutely-imperative-that-every-human/ | |
:) | |
declare function local:product ($result, $next) { $result * $next }; | |
declare function local:grouping ($a, $b, $c) { | |
let $grouped := | |
for $i in ($a, $b, $c) | |
group by $n := $i | |
return | |
if (count($i) = 1) | |
then $n | |
else 1 | |
return | |
fold-left($grouped, 1, local:product#2) | |
}; | |
declare function local:reducer ($accu, $next) { | |
let $seen := $accu?2 | |
let $result := $accu?1 | |
return [ | |
if ($next = $seen) | |
then $result div $next | |
else $result * $next | |
, | |
($seen, $next) | |
] | |
}; | |
declare function local:reduction ($a, $b, $c) { | |
let $s := ($a, $b, $c) | |
return | |
if ($s != $a) | |
then fold-left($s, [1, ()], local:reducer#2)?1 | |
else 1 | |
}; | |
map { | |
"expected":(6,4,1), | |
"grouping": ( | |
local:grouping(1,2,3), | |
local:grouping(6,6,4), | |
local:grouping(7,7,7) | |
), | |
"reduction": ( | |
local:reduction(1,2,3), | |
local:reduction(6,6,4), | |
local:reduction(7,7,7) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment