expl3 (LaTeX3 programming layer), 65 bytes
The following defines a function that prints the result to the terminal (unfortunately expl3
has very verbose function names):
\def\1#1{\fp_show:n{1/(\clist_map_function:nN{#1}\2)}}\def\2{+1/}
A complete script which can be run from the terminal including all the test cases as well as the setup to enter expl3
:
\RequirePackage{expl3}\ExplSyntaxOn
\def\1#1{\fp_show:n{1/(\clist_map_function:nN{#1}\2)}}\def\2{+1/}
\1{1, 1}
\1{1, 1, 1}
\1{4, 6, 3}
\1{20, 14, 18, 8, 2, 12}
\1{10, 10, 20, 30, 40, 50, 60, 70, 80, 90}
\stop
If run with pdflatex <filename>
the following is the console output:
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./cg_resistance.tex
LaTeX2e <2018-12-01>
(/usr/local/texlive/2019/texmf-dist/tex/latex/unravel/unravel.sty
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3backend/l3backend-pdfmode.def))
(/usr/local/texlive/2019/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
(/usr/local/texlive/2019/texmf-dist/tex/generic/gtl/gtl.sty))
> 1/(\clist_map_function:nN {1,1}\2)=0.5.
<recently read> }
l.3 \1{1, 1}
?
> 1/(\clist_map_function:nN {1,1,1}\2)=0.3333333333333333.
<recently read> }
l.4 \1{1, 1, 1}
?
> 1/(\clist_map_function:nN {4,6,3}\2)=1.333333333333333.
<recently read> }
l.5 \1{4, 6, 3}
?
> 1/(\clist_map_function:nN {20,14,18,8,2,12}\2)=1.129538323621694.
<recently read> }
l.6 \1{20, 14, 18, 8, 2, 12}
?
> 1/(\clist_map_function:nN
{10,10,20,30,40,50,60,70,80,90}\2)=2.611669603067675.
<recently read> }
l.7 \1{10, 10, 20, 30, 40, 50, 60, 70, 80, 90}
?
)
No pages of output.
Transcript written on cg_resistance.log.
Explanation
\fp_show:n
: evaluates its argument as a floating point expression and prints the result on the terminal, every expandable macro is expanded during that process.
\clist_map_function:nN
: takes two arguments, a comma separated list and a function/macro, if called like \clist_map_function:nN { l1, l2, l3 } \foo
it expands to something like \foo{l1}\foo{l2}\foo{l3}
. In our case instead of \foo
the macro \2
is used, which expands to +1/
so that the expression expands to +1/{l1}+1/{l2}+1/{l3}
6
There are a few other challenges that refer to the harmonic mean (1 2 3) but I don't think there is a duplicate. In line with what flawr suggested, I think this challenge body should have that phrase listed somewhere so we can close a future dupe more easily.
– FryAmTheEggman – 2019-09-11T15:17:17.173