From 5ade41113e14eb41ea9c4c8514bc2667a832dabf Mon Sep 17 00:00:00 2001 From: krahets Date: Thu, 4 Jan 2024 04:56:53 +0800 Subject: [PATCH] build --- .../basic_data_types.md | 52 +++++++++---------- .../iteration_and_recursion.md | 32 ++++++++++++ overrides/stylesheets/extra.css | 34 +++++++++++- 3 files changed, 91 insertions(+), 27 deletions(-) diff --git a/docs-en/chapter_data_structure/basic_data_types.md b/docs-en/chapter_data_structure/basic_data_types.md index c93d6f0d6..69803de80 100644 --- a/docs-en/chapter_data_structure/basic_data_types.md +++ b/docs-en/chapter_data_structure/basic_data_types.md @@ -2,27 +2,27 @@ comments: true --- -# 3.2   Fundamental Data Types +# 3.2   Basic Data Types -When we think of data in computers, we imagine various forms like text, images, videos, voice, 3D models, etc. Despite their different organizational forms, they are all composed of various fundamental data types. +When discussing data in computers, various forms like text, images, videos, voice and 3D models comes to mind. Despite their different organizational forms, they are all composed of various basic data types. -**Fundamental data types are those that the CPU can directly operate on** and are directly used in algorithms, mainly including the following. +**Basic data types are those that the CPU can directly operate on** and are directly used in algorithms, mainly including the following. - Integer types: `byte`, `short`, `int`, `long`. - Floating-point types: `float`, `double`, used to represent decimals. - Character type: `char`, used to represent letters, punctuation, and even emojis in various languages. -- Boolean type: `bool`, used for "yes" or "no" decisions. +- Boolean type: `bool`, used to represent "yes" or "no" decisions. -**Fundamental data types are stored in computers in binary form**. One binary digit is equal to 1 bit. In most modern operating systems, 1 byte consists of 8 bits. +**Basic data types are stored in computers in binary form**. One binary digit is 1 bit. In most modern operating systems, 1 byte consists of 8 bits. -The range of values for fundamental data types depends on the size of the space they occupy. Below, we take Java as an example. +The range of values for basic data types depends on the size of the space they occupy. Below, we take Java as an example. - The integer type `byte` occupies 1 byte = 8 bits and can represent $2^8$ numbers. - The integer type `int` occupies 4 bytes = 32 bits and can represent $2^{32}$ numbers. -The following table lists the space occupied, value range, and default values of various fundamental data types in Java. This table does not need to be memorized, but understood roughly and referred to when needed. +The following table lists the space occupied, value range, and default values of various basic data types in Java. While memorizing this table isn't necessary, having a general understanding of it and referencing it when required is recommended. -

Table 3-1   Space Occupied and Value Range of Fundamental Data Types

+

Table 3-1   Space Occupied and Value Range of Basic Data Types

@@ -39,36 +39,36 @@ The following table lists the space occupied, value range, and default values of
-Please note that the above table is specific to Java's fundamental data types. Each programming language has its own data type definitions, and their space occupied, value ranges, and default values may differ. +Please note that the above table is specific to Java's basic data types. Every programming language has its own data type definitions, which might differ in space occupied, value ranges, and default values. - In Python, the integer type `int` can be of any size, limited only by available memory; the floating-point `float` is double precision 64-bit; there is no `char` type, as a single character is actually a string `str` of length 1. -- C and C++ do not specify the size of fundamental data types, which varies with implementation and platform. The above table follows the LP64 [data model](https://en.cppreference.com/w/cpp/language/types#Properties), used for Unix 64-bit operating systems including Linux and macOS. +- C and C++ do not specify the size of basic data types, it varies with implementation and platform. The above table follows the LP64 [data model](https://en.cppreference.com/w/cpp/language/types#Properties), used for Unix 64-bit operating systems including Linux and macOS. - The size of `char` in C and C++ is 1 byte, while in most programming languages, it depends on the specific character encoding method, as detailed in the "Character Encoding" chapter. - Even though representing a boolean only requires 1 bit (0 or 1), it is usually stored in memory as 1 byte. This is because modern computer CPUs typically use 1 byte as the smallest addressable memory unit. -So, what is the connection between fundamental data types and data structures? We know that data structures are ways to organize and store data in computers. The focus here is on "structure" rather than "data". +So, what is the connection between basic data types and data structures? We know that data structures are ways to organize and store data in computers. The focus here is on "structure" rather than "data". -If we want to represent "a row of numbers", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and order of numbers, but whether the stored content is an integer `int`, a decimal `float`, or a character `char`, is irrelevant to the "data structure". +If we want to represent "a row of numbers", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and the ordering of the numbers, but whether the stored content is an integer `int`, a decimal `float`, or a character `char`, is irrelevant to the "data structure". -In other words, **fundamental data types provide the "content type" of data, while data structures provide the "way of organizing" data**. For example, in the following code, we use the same data structure (array) to store and represent different fundamental data types, including `int`, `float`, `char`, `bool`, etc. +In other words, **basic data types provide the "content type" of data, while data structures provide the "way of organizing" data**. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including `int`, `float`, `char`, `bool`, etc. === "Python" ```python title="" - # Using various fundamental data types to initialize arrays + # Using various basic data types to initialize arrays numbers: list[int] = [0] * 5 decimals: list[float] = [0.0] * 5 # Python's characters are actually strings of length 1 characters: list[str] = ['0'] * 5 bools: list[bool] = [False] * 5 - # Python's lists can freely store various fundamental data types and object references + # Python's lists can freely store various basic data types and object references data = [0, 0.0, 'a', False, ListNode(0)] ``` === "C++" ```cpp title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays int numbers[5]; float decimals[5]; char characters[5]; @@ -78,7 +78,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "Java" ```java title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays int[] numbers = new int[5]; float[] decimals = new float[5]; char[] characters = new char[5]; @@ -88,7 +88,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "C#" ```csharp title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays int[] numbers = new int[5]; float[] decimals = new float[5]; char[] characters = new char[5]; @@ -98,7 +98,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "Go" ```go title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays var numbers = [5]int{} var decimals = [5]float64{} var characters = [5]byte{} @@ -108,7 +108,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "Swift" ```swift title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays let numbers = Array(repeating: Int(), count: 5) let decimals = Array(repeating: Double(), count: 5) let characters = Array(repeating: Character("a"), count: 5) @@ -118,14 +118,14 @@ In other words, **fundamental data types provide the "content type" of data, whi === "JS" ```javascript title="" - // JavaScript's arrays can freely store various fundamental data types and objects + // JavaScript's arrays can freely store various basic data types and objects const array = [0, 0.0, 'a', false]; ``` === "TS" ```typescript title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays const numbers: number[] = []; const characters: string[] = []; const bools: boolean[] = []; @@ -134,7 +134,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "Dart" ```dart title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays List numbers = List.filled(5, 0); List decimals = List.filled(5, 0.0); List characters = List.filled(5, 'a'); @@ -144,7 +144,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "Rust" ```rust title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays let numbers: Vec = vec![0; 5]; let decimals: Vec = vec![0.0, 5]; let characters: Vec = vec!['0'; 5]; @@ -154,7 +154,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "C" ```c title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays int numbers[10]; float decimals[10]; char characters[10]; @@ -164,7 +164,7 @@ In other words, **fundamental data types provide the "content type" of data, whi === "Zig" ```zig title="" - // Using various fundamental data types to initialize arrays + // Using various basic data types to initialize arrays var numbers: [5]i32 = undefined; var decimals: [5]f32 = undefined; var characters: [5]u8 = undefined; diff --git a/docs/chapter_computational_complexity/iteration_and_recursion.md b/docs/chapter_computational_complexity/iteration_and_recursion.md index 99ca5c63a..fb88b4914 100644 --- a/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -182,6 +182,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 图 2-1 是该求和函数的流程框图。 ![求和函数的流程框图](iteration_and_recursion.assets/iteration.png){ class="animation-figure" } @@ -388,6 +392,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + **`while` 循环比 `for` 循环的自由度更高**。在 `while` 循环中,我们可以自由地设计条件变量的初始化和更新步骤。 例如在以下代码中,条件变量 $i$ 每轮进行两次更新,这种情况就不太方便用 `for` 循环实现: @@ -607,6 +615,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 总的来说,**`for` 循环的代码更加紧凑,`while` 循环更加灵活**,两者都可以实现迭代结构。选择使用哪一个应该根据特定问题的需求来决定。 ### 3.   嵌套循环 @@ -821,6 +833,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 图 2-2 是该嵌套循环的流程框图。 ![嵌套循环的流程框图](iteration_and_recursion.assets/nested_iteration.png){ class="animation-figure" } @@ -1026,6 +1042,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 图 2-3 展示了该函数的递归过程。 ![求和函数的递归过程](iteration_and_recursion.assets/recursion_sum.png){ class="animation-figure" } @@ -1222,6 +1242,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 尾递归的执行过程如图 2-5 所示。对比普通递归和尾递归,两者的求和操作的执行点是不同的。 - **普通递归**:求和操作是在“归”的过程中执行的,每层返回后都要再执行一次求和操作。 @@ -1430,6 +1454,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 观察以上代码,我们在函数内递归调用了两个函数,**这意味着从一个调用产生了两个调用分支**。如图 2-6 所示,这样不断递归调用下去,最终将产生一棵层数为 $n$ 的「递归树 recursion tree」。 ![斐波那契数列的递归树](iteration_and_recursion.assets/recursion_tree.png){ class="animation-figure" } @@ -1748,6 +1776,10 @@ comments: true } ``` +??? pythontutor "分步调试" + + + 观察以上代码,当递归转化为迭代后,代码变得更加复杂了。尽管迭代和递归在很多情况下可以互相转化,但不一定值得这样做,有以下两点原因。 - 转化后的代码可能更加难以理解,可读性更差。 diff --git a/overrides/stylesheets/extra.css b/overrides/stylesheets/extra.css index 9737651aa..f170e40fa 100644 --- a/overrides/stylesheets/extra.css +++ b/overrides/stylesheets/extra.css @@ -9,6 +9,7 @@ --md-default-bg-color: #ffffff; --md-code-fg-color: #1d1d20; + --md-code-bg-color: #f5f5f5; --md-accent-fg-color: #999; @@ -19,6 +20,8 @@ --md-typeset-btn-color: #55aea6; --md-typeset-btn-hover-color: #52bbb1; + + --md-admonition-icon--pythontutor: url('data:image/svg+xml;charset=utf-8,'); } [data-md-color-scheme="slate"] { @@ -28,8 +31,8 @@ --md-default-fg-color: #adbac7; --md-default-bg-color: #22272e; - --md-code-bg-color: #1d2126; --md-code-fg-color: #adbac7; + --md-code-bg-color: #1d2126; --md-accent-fg-color: #aaa; @@ -106,6 +109,14 @@ text-transform: none; } +.md-typeset code { + border-radius: 0.2rem; +} + +.highlight span.filename { + font-weight: normal; +} + /* font-family setting for Win10 */ body { --md-text-font-family: -apple-system, BlinkMacSystemFont, @@ -163,3 +174,24 @@ body { height: auto; margin-right: 0.5em; /* Add some space between the SVG icon and the text */ } + +/* Admonition for python tutor */ +.md-typeset .admonition.pythontutor, +.md-typeset details.pythontutor { + border: none; +} + +.md-typeset .admonition:focus-within, .md-typeset details:focus-within { + box-shadow: var(--md-shadow-z1); +} + +.md-typeset .pythontutor > .admonition-title, +.md-typeset .pythontutor > summary { + background-color: var(--md-code-bg-color); +} +.md-typeset .pythontutor > .admonition-title::before, +.md-typeset .pythontutor > summary::before { + background-color: rgb(55, 118, 171); + -webkit-mask-image: var(--md-admonition-icon--pythontutor); + mask-image: var(--md-admonition-icon--pythontutor); +}