Skip to content

Commit

Permalink
Updated chapter of list - length counting (EN)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinyu95 committed Mar 13, 2024
1 parent cf53b58 commit 5801636
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions others/appendix/list/list-en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ \subsection{Access}

\section{Basic operations}
\index{List!length}
From the definition, we can count the length recursively. the length of the empty list is 0, or it is 1 plus the length of the sub-list.
We may count the length of a list recursively: the length of the empty list is 0 or it is 1 plus the length of the sub-list.

\be
\begin{array}{rcl}
length\ [\ ] & = & 0 \\
length\ (x \cons xs) & = & 1 + length\ xs
\end{array}
\ee
\begin{lalign}
length\ [\ ] & = 0 \\
length\ (x \cons xs) & = 1 + length\ xs
\end{lalign}

We traverse the list to count the length, the performance is bound to $O(n)$, where $n$ is the number of elements. We use $|X|$ as the length of $X$ when the context is clear. To avoid repeatedly counting, we can persist the length in a variable, and update it when mutate (add or delete). Below is the iterative length counting:
As we traverse the list to count the length, the performance is bound to $O(n)$, where $n$ is the number of elements. Denote the length of the list $X$ by $|X|$ when the context is clear. To avoid repeatedly counting, we may persist the length in a variable and update it when mutate (add or delete). Below is the iterative length counting:

\begin{algorithmic}[1]
\Function{Length}{X}
Expand All @@ -130,7 +128,7 @@ \subsection{index}
\be
getAt\ i\ (x \cons xs) = \begin{cases}
i = 0: & x \\
i \neq 0: & getAt\ (i - 1)\ xs \\
i \neq 0: & getAt\ (i - 1)\ xs
\end{cases}
\ee

Expand Down
2 changes: 1 addition & 1 deletion others/preface/preface-zh-cn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ \subsection*{改进}
$n$个自然数$x_1, x_2, \dotsc , x_n$,如果存在小于$n$的可用数,必然存在某个$x_i$不在区间$[0, n)$\footnote{左开右闭区间$[a, b)$包括$a$但不包括$b$。}内。否则这些数一定是$0, 1, \dotsc, n - 1$的某个排列,这种情况下,最小的可用自然数是$n$

\be
\textit{minfree}(x_1, x_2, ..., x_n) \leq n
\textit{minfree}(x_1, x_2, \dotsc, x_n) \leq n
\label{eq:min-free}
\ee

Expand Down

0 comments on commit 5801636

Please sign in to comment.