查找列表中最常出现的元素的数量

查找列表中最常出现的元素的数量

第一的:这类似于“计算列表中每种大小的元素数量“ 和 ”列表中最大元素的位置“但这并不一样。”

代码+问题:

\documentclass{beamer}

\usepackage{lmodern}

\def\elevA{6}
\def\elevB{0}
\def\elevC{0}
\def\elevD{3}
\def\elevE{0}
\def\elevF{2}
\def\elevG{1}
\def\elevH{1}
\def\elevI{1}
\def\elevJ{5}
\def\elevK{0}
\def\elevL{3}
\def\elevM{7}
\def\elevN{3}
\def\elevO{1}
\def\elevP{1}
\def\elevQ{0}
\def\elevR{0}
\def\elevS{0}
\def\elevT{2}
\def\elevU{1}
\def\elevV{2}
\def\elevW{0}
\def\elevX{2}
\def\elevY{1}
\def\elevZ{1}
\def\elevAa{4}
\def\elevAb{6}
\def\elevAc{1}
\def\elevAd{1}
\def\elevAe{6}
\def\elevAf{2}
\def\elevAg{0}
\def\elevAh{3}
\def\elevAi{2}
\def\elevAj{1}
\def\elevAk{0}
\def\elevAl{0}
\def\elevAm{0}
\def\elevAn{0}
\def\elevAo{0}
\def\elevAp{4}

\begin{document}

\begin{frame}
What number among the ones defined in the input occurs most often?\\[\baselineskip]
(I know that the answer is `$0$' in this case but how can I get \LaTeX{} to calculate it?)
\end{frame}

\end{document}

输出

笔记:所有值均通过该\def方法预先定义。

答案1

对您上次遗憾地未接受的答案进行一点补充:-)

\documentclass{beamer}

\usepackage[danish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{array}

\def\most{0}
\def\mostnum{0}

\def\zz\ignorespaces#1{%
\expandafter\xdef\csname zz#1\endcsname{%
\expandafter\ifx\csname zz#1\endcsname\relax
1%
\else
\the\numexpr\csname zz#1\endcsname+1\relax
\fi}%
\ifnum\mostnum<\csname zz#1\endcsname
  \gdef\most{#1}%
   \global\expandafter\let\expandafter\mostnum\csname zz#1\endcsname
\fi
#1}

\begin{document}

\def\elevA{6}
\def\elevB{0}
\def\elevC{0}
\def\elevD{3}
\def\elevE{0}
\def\elevF{2}
\def\elevG{1}
\def\elevH{1}
\def\elevI{1}
\def\elevJ{5}
\def\elevK{0}
\def\elevL{3}
\def\elevM{7}
\def\elevN{3}
\def\elevO{1}
\def\elevP{1}
\def\elevQ{0}
\def\elevR{0}
\def\elevS{0}
\def\elevT{2}
\def\elevU{1}
\def\elevV{2}
\def\elevW{0}
\def\elevX{2}
\def\elevY{1}
\def\elevZ{1}
\def\elevAa{4}
\def\elevAb{6}
\def\elevAc{1}
\def\elevAd{1}
\def\elevAe{6}
\def\elevAf{2}
\def\elevAg{0}
\def\elevAh{3}
\def\elevAi{2}
\def\elevAj{1}
\def\elevAk{0}
\def\elevAl{0}
\def\elevAm{0}
\def\elevAn{0}
\def\elevAo{0}
\def\elevAp{4}

\begin{frame}

\begin{table}
\centering
\small
  \begin{tabular}{
    *{14}{>\zz c}
  }
   \toprule
    \elevA  & \elevB  & \elevC  & \elevD  & \elevE  & \elevF  & \elevG  &
    \elevH  & \elevI  & \elevJ  & \elevK  & \elevL  & \elevM  & \elevN    \\[0.5ex]
    \elevO  & \elevP  & \elevQ  & \elevR  & \elevS  & \elevT  & \elevU  &
    \elevV  & \elevW  & \elevX  & \elevY  & \elevZ  & \elevAa & \elevAb   \\[0.5ex]
    \elevAc & \elevAd & \elevAe & \elevAf & \elevAg & \elevAh & \elevAi &
    \elevAj & \elevAk & \elevAl & \elevAm & \elevAn & \elevAo & \elevAp   \\
   \bottomrule
  \end{tabular}
\end{table}

{\count0=0
\loop
\the\count0:\csname zz\the\count0\endcsname\endgraf
\ifnum\count0<8
\advance\count0 1
\repeat
}


\most\ occurred the most times (\mostnum)
\end{frame}

\end{document}

答案2

假设可能值从09。可以轻松扩展以适应可能值未知的情况。但以最通用为目标毫无意义。

帖子中链接的早期问题的上下文包含所有必要的结构,但我们必须在这里从头开始做一些事情。

\documentclass{beamer}

\usepackage{lmodern}

\def\elevA{6}
\def\elevB{0}
\def\elevC{0}
\def\elevD{3}
\def\elevE{0}
\def\elevF{2}
\def\elevG{1}
\def\elevH{1}
\def\elevI{1}
\def\elevJ{5}
\def\elevK{0}
\def\elevL{3}
\def\elevM{7}
\def\elevN{3}
\def\elevO{1}
\def\elevP{1}
\def\elevQ{0}
\def\elevR{0}
\def\elevS{0}
\def\elevT{2}
\def\elevU{1}
\def\elevV{2}
\def\elevW{0}
\def\elevX{2}
\def\elevY{1}
\def\elevZ{1}
\def\elevAa{4}
\def\elevAb{6}
\def\elevAc{1}
\def\elevAd{1}
\def\elevAe{6}
\def\elevAf{2}
\def\elevAg{0}
\def\elevAh{3}
\def\elevAi{2}
\def\elevAj{1}
\def\elevAk{0}
\def\elevAl{0}
\def\elevAm{0}
\def\elevAn{0}
\def\elevAo{0}
\def\elevAp{4}

\makeatletter
\@tfor\svend@x:=0123456789\do{\@namedef{svend@\svend@x}{0}}

\@tfor\svend@x:=\elevA
\elevB
\elevC
\elevD
\elevE
\elevF
\elevG
\elevH
\elevI
\elevJ
\elevK
\elevL
\elevM
\elevN
\elevO
\elevP
\elevQ
\elevR
\elevS
\elevT
\elevU
\elevV
\elevW
\elevX
\elevY
\elevZ
\elevAa
\elevAb
\elevAc
\elevAd
\elevAe
\elevAf
\elevAg
\elevAh
\elevAi
\elevAj
\elevAk
\elevAl
\elevAm
\elevAn
\elevAo
\elevAp\do{\expandafter\edef\csname svend@\svend@x\endcsname
            {\the\numexpr\csname svend@\svend@x\endcsname+\@ne}}

\def\svend@Max {-1}
\def\svend@Min {"7FFFFFF}

\@tfor\svend@x:=0123456789\do{%
  \ifnum\csname svend@\svend@x\endcsname>\z@
      \ifnum\csname svend@\svend@x\endcsname>\svend@Max\relax
        \edef\MostFrequent {\svend@x}%
        \edef\svend@Max {\csname svend@\svend@x\endcsname }%
      \fi
      \ifnum\csname svend@\svend@x\endcsname<\svend@Min\relax
        \edef\LessFrequent {\svend@x}%
        \edef\svend@Min {\csname svend@\svend@x\endcsname }%
      \fi
  \fi
}

\makeatother



\begin{document}

\begin{frame}
What number among the ones defined in the input occurs most often?\\
(I got \LaTeX{} to compute that the smallest among them is `$\MostFrequent$')\\[\baselineskip]

And what number among the ones defined occurs less often ?\\
(I got \LaTeX{} to compute that the smallest among them is `$\LessFrequent$')

\end{frame}

\end{document}

在此处输入图片描述

答案3

这是扩展版本,用于计算最后一次\countappearances声明后出现次数最多的数字。

\documentclass{beamer}

\usepackage[danish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{xparse}

\ExplSyntaxOn
\prop_new:N \g_svend_elev_prop
\prop_gput:Nnn \g_svend_elev_prop {A} {6}
\prop_gput:Nnn \g_svend_elev_prop {B} {0}
\prop_gput:Nnn \g_svend_elev_prop {C} {0}
\prop_gput:Nnn \g_svend_elev_prop {D} {3}
\prop_gput:Nnn \g_svend_elev_prop {E} {0}
\prop_gput:Nnn \g_svend_elev_prop {F} {2}
\prop_gput:Nnn \g_svend_elev_prop {G} {1}
\prop_gput:Nnn \g_svend_elev_prop {H} {1}
\prop_gput:Nnn \g_svend_elev_prop {I} {1}
\prop_gput:Nnn \g_svend_elev_prop {J} {5}
\prop_gput:Nnn \g_svend_elev_prop {K} {0}
\prop_gput:Nnn \g_svend_elev_prop {L} {3}
\prop_gput:Nnn \g_svend_elev_prop {M} {7}
\prop_gput:Nnn \g_svend_elev_prop {N} {3}
\prop_gput:Nnn \g_svend_elev_prop {O} {1}
\prop_gput:Nnn \g_svend_elev_prop {P} {1}
\prop_gput:Nnn \g_svend_elev_prop {Q} {0}
\prop_gput:Nnn \g_svend_elev_prop {R} {0}
\prop_gput:Nnn \g_svend_elev_prop {S} {0}
\prop_gput:Nnn \g_svend_elev_prop {T} {2}
\prop_gput:Nnn \g_svend_elev_prop {U} {1}
\prop_gput:Nnn \g_svend_elev_prop {V} {2}
\prop_gput:Nnn \g_svend_elev_prop {W} {0}
\prop_gput:Nnn \g_svend_elev_prop {X} {2}
\prop_gput:Nnn \g_svend_elev_prop {Y} {1}
\prop_gput:Nnn \g_svend_elev_prop {Z} {1}
\prop_gput:Nnn \g_svend_elev_prop {Aa} {4}
\prop_gput:Nnn \g_svend_elev_prop {Ab} {6}
\prop_gput:Nnn \g_svend_elev_prop {Ac} {1}
\prop_gput:Nnn \g_svend_elev_prop {Ad} {1}
\prop_gput:Nnn \g_svend_elev_prop {Ae} {6}
\prop_gput:Nnn \g_svend_elev_prop {Af} {2}
\prop_gput:Nnn \g_svend_elev_prop {Ag} {0}
\prop_gput:Nnn \g_svend_elev_prop {Ah} {3}
\prop_gput:Nnn \g_svend_elev_prop {Ai} {2}
\prop_gput:Nnn \g_svend_elev_prop {Aj} {1}
\prop_gput:Nnn \g_svend_elev_prop {Ak} {0}
\prop_gput:Nnn \g_svend_elev_prop {Al} {0}
\prop_gput:Nnn \g_svend_elev_prop {Am} {0}
\prop_gput:Nnn \g_svend_elev_prop {An} {0}
\prop_gput:Nnn \g_svend_elev_prop {Ao} {0}
\prop_gput:Nnn \g_svend_elev_prop {Ap} {4}

\prop_new:N \g_svend_count_prop
\prop_new:N \g_svend_count_zero_prop
\prop_gput:Nnn \g_svend_count_zero_prop { 0 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 1 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 2 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 3 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 4 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 5 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 6 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 7 } { 0 }
\prop_gset_eq:NN \g_svend_count_prop \g_svend_count_zero_prop

\tl_new:N \l_svend_number_tl
\tl_new:N \l_svend_count_tl
\tl_new:N \l_svend_mostnum_tl
\tl_new:N \l_svend_mostcount_tl

\DeclareExpandableDocumentCommand{\elev}{m}
 {
  \svend_get_item:n { #1 }
 }

\NewDocumentCommand{\countappearances}{}
 {
  % now \elev will also count
  \cs_set_eq:NN \elev \svend_get_item_count:n
  % reinitialize the counter property list
  \prop_set_eq:NN \g_svend_count_prop \g_svend_count_zero_prop
 }

\NewDocumentCommand{\getmost}{m}
 {
  \tl_set:Nn \l_svend_mostnum_tl { 0 }
  \tl_set:Nx \l_svend_mostcount_tl { \prop_item:Nn \g_svend_count_prop { 0 } }
  \int_step_inline:nnnn { 1 } { 1 } { 7 }
   {
    \int_compare:xT { \prop_item:Nn \g_svend_count_prop { ##1 } > \l_svend_mostcount_tl }
     {
      \tl_set:Nn \l_svend_mostnum_tl { ##1 }
      \tl_set:Nx \l_svend_mostcount_tl { \prop_item:Nn \g_svend_count_prop { ##1 } }
     }
   }
  \tl_set_eq:NN \mostnum \l_svend_mostnum_tl
  \tl_set_eq:NN \mostcount \l_svend_mostcount_tl
  #1
 }

\cs_generate_variant:Nn \int_compare:nT { x }

\DeclareExpandableDocumentCommand{\HowMany}{m}
 {
  \prop_item:Nn \g_svend_count_prop { #1 }
 }

\cs_new:Npn \svend_get_item:n #1
 {
  \prop_item:Nn \g_svend_elev_prop { #1 }
 }

\cs_new_protected:Npn \svend_get_item_count:n #1
 {
  \tl_set:Nx \l_svend_number_tl { \svend_get_item:n { #1 } }
  % print the entry
  \tl_use:N \l_svend_number_tl
  % get the current count
  \tl_set:Nx \l_svend_count_tl
   {
    \prop_item:NV \g_svend_count_prop \l_svend_number_tl
   }
  % advance the count by 1
  \tl_set:Nx \l_svend_count_tl { \int_to_arabic:n { \l_svend_count_tl + 1 } }
  % update the property
  \prop_gput:NVV \g_svend_count_prop \l_svend_number_tl \l_svend_count_tl
 }

\cs_generate_variant:Nn \prop_item:Nn { NV }
\cs_generate_variant:Nn \prop_gput:Nnn { NVV }

\ExplSyntaxOff

\begin{document}

\begin{frame}

\begin{table}
\centering
\small
\countappearances

\begin{tabular}{*{14}{c}}
\toprule
\elev{A}  & \elev{B}  & \elev{C}  & \elev{D}  & \elev{E}  & \elev{F}  & \elev{G}  &
\elev{H}  & \elev{I}  & \elev{J}  & \elev{K}  & \elev{L}  & \elev{M}  & \elev{N}    \\[0.5ex]
\elev{O}  & \elev{P}  & \elev{Q}  & \elev{R}  & \elev{S}  & \elev{T}  & \elev{U}  &
\elev{V}  & \elev{W}  & \elev{X}  & \elev{Y}  & \elev{Z}  & \elev{Aa} & \elev{Ab}   \\[0.5ex]
\elev{Ac} & \elev{Ad} & \elev{Ae} & \elev{Af} & \elev{Ag} & \elev{Ah} & \elev{Ai} &
\elev{Aj} & \elev{Ak} & \elev{Al} & \elev{Am} & \elev{An} & \elev{Ao} & \elev{Ap}   \\
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{*{8}{c}}
\toprule
\multicolumn{8}{c}{How many} \\
\midrule
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\HowMany{0} &
\HowMany{1} &
\HowMany{2} &
\HowMany{3} &
\HowMany{4} &
\HowMany{5} &
\HowMany{6} &
\HowMany{7} \\
\bottomrule
\end{tabular}

\bigskip

\getmost{%
  The number that appeared the most is \mostnum;
  it appeared \mostcount\ times.}

\end{table}
\end{frame}

\end{document}

该宏\getmost将您要打印的文本作为参数。如果您使用\getmost{},则不会打印任何文本,但无论如何您都可以使用\mostnum\mostcount。请注意,这是出现的组的本地内容\countappearances。如果需要,将其设为全局并不困难。

在此处输入图片描述

相关内容