diff --git a/algorithm/basic_algo/binary_search/index.html b/algorithm/basic_algo/binary_search/index.html index a499d64..275531d 100755 --- a/algorithm/basic_algo/binary_search/index.html +++ b/algorithm/basic_algo/binary_search/index.html @@ -1305,6 +1305,17 @@ + + @@ -1315,6 +1326,52 @@ + + + + @@ -1754,6 +1811,41 @@ + + + @@ -1784,6 +1876,98 @@

Binary Search

+

Introduction

+

Binary Search(二元搜尋法) 是分治法(Divide and Conquer)的一種應用,非常有效率,但只適用在單調(Monotonic)的序列或函數上,也就是說,必須是遞增或遞減的,或是你簡單理解成排序過(sorted)的,舉幾個例子:

+
    +
  1. [1, 2, 3, 4, 5]
  2. +
  3. ['a', 'b', 'c', 'd', 'e']
  4. +
  5. [32, 16, 8, 4, 2, 1]
  6. +
  7. \(f(x)=x^2, \ x \ge 0,\ [f(1), f(2), f(3), f(4), f(5)]\)
  8. +
+

函數 \(f(x)=x^2\)\(x \ge 0\) 時是遞增的:

+

+
1
+2
+3
+4
+5
+6
+7
+8
+9
def f(x):
+    return x ** 2
+
+
+xs = [1, 2, 3, 4, 5]
+print(xs)
+# [f(1), f(2), f(3), f(4), f(5)]
+ys = [f(x) for x in xs]
+print(ys)
+
+
Output
1
+2
[1, 2, 3, 4, 5]
+[1, 4, 9, 16, 25]
+
+

最後一個例子是想跟你強調,函數也可以用二元搜尋法,例如想要找第一個 \(f(x) \ge 10\) 的整數 \(x\),我們可以用二元搜尋法找到 \(x=4\)

+

Complexity

+

玩過猜數字嗎?你每猜一次,我會告訴你猜的數字是太大還是太小,讓你從 \([1, 100]\) 間猜一個數字,怎麼猜會最有效率?如果你第一次就猜 \(50\),我告訴你猜的數字太大,你就可以知道答案一定在 \([1, 49]\) 之間,這樣你就可以省下一半的時間,你可以再猜 \(25\),而我告訴你猜的數字太小,那麼答案一定在 \([26, 49]\) 之間,你又省下一半的時間,這就是二元搜尋法的精神。

+ + +

那最多要猜幾次呢?假設你真的很衰,猜到剩下 \([47, 48]\) 這兩個數字在選的時候又猜錯,你猜了 \(47\) 而我告訴你猜太小,那就只剩下 \(48\) 了,也就是區間 \([48, 48]\) 剩下一個元素,這是我們的終止條件。

+

考慮一般的情況,有 \(n\) 個數字,每次猜都可以把區間縮小一半,設我們要猜 \(k\) 次才能把區間縮小到剩下一個元素:

+
\[ +\frac{n}{2^k} = 1 +\]
+
\[ +\Rightarrow n = 2^k +\]
+
\[ +\Rightarrow \log_2 n = k +\]
+

那麼時間複雜度就是 \(\mathcal{O}(k) = \mathcal{O}(\log n)\)

+

Implementation

+

在寫程式之前,先來看動畫,有一個已排序過的串列 [1, 1, 4, 5, 7, 8, 9, 10, 11, 12],我們要找 4:

+ + +

再來看程式碼:

+
binary_search.py
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
def binary_search(lst, target):
+    left, right = 0, len(lst) - 1
+
+    while left <= right:
+        mid = (left + right) // 2
+        if lst[mid] == target:
+            return mid
+        elif lst[mid] < target:
+            left = mid + 1
+        else:
+            right = mid - 1
+
+    return -1
+
+nums = [1, 1, 4, 5, 7, 8, 9, 10, 11, 12]
+print(binary_search(nums, 4))
+
+
Output
2
+
diff --git a/algorithm/basic_algo/linear_search/index.html b/algorithm/basic_algo/linear_search/index.html index 1158dea..b63f321 100755 --- a/algorithm/basic_algo/linear_search/index.html +++ b/algorithm/basic_algo/linear_search/index.html @@ -1916,7 +1916,7 @@

Introduction這應該是最簡單的演算法之一,不過我們可以來複習上一章所學的時間複雜度。

Implementation

直接來看程式碼,定義 linear_search(lst, target) 函式,這個函式接受一個串列 lst 和一個目標值 target,回傳目標值在串列中的索引,如果目標值不存在就回傳 -1

-
 1
+
linear_search.py
 1
  2
  3
  4
diff --git a/algorithm/basic_algo/media/binary_search_1.png b/algorithm/basic_algo/media/binary_search_1.png
new file mode 100755
index 0000000..a211313
Binary files /dev/null and b/algorithm/basic_algo/media/binary_search_1.png differ
diff --git a/algorithm/basic_algo/media/binary_search_2.mp4 b/algorithm/basic_algo/media/binary_search_2.mp4
new file mode 100755
index 0000000..d285343
Binary files /dev/null and b/algorithm/basic_algo/media/binary_search_2.mp4 differ
diff --git a/algorithm/basic_algo/media/binary_search_3.mp4 b/algorithm/basic_algo/media/binary_search_3.mp4
new file mode 100755
index 0000000..0e9cb21
Binary files /dev/null and b/algorithm/basic_algo/media/binary_search_3.mp4 differ
diff --git a/algorithm/what_is_algo/index.html b/algorithm/what_is_algo/index.html
index 66de9ed..531082a 100755
--- a/algorithm/what_is_algo/index.html
+++ b/algorithm/what_is_algo/index.html
@@ -1265,6 +1265,15 @@
     
-

定義 \(f(n)=n^{2}+2n+3\) 來代表輸入 \(n\),會印出 hello, Algorithm! 的次數,

+

定義 \(f(n)=n^{2}+2n+3\) 來代表輸入 \(n\),會印出 hello, Algorithm! 的次數之間的關係。

\(f(2)=11\),也就是說當 \(n=2\) 時,我們的演算法會印出 hello, Algorithm! \(11\) 次。

-

這稱為時間複雜度(Time Complexity),而既然有時間複雜度,那當然也有空間複雜度(Space Complexity),這是指演算法執行時所需要的記憶體空間,例如: 陣列的大小、變數的數量等等。

+

\(f(n)\) 為這個演算法的時間複雜度(Time Complexity),而既然有時間複雜度,那當然也有空間複雜度(Space Complexity),這是指演算法執行時所需要的記憶體空間,例如: 陣列的大小、變數的數量等等。

再舉一個例子,反轉一個串列,提供了兩種解法:

 1
  2
@@ -2105,7 +2126,9 @@ 

Complexity\(n\) 的串列,儲存反轉後的串列,因此 \(f(n)\)\(g(n)\) 都是 \(n\)

reverse_list2 則是直接在原地(in-place)進行反轉,只使用 tmp 這個變數,所以 \(g(n)\)\(1\),又因為只需要反轉一半的串列,所以 \(f(n)\)\(\frac{n}{2}\)

這樣的比較方式,可以幫助我們選擇適合的演算法,並且評估演算法的好壞,但是這樣還稍嫌麻煩,有沒有更清楚且簡單的方法呢?

+

為接下來的內容先打個預防針,因為數學符號有點多,如果真的看不懂,可以直接跳到 Calculating Big O,但還是請你試著多看幾次,我相信你可以。

Big O Notation

+

Definition

這時候就要介紹大 O 符號(Big O Notation)了,它是漸近符號(Asymptotic Notation)的一種,用來描述一個演算法在「最壞情況」下的時間複雜度,所以說它是一種上界(Upper Bound),也通常指輸入資料的規模趨近無窮大時的行為,也就是 \(n\) 很大的時候。

例如: 當 \(n \to \infty\)\(f(n)=n^{2}+2n+3\) 的行為就是 \(\mathcal{O}(n^{2})\),因為當 \(n\) 很大的時候,\(n^{2}\) 會遠遠大於 \(2n+3\),所以可以忽略掉 \(2n+3\)

我們來看較為正式的定義:

@@ -2115,13 +2138,16 @@

Big O Notation\[\text{The function }f(n)=\mathcal{O}(g(n)) \text{ iff } \exists c,n_0 \text{ s.t } 0\le f(n) \le c*g(n),\forall n \ge n_0\]

先別急著關掉啦,先用中文解釋一下:

-

\(f(n)\)\(g(n)\) 是兩個函數,若且唯若存在常數 \(c\)\(n_0\),對於所有大於等於 \(n_0\)\(n\),使得 \(f(n)\) 小於等於 \(c*g(n)\)

-

其中 \(\mathcal{O}(g(n))\) 是一個函數集合,它包含了所有以 \(c*g(n)\) 為上界的函數,而 \(f(n)=\mathcal{O}(g(n))\)\(f(n) \in \mathcal{O}(g(n))\) 都是代表 \(f(n)\) 屬於 \(\mathcal{O}(g(n))\) 的一員,看你習慣用哪一種符號。

-

但覺得這樣還不夠清楚,我們直接來看例子:

-

定義 \(f(n)=2n+1,g(n)=n\),以及 \(c=3,h(n)=c*g(n)=3n\),來看 \(f(n)\) 是否屬於 \(\mathcal{O}(g(n))\),請看下圖:

+

\(f(n)\)\(g(n)\) 是兩個函數,若且唯若存在常數 \(c\)\(n_0\),對於所有大於等於 \(n_0\)\(n\),使得 \(f(n)\) 小於等於 \(c*g(n)\),那麼 \(f(n)\) 就屬於 \(\mathcal{O}(g(n))\)

+

其中 \(\mathcal{O}(g(n))\) 是一個函數集合,它包含了所有以 \(c*g(n)\) 為上界的函數,而 \(f(n)=\mathcal{O}(g(n))\)\(f(n) \in \mathcal{O}(g(n))\) 都是代表 \(f(n)\) 屬於 \(\mathcal{O}(g(n))\) 的一員,看你習慣用哪一種符號。

+

還是不太懂,那我用更白話的方式還有我們熟悉的 \(f(x)\) 來解釋:

+

有兩個函數 \(f(x)\)\(g(x)\) 在二維的座標平面上,我如果找的到一個倍數 \(c\) 和一條鉛直線 \(x=n_0\),使得 \(f(x)\) 在鉛直線右方(\(x \ge n_0\))\(f(x)\) 的圖形都在 \(c*g(x)\)下方,那麼 \(f(x)\) 就屬於 \(\mathcal{O}(g(x))\)

+

注意看強調的文字喔,白話的解釋應該能讓你在腦海中有畫面。

+

但覺得這樣還不夠清楚,我們直接來看例子,我相信你能看懂!

+

定義 \(f(n)=2n+1, \ g(n)=n\),以及 \(c=3, \ h(n)=c*g(n)=3n\),來看 \(f(n)\) 是否屬於 \(\mathcal{O}(g(n))\),請看下圖:

\(h(n)\)\(f(n)\) 交於 \((1,3)\),那麼 \(n_0=1\),且當 \(n \ge n_0\) 時,\(f(n)\) 會小於等於 \(h(n)\),根據定義,我們找的到 \(c\)\(n_0\),所以 \(f(n)=\mathcal{O}(g(n))=\mathcal{O}(n)\)

-

再舉一個例子,定義 \(f(n)=n+2, g(n)=n^{2}, c=1,g(n)=cg(n)\),直接看圖:

+

再舉一個例子,定義 \(f(n)=n+2,\ g(n)=n^{2},\ c=1,g(n)=cg(n)\),直接看圖:

\(g(n)\)\(f(n)\) 交於 \((2,4)\),那麼 \(n_0=2\),且當 \(n \ge n_0\) 時,\(f(n)\) 會小於等於 \(c*g(n)\),根據定義,我們找的到 \(c\)\(n_0\),所以 \(f(n)=\mathcal{O}(g(n))=\mathcal{O}(n^{2})\)

在這個例子中,\(n+2=\mathcal{O}(n^2)\),但你可以從第一個例子中類推出 \(n+2=\mathcal{O}(n)\),根據定義,前者也是對的,但我們應該選擇更接近的上界 \(\mathcal{O}(n)\) ,因為更能表現 \(n+2\) 的行為,當然,\(n+2=O(n\log_{}n)=O(n^2)=O(n^3)=O(n!)\cdots\) 也都是對的。

@@ -2182,6 +2208,7 @@

Practice Answer 1 +

內層迴圈的執行次數取決於外層迴圈的 i 值,因此,第一次執行 \(0\) 次,第二次執行 \(1\) 次,第三次執行 \(2\) 次,以此類推,所以時間複雜度是:

\(0+1+2+\cdots+(n-1)=\sum_{i=0}^{n-1}i=\frac{(n-1)n}{2}=\frac{n^2-n}{2}=\mathcal{O}(n^2)\)

這個複雜度在介紹排序演算法時會再次提到。

@@ -2205,7 +2232,7 @@

Practice Answer 2 -

很直觀的,\(i^2=n\),所以 \(i=\sqrt{n}\),所以時間複雜度是 \(\mathcal{O}(\sqrt{n})\)

+

\(i^2=n\),所以 \(i=\sqrt{n}\),所以時間複雜度是 \(\mathcal{O}(\sqrt{n})\)

Practice 3

diff --git a/search/search_index.json b/search/search_index.json index 1bfda54..7007c55 100755 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Hello ZestAlgo Book!","text":""},{"location":"#welcome_to_zestalgo","title":"Welcome to ZestAlgo","text":"

\u672c\u66f8\u7684\u6b63\u78ba\u6253\u958b\u65b9\u5f0f\u662f\u6697\u8272\u6a21\u5f0f\u3002

print(\"Hello ZestAlgo Book!\")\nprint(\"Welcome to LMcps!\")\n
"},{"location":"#why_does_it_start","title":"Why does it start?","text":"

\u767c\u73fe\u4e86\u76ee\u524d\u8cc7\u8a0a\u6559\u80b2\u7684\u7a98\u56f0\uff0c\u8af8\u5982\u7db2\u8def\u4e0a\u7684\u8cc7\u6e90\u96d6\u591a\uff0c\u4f46\u4e0d\u6613\u5165\u9580\uff0c\u65bc\u662f\u6c7a\u5b9a\u5beb\u4e00\u672c\u66f8\uff0c\u4ee5\u5927\u91cf\u7684\u5716\u7247\u53ca\u7c21\u55ae\u7684\u6587\u5b57\uff0c\u8b93\u65b0\u624b\u80fd\u5920\u5feb\u901f\u4e14\u53cb\u5584\u5730\u5165\u9580\uff0c\u4e26\u4e14\u80fd\u5920\u5728\u77ed\u6642\u9593\u5167\uff0c\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\u8207\u81ea\u4fe1\u3002

"},{"location":"#content","title":"Content","text":"

\u9019\u672c\u66f8\u8457\u91cd\u65bc\u61c9\u7528\u9762\uff0c\u4e26\u4e14\u5be6\u4f5c\u70ba\u4e3b\uff0c\u4e5f\u4e26\u975e\u5e36\u4f60\u6253\u7af6\u7a0b\uff0c\u800c\u662f\u5e36\u4f60\u4e86\u89e3\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u3002

"},{"location":"#wait_to_be_done","title":"Wait to be done","text":"
  1. \u5fc3\u5f97\u5206\u4eab\u5c08\u5340
  2. \u5c08\u696d\u7528\u8a9e
"},{"location":"#contributors","title":"Contributors","text":""},{"location":"algorithm/","title":"Algorithm","text":"

\u5728\u9019\u88e1\uff0c\u6211\u6703\u9010\u6b65\u5e36\u4f60\u8a8d\u8b58\u6f14\u7b97\u6cd5\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e00\u4e9b\u57fa\u672c\u7684\u6f14\u7b97\u6cd5\u7bc4\u4f8b\u3002

"},{"location":"algorithm/what_is_algo/","title":"What is an Algorithm?","text":""},{"location":"algorithm/what_is_algo/#introduction","title":"Introduction","text":"

\u606d\u559c\u4f60\uff0c\u4f60\u8e0f\u5165\u4e86\u6f14\u7b97\u6cd5\u7684\u5927\u9580\u3002

\u5982\u679c\u4f60\u6709\u770b\u904e\u4e00\u4e9b\u6f14\u7b97\u6cd5\u7684\u66f8\u6216\u8005\u662f\u4e00\u4e9b\u7db2\u7ad9\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u6f14\u7b97\u6cd5\u7684\u5b9a\u7fa9\u6709\u9ede\u62bd\u8c61\u3002\u4f46\u5c31\u6211\u7684\u7406\u89e3\uff0c\u5c31\u662f\u9019\u56db\u9ede:

  1. \u6f14\u7b97\u6cd5\u7531\u6709\u9650\u7684\u6307\u4ee4\u7d44\u6210
  2. \u6f14\u7b97\u6cd5\u662f\u70ba\u4e86\u89e3\u6c7a\u7279\u5b9a\u554f\u984c
  3. \u6f14\u7b97\u6cd5\u662f\u4e00\u6b65\u4e00\u6b65\u57f7\u884c\u7684
  4. \u6f14\u7b97\u6cd5\u542b\u6709\u8f38\u5165\u8207\u8f38\u51fa

\u9019\u6a23\u5c31\u597d\u4e86\uff0c\u4e26\u4e0d\u7528\u5c0d\u5b9a\u7fa9\u592a\u904e\u65bc\u56b4\u683c\uff0c\u6703\u5beb\u66f4\u91cd\u8981\uff0c\u6f14\u7b97\u6cd5\u5c31\u662f\u4e00\u500b\u89e3\u6c7a\u554f\u984c\u7684\u65b9\u6cd5\u3002

\u4f60\u53ef\u80fd\u6709\u807d\u904e\u6d41\u7a0b\u5716\uff0c\u9019\u662f\u4e00\u500b\u5f88\u597d\u7528\u4f86\u8996\u89ba\u5316\u6f14\u7b97\u6cd5\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u4e4b\u524d\u5728 Repetiton Structures \u6709\u63d0\u5230\u7684 Collatz Conjecture\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u6d41\u7a0b\u5716\u4f86\u8868\u793a:

graph LR\nA[Input a number N] --> B{Is N == 1?};\nB -- Yes --> C[End];\nB -- No --> D{Is N odd?};\nD -- Yes --> E[N = 3N + 1];\nD -- No --> F[N = N // 2];\nE --> B;\nF --> B;
"},{"location":"algorithm/what_is_algo/#complexity","title":"Complexity","text":"

\u5728\u4e4b\u524d\u7684\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u9047\u5230\u4e86\u4e00\u4e9b\u6709\u591a\u7a2e\u89e3\u6cd5\u7684\u554f\u984c\uff0c\u90a3\u6211\u5011\u8981\u5982\u4f55\u8a55\u4f30\u9019\u4e9b\u6f14\u7b97\u6cd5\u7684\u597d\u58de\u5462?\u4ee5\u53ca\u8981\u4ee5\u4ec0\u9ebc\u6a19\u6e96(criteria)\u6216\u57fa\u6e96(bechmark)\u4f86\u8a55\u4f30\u5462?

\u5f88\u76f4\u89c0\u7684\uff0c\u6211\u5011\u6703\u60f3\u5230\u4ee5\u7a0b\u5f0f\u7684\u57f7\u884c\u6642\u9593\u4f86\u8a55\u4f30\uff0c\u4f46\u9019\u6a23\u7684\u65b9\u6cd5\u6709\u4e00\u500b\u554f\u984c\uff0c\u5c31\u662f\u7a0b\u5f0f\u7684\u57f7\u884c\u6642\u9593\u6703\u53d7\u5230\u5f88\u591a\u56e0\u7d20\u7684\u5f71\u97ff\uff0c\u4f8b\u5982: \u786c\u9ad4\u3001\u4f5c\u696d\u7cfb\u7d71\u3001\u7a0b\u5f0f\u8a9e\u8a00\u3001\u7de8\u8b6f\u5668\u3001\u8f38\u5165\u8cc7\u6599\u7b49\u7b49\u3002\u6240\u4ee5\u6211\u5011\u9700\u8981\u4e00\u500b\u66f4\u7a69\u5b9a\u7684\u65b9\u6cd5\u4f86\u8a55\u4f30\u6f14\u7b97\u6cd5\u7684\u597d\u58de\u3002

\u6216\u8a31\u6211\u5011\u5728 C++ \u4e0a\u5beb\u4e86\u4e00\u500b\u7cdf\u7cd5\u7684\u6f14\u7b97\u6cd5\uff0c\u800c\u5728 Python \u4e0a\u5beb\u4e86\u4e00\u500b\u300c\u597d\u7684\u300d\u6f14\u7b97\u6cd5\uff0c\u4f46\u5f8c\u8005\u7684\u57f7\u884c\u6642\u9593\u537b\u6bd4\u524d\u8005\u9577\uff0c\u96d6\u7136\u4f60 Python \u5beb\u7684\u901f\u5ea6\u6bd4 C++ \u5feb(XD)

\u6211\u5011\u4e0d\u59a8\u4ee5\u6f14\u7b97\u6cd5\u7684\u6b65\u9a5f\u6578\u4f86\u8a55\u4f30\uff0c\u4e26\u4e14\u89c0\u5bdf\u8f38\u5165\u8cc7\u6599\u8207\u6b65\u9a5f\u6578\u7684\u95dc\u4fc2\u3002

\u8209\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u505a\u70ba\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f print_hello\uff0c\u4e26\u63a5\u53d7\u4e00\u500b\u53c3\u6578 n\uff0c\u5370\u51fa hello, Algorithm! \u82e5\u5e72\u6b21\u3002

def print_hello(n):\n    for i in range(n):\n        for j in range(n):\n            print('hello, Algorithm!')\n\n    for i in range(2 * n):\n        print('hello, Algorithm!')\n\n    print('hello, Algorithm!')\n    print('hello, Algorithm!')\n    print('hello, Algorithm!')\n\n\nprint_hello(2)\n
Output
hello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\n

\u5b9a\u7fa9 \\(f(n)=n^{2}+2n+3\\) \u4f86\u4ee3\u8868\u8f38\u5165 \\(n\\)\uff0c\u6703\u5370\u51fa hello, Algorithm! \u7684\u6b21\u6578\uff0c

\u800c \\(f(2)=11\\)\uff0c\u4e5f\u5c31\u662f\u8aaa\u7576 \\(n=2\\) \u6642\uff0c\u6211\u5011\u7684\u6f14\u7b97\u6cd5\u6703\u5370\u51fa hello, Algorithm! \\(11\\) \u6b21\u3002

\u9019\u7a31\u70ba\u6642\u9593\u8907\u96dc\u5ea6(Time Complexity)\uff0c\u800c\u65e2\u7136\u6709\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u90a3\u7576\u7136\u4e5f\u6709\u7a7a\u9593\u8907\u96dc\u5ea6(Space Complexity)\uff0c\u9019\u662f\u6307\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u6240\u9700\u8981\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u4f8b\u5982: \u9663\u5217\u7684\u5927\u5c0f\u3001\u8b8a\u6578\u7684\u6578\u91cf\u7b49\u7b49\u3002

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u53cd\u8f49\u4e00\u500b\u4e32\u5217\uff0c\u63d0\u4f9b\u4e86\u5169\u7a2e\u89e3\u6cd5:

def reverse_list(lst):\n    reversed_lst = []\n    for i in range(len(lst) - 1, -1, -1):\n        reversed_lst.append(lst[i])\n\n    return reversed_lst\n\n\ndef reverse_list2(lst):\n    n = len(lst)\n    for i in range(n // 2):\n        tmp = lst[i]\n        lst[i] = lst[n - i - 1]\n        lst[n - i - 1] = tmp\n    return lst\n\n\nprint(reverse_list([1, 2, 3, 4, 5]))\nprint(reverse_list2([1, 2, 3, 4, 5]))\n
Output
[5, 4, 3, 2, 1]\n[5, 4, 3, 2, 1]\n

\u5b9a\u7fa9 \\(g(n)\\) \u4f86\u4ee3\u8868\u8f38\u5165 \\(n\\)\uff0c\u6f14\u7b97\u6cd5\u6240\u9700\u8981\u7684\u984d\u5916\u7a7a\u9593\uff0c\u4f8b\u5982 \\(g(n)=n\\)\uff0c\u4e5f\u5c31\u662f\u8aaa\u7576 \\(n=5\\) \u6642\uff0c\u6211\u5011\u7684\u6f14\u7b97\u6cd5\u6703\u9700\u8981 \\(5\\) \u500b\u7a7a\u9593\uff0c\u6216\u8005\u8aaa\u662f\u9700\u8981\u5927\u5c0f\u70ba \\(5\\) \u7684\u4e32\u5217\u3002

\u8a66\u8457\u6bd4\u8f03\u4e0a\u8ff0\u5169\u500b\u53cd\u8f49\u4e32\u5217\u7684\u6f14\u7b97\u6cd5:

Algorithm \\(f(n)\\) \\(g(n)\\) reverse_list \\(n\\) \\(n\\) reverse_list2 \\(n/2\\) \\(1\\)

reverse_list \u6703\u5efa\u7acb\u4e00\u500b\u5927\u5c0f\u70ba \\(n\\) \u7684\u4e32\u5217\uff0c\u5132\u5b58\u53cd\u8f49\u5f8c\u7684\u4e32\u5217\uff0c\u56e0\u6b64 \\(f(n)\\) \u8207 \\(g(n)\\) \u90fd\u662f \\(n\\)\uff0c

\u800c reverse_list2 \u5247\u662f\u76f4\u63a5\u5728\u539f\u5730(in-place)\u9032\u884c\u53cd\u8f49\uff0c\u53ea\u4f7f\u7528 tmp \u9019\u500b\u8b8a\u6578\uff0c\u6240\u4ee5 \\(g(n)\\) \u662f \\(1\\)\uff0c\u53c8\u56e0\u70ba\u53ea\u9700\u8981\u53cd\u8f49\u4e00\u534a\u7684\u4e32\u5217\uff0c\u6240\u4ee5 \\(f(n)\\) \u662f \\(\\frac{n}{2}\\)\u3002

\u9019\u6a23\u7684\u6bd4\u8f03\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u9078\u64c7\u9069\u5408\u7684\u6f14\u7b97\u6cd5\uff0c\u4e26\u4e14\u8a55\u4f30\u6f14\u7b97\u6cd5\u7684\u597d\u58de\uff0c\u4f46\u662f\u9019\u6a23\u9084\u7a0d\u5acc\u9ebb\u7169\uff0c\u6709\u6c92\u6709\u66f4\u6e05\u695a\u4e14\u7c21\u55ae\u7684\u65b9\u6cd5\u5462?

"},{"location":"algorithm/what_is_algo/#big_o_notation","title":"Big O Notation","text":"

\u9019\u6642\u5019\u5c31\u8981\u4ecb\u7d39\u5927 O \u7b26\u865f(Big O Notation)\u4e86\uff0c\u5b83\u662f\u6f38\u8fd1\u7b26\u865f(Asymptotic Notation)\u7684\u4e00\u7a2e\uff0c\u7528\u4f86\u63cf\u8ff0\u4e00\u500b\u6f14\u7b97\u6cd5\u5728\u300c\u6700\u58de\u60c5\u6cc1\u300d\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u6240\u4ee5\u8aaa\u5b83\u662f\u4e00\u7a2e\u4e0a\u754c(Upper Bound)\uff0c\u4e5f\u901a\u5e38\u6307\u8f38\u5165\u8cc7\u6599\u7684\u898f\u6a21\u8da8\u8fd1\u7121\u7aae\u5927\u6642\u7684\u884c\u70ba\uff0c\u4e5f\u5c31\u662f \\(n\\) \u5f88\u5927\u7684\u6642\u5019\u3002

\u4f8b\u5982: \u7576 \\(n \\to \\infty\\)\uff0c\\(f(n)=n^{2}+2n+3\\) \u7684\u884c\u70ba\u5c31\u662f \\(\\mathcal{O}(n^{2})\\)\uff0c\u56e0\u70ba\u7576 \\(n\\) \u5f88\u5927\u7684\u6642\u5019\uff0c\\(n^{2}\\) \u6703\u9060\u9060\u5927\u65bc \\(2n+3\\)\uff0c\u6240\u4ee5\u53ef\u4ee5\u5ffd\u7565\u6389 \\(2n+3\\)\u3002

\u6211\u5011\u4f86\u770b\u8f03\u70ba\u6b63\u5f0f\u7684\u5b9a\u7fa9:

Big O

\\[f(n) \\text{ and } g(n) \\text{ are two functions.}\\] \\[\\text{The function }f(n)=\\mathcal{O}(g(n)) \\text{ iff } \\exists c,n_0 \\text{ s.t } 0\\le f(n) \\le c*g(n),\\forall n \\ge n_0\\]

\u5148\u5225\u6025\u8457\u95dc\u6389\u5566\uff0c\u5148\u7528\u4e2d\u6587\u89e3\u91cb\u4e00\u4e0b:

\\(f(n)\\) \u548c \\(g(n)\\) \u662f\u5169\u500b\u51fd\u6578\uff0c\u82e5\u4e14\u552f\u82e5\u5b58\u5728\u5e38\u6578 \\(c\\) \u548c \\(n_0\\)\uff0c\u5c0d\u65bc\u6240\u6709\u5927\u65bc\u7b49\u65bc \\(n_0\\) \u7684 \\(n\\)\uff0c\u4f7f\u5f97 \\(f(n)\\) \u5c0f\u65bc\u7b49\u65bc \\(c*g(n)\\)\u3002

\u5176\u4e2d \\(\\mathcal{O}(g(n))\\) \u662f\u4e00\u500b\u51fd\u6578\u96c6\u5408\uff0c\u5b83\u5305\u542b\u4e86\u6240\u6709\u4ee5 \\(c*g(n)\\) \u70ba\u4e0a\u754c\u7684\u51fd\u6578\uff0c\u800c \\(f(n)=\\mathcal{O}(g(n))\\) \u8207 \\(f(n) \\in \\mathcal{O}(g(n))\\) \u90fd\u662f\u4ee3\u8868 \\(f(n)\\) \u5c6c\u65bc \\(\\mathcal{O}(g(n))\\) \u7684\u4e00\u54e1\uff0c\u770b\u4f60\u7fd2\u6163\u7528\u54ea\u4e00\u7a2e\u7b26\u865f\u3002

\u4f46\u89ba\u5f97\u9019\u6a23\u9084\u4e0d\u5920\u6e05\u695a\uff0c\u6211\u5011\u76f4\u63a5\u4f86\u770b\u4f8b\u5b50:

\u5b9a\u7fa9 \\(f(n)=2n+1,g(n)=n\\)\uff0c\u4ee5\u53ca \\(c=3,h(n)=c*g(n)=3n\\)\uff0c\u4f86\u770b \\(f(n)\\) \u662f\u5426\u5c6c\u65bc \\(\\mathcal{O}(g(n))\\)\uff0c\u8acb\u770b\u4e0b\u5716:

\\(h(n)\\) \u8207 \\(f(n)\\) \u4ea4\u65bc \\((1,3)\\)\uff0c\u90a3\u9ebc \\(n_0=1\\)\uff0c\u4e14\u7576 \\(n \\ge n_0\\) \u6642\uff0c\\(f(n)\\) \u6703\u5c0f\u65bc\u7b49\u65bc \\(h(n)\\)\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u6211\u5011\u627e\u7684\u5230 \\(c\\) \u548c \\(n_0\\)\uff0c\u6240\u4ee5 \\(f(n)=\\mathcal{O}(g(n))=\\mathcal{O}(n)\\)\u3002

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9 \\(f(n)=n+2, g(n)=n^{2}, c=1,g(n)=cg(n)\\)\uff0c\u76f4\u63a5\u770b\u5716:

\\(g(n)\\) \u8207 \\(f(n)\\) \u4ea4\u65bc \\((2,4)\\)\uff0c\u90a3\u9ebc \\(n_0=2\\)\uff0c\u4e14\u7576 \\(n \\ge n_0\\) \u6642\uff0c\\(f(n)\\) \u6703\u5c0f\u65bc\u7b49\u65bc \\(c*g(n)\\)\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u6211\u5011\u627e\u7684\u5230 \\(c\\) \u548c \\(n_0\\)\uff0c\u6240\u4ee5 \\(f(n)=\\mathcal{O}(g(n))=\\mathcal{O}(n^{2})\\)\u3002

\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\\(n+2=\\mathcal{O}(n^2)\\)\uff0c\u4f46\u4f60\u53ef\u4ee5\u5f9e\u7b2c\u4e00\u500b\u4f8b\u5b50\u4e2d\u985e\u63a8\u51fa \\(n+2=\\mathcal{O}(n)\\)\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u524d\u8005\u4e5f\u662f\u5c0d\u7684\uff0c\u4f46\u6211\u5011\u61c9\u8a72\u9078\u64c7\u66f4\u63a5\u8fd1\u7684\u4e0a\u754c \\(\\mathcal{O}(n)\\) \uff0c\u56e0\u70ba\u66f4\u80fd\u8868\u73fe \\(n+2\\) \u7684\u884c\u70ba\uff0c\u7576\u7136\uff0c\\(n+2=O(n\\log_{}n)=O(n^2)=O(n^3)=O(n!)\\cdots\\) \u4e5f\u90fd\u662f\u5c0d\u7684\u3002

"},{"location":"algorithm/what_is_algo/#calculating_big_o","title":"Calculating Big O","text":"

\u90a3\u9ebc\u5982\u4f55\u8a08\u7b97\u4e00\u500b\u51fd\u6578\u7684\u5927 \\(\\mathcal{O}\\) \u7b26\u865f\u5462?\u53ea\u8981\u8a18\u4f4f\u4ee5\u4e0b\u539f\u5247:

  1. \u5ffd\u7565\u5e38\u6578\u8207\u4fc2\u6578\uff0c\u4f8b\u5982: \\(f(n)=114514\\)\uff0c\u90a3\u9ebc \\(f(n)=\\mathcal{O}(1)\\)\uff0c\u6bd4\u8f03\u7279\u5225\u7684\u662f\u56e0\u70ba\u8f38\u5165\u8cc7\u6599\u7684\u898f\u6a21\u4e0d\u6703\u5f71\u97ff \\(f(n)\\) \u7684\u503c\uff0c\\(\\mathcal{O}(1)\\) \u53c8\u7a31\u70ba\u5e38\u6578\u6642\u9593\u3002
  2. \u53ea\u4fdd\u7559\u6700\u9ad8\u6b21\u9805\uff0c\u4f8b\u5982: \\(f(n)=n^2+2n+3\\)\uff0c\u90a3\u9ebc \\(f(n)=\\mathcal{O}(n^2)\\)\u3002
  3. \u4e0d\u5728\u4e4e\u5c0d\u6578\u7684\u5e95\u6578\uff0c\u4f8b\u5982: \\(f(n)=n\\log_{2}n\\)\uff0c\u90a3\u9ebc \\(f(n)=\\mathcal{O}(n\\log_{}n)\\)\u3002
"},{"location":"algorithm/what_is_algo/#common_big_o","title":"Common Big O","text":"

\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u5927 \\(\\mathcal{O}\\) \u7b26\u865f:

  1. \\(\\mathcal{O}(1)\\): \u5e38\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u5b58\u53d6\u9663\u5217\u7684\u5143\u7d20
  2. \\(\\mathcal{O}(\\log_{}n)\\): \u5c0d\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u4e8c\u5143\u641c\u5c0b\u6cd5(Binary Search)
  3. \\(\\mathcal{O}(n)\\): \u7dda\u6027\u6642\u9593\uff0c\u4f8b\u5982: \u627e\u51fa\u9663\u5217\u4e2d\u7684\u6700\u5927\u503c
  4. \\(\\mathcal{O}(n\\log_{}n)\\): \u7dda\u6027\u5c0d\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u5feb\u901f\u6392\u5e8f(Quick Sort)
  5. \\(\\mathcal{O}(n^2)\\): \u5e73\u65b9\u6642\u9593\uff0c\u4f8b\u5982: \u6ce1\u6cab\u6392\u5e8f(Bubble Sort)
  6. \\(\\mathcal{O}(n^3)\\): \u7acb\u65b9\u6642\u9593\uff0c\u4f8b\u5982: \u77e9\u9663\u4e58\u6cd5
  7. \\(\\mathcal{O}(2^n)\\): \u6307\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u905e\u8ff4\u8cbb\u6c0f\u6578\u5217(Fibonacci Sequence)
  8. \\(\\mathcal{O}(n!)\\): \u968e\u4e58\u6642\u9593\uff0c\u4f8b\u5982: \u65c5\u884c\u63a8\u92b7\u54e1\u554f\u984c(Travelling Salesman Problem)

\u9019\u500b\u53c3\u8003\u5c31\u597d\u5594\uff0c\u770b\u904e\u53bb\u5c31\u597d\uff0c\u4e0d\u7528\u80cc\u3002

"},{"location":"algorithm/what_is_algo/#other_notations","title":"Other Notations","text":"

\u9664\u4e86\u5927 \\(\\mathcal{O}\\) \u7b26\u865f\u5916\uff0c\u9084\u6709\u5176\u4ed6\u7684\u6f38\u8fd1\u7b26\u865f:

  1. Big Omega: \\(\\Omega(g(n))\\)\uff0c\u7528\u4f86\u63cf\u8ff0\u4e00\u500b\u6f14\u7b97\u6cd5\u5728\u300c\u6700\u4f73\u60c5\u6cc1\u300d\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e0b\u754c(Lower Bound)\u3002
  2. Big Theta: \\(\\Theta(g(n))\\)\uff0c\u7528\u4f86\u63cf\u8ff0\u4e00\u500b\u6f14\u7b97\u6cd5\u5728\u300c\u5e73\u5747\u60c5\u6cc1\u300d\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e0a\u754c(Upper Bound)\u548c\u4e0b\u754c(Lower Bound)\u7684\u4ea4\u96c6\u3002

\u4f46\u5927 \\(\\mathcal{O}\\) \u7b26\u865f\u5c31\u5920\u7528\u4e86\uff0c\u9019\u5169\u500b\u7b26\u865f\u4e0d\u7528\u592a\u904e\u65bc\u6df1\u7a76\u3002

\u4f86\u500b\u7e3d\u7d50\u5427:

\\[\\text{if } f(n)=3n+2 \\text{ then:}\\] \\[\\underbrace{1 \\lt \\log_{}n \\lt \\sqrt{n} \\lt n^{\\frac{2}{3}}}_\\text{lower bound, including \u03a9(n)} \\lt \\overbrace{n}^\\text{Average bound, \u0398(n)} \\lt \\underbrace{n\\log_{}n \\lt n^2 \\lt n^3 \\lt \\cdots \\lt 2^n \\lt \\cdots \\lt n^n}_\\text{upper bound, including O(n)}\\]

\u4e00\u6a23\uff0c\u770b\u770b\u5c31\u597d\uff0c\u53ea\u662f\u70ba\u4e86\u8b93\u4f60\u77e5\u9053\u9084\u6709\u5176\u4ed6\u7684\u7b26\u865f\u3002

\u770b\u4e86\u90a3\u9ebc\u591a\uff0c\u4f11\u606f\u4e00\u4e0b\uff0c\u807d\u9996\u6b4c\u5427!

Coldplay - Viva La Vida

"},{"location":"algorithm/what_is_algo/#practice","title":"Practice","text":"

Practice 1

\u8acb\u554f\u4e0b\u65b9\u7a0b\u5f0f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

n = 5\ncnt = 0\n\nfor i in range(n):\n    for j in range(i):\n        cnt += 1\n\nprint(cnt)\n

Answer 1

\\(0+1+2+\\cdots+(n-1)=\\sum_{i=0}^{n-1}i=\\frac{(n-1)n}{2}=\\frac{n^2-n}{2}=\\mathcal{O}(n^2)\\)

\u9019\u500b\u8907\u96dc\u5ea6\u5728\u4ecb\u7d39\u6392\u5e8f\u6f14\u7b97\u6cd5\u6642\u6703\u518d\u6b21\u63d0\u5230\u3002

Practice 2

\u8acb\u554f\u4e0b\u65b9\u7a0b\u5f0f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

n = 64\ni = 0\n\nwhile i ** 2 < n:\n    i += 1\n\nprint(i)\n

Answer 2

\u5f88\u76f4\u89c0\u7684\uff0c\\(i^2=n\\)\uff0c\u6240\u4ee5 \\(i=\\sqrt{n}\\)\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(\\sqrt{n})\\)\u3002

Practice 3

\u8acb\u554f\u51fd\u5f0f search \u7684\u6700\u4f73\u3001\u6700\u58de\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

def search(lst, target):\n    for i in range(len(lst)):\n        if lst[i] == target:\n            return i\n    return -1\n\n\na = [1, 2, 3, 4, 5, 6]\nprint(search(a, 1))\nprint(search(a, 3))\nprint(search(a, 7))\n

Answer 3

\u6700\u4f73\u72c0\u6cc1\u5c31\u662f\u7b2c\u4e00\u500b\u5143\u7d20\u5c31\u662f\u76ee\u6a19\uff0c\u6700\u58de\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u4e0d\u5b58\u5728\u6216\u662f\u4ed6\u662f\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u5e73\u5747\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u5728\u4e32\u5217\u7684\u4e2d\u9593\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(1),\\mathcal{O}(n),\\mathcal{O}(n/2)\\)\u3002

\u9019\u500b\u51fd\u5f0f\u5c31\u662f\u7dda\u6027\u641c\u5c0b\u6cd5(Linear Search)\uff0c\u5728\u5f8c\u9762\u7684\u7ae0\u7bc0\u6703\u518d\u6b21\u63d0\u5230\u3002

Practice 4

\u8acb\u554f\u51fd\u5f0f factorial \u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

def factorial(n):\n    if n == 0:\n        return 1\n    return n * factorial(n - 1)\n\nprint(factorial(2))\n

Answer 4

\u5225\u5bb3\u6015\uff0c\u9019\u662f\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f\uff0c\u756b\u51fa\u6d41\u7a0b\u5716:

graph LR\n    A[\"factorial(2)\"] -->|\"2 != 0\"| B[\"2 * factorial(1)\"]\n    B -->|\"1 != 0\"| C[\"1 * factorial(0)\"]\n    C -->|\"0 == 0\"| D[\"1\"]\n    D -->|return| C\n    C -->|return| B\n    B -->|return| A

\u76f4\u5230 \\(n=0\\) \u6642\uff0c\u624d\u6703\u7d50\u675f\u905e\u8ff4\uff0c\u63a5\u8457\u5411\u4e0a\u8fd4\u56de\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(n)\\)\u3002

"},{"location":"algorithm/basic_algo/","title":"Basic Algorithm","text":"

\u5148\u4f86\u71b1\u500b\u8eab\uff0c\u6211\u5011\u5f9e\u4e00\u4e9b\u57fa\u672c\u7684\u6f14\u7b97\u6cd5\u958b\u59cb\u3002

"},{"location":"algorithm/basic_algo/binary_search/","title":"Binary Search","text":""},{"location":"algorithm/basic_algo/linear_search/","title":"Linear Search","text":""},{"location":"algorithm/basic_algo/linear_search/#introduction","title":"Introduction","text":"

\u7dda\u6027\u641c\u5c0b(Linear Search)\u6216\u7a31\u5faa\u5e8f\u641c\u5c0b(Squential Search)\uff0c\u662f\u4e00\u7a2e\u7c21\u55ae\u7684\u641c\u5c0b\u6f14\u7b97\u6cd5\uff0c\u5b83\u662f\u4e00\u7a2e\u904d\u6b77\u6574\u500b\u4e32\u5217\u7684\u65b9\u5f0f\uff0c\u9010\u4e00\u6aa2\u67e5\u6bcf\u500b\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6a19\u3002

\u9019\u500b\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(n)\\)\uff0c\u56e0\u70ba\u6700\u58de\u7684\u60c5\u6cc1\u5c31\u662f\u76ee\u6a19\u503c\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u6aa2\u67e5\u6574\u500b\u4e32\u5217\u3002

\u9019\u61c9\u8a72\u662f\u6700\u7c21\u55ae\u7684\u6f14\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4e0d\u904e\u6211\u5011\u53ef\u4ee5\u4f86\u8907\u7fd2\u4e0a\u4e00\u7ae0\u6240\u5b78\u7684\u6642\u9593\u8907\u96dc\u5ea6\u3002

"},{"location":"algorithm/basic_algo/linear_search/#implementation","title":"Implementation","text":"

\u76f4\u63a5\u4f86\u770b\u7a0b\u5f0f\u78bc\uff0c\u5b9a\u7fa9 linear_search(lst, target) \u51fd\u5f0f\uff0c\u9019\u500b\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u4e32\u5217 lst \u548c\u4e00\u500b\u76ee\u6a19\u503c target\uff0c\u56de\u50b3\u76ee\u6a19\u503c\u5728\u4e32\u5217\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u76ee\u6a19\u503c\u4e0d\u5b58\u5728\u5c31\u56de\u50b3 -1\u3002

def linear_search(lst, target):\n    for i in range(len(lst)):\n        if lst[i] == target:\n            return i\n\n    return -1\n\n\na = [4, 8, 4, 3, 1, 2, 3, 0]\nprint(linear_search(a, 4))\nprint(linear_search(a, 0))\nprint(linear_search(a, 10))\n
Output
0\n7\n-1\n

\u9084\u8a18\u5f97\u4e0a\u4e00\u7ae0\u7684\u7df4\u7fd2\u55ce? \u9019\u500b\u51fd\u5f0f\u7684\u6700\u4f73\u3001\u6700\u58de\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

Answer

\u6700\u4f73\u72c0\u6cc1\u5c31\u662f\u7b2c\u4e00\u500b\u5143\u7d20\u5c31\u662f\u76ee\u6a19\uff0c\u6700\u58de\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u4e0d\u5b58\u5728\u6216\u662f\u4ed6\u662f\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u5e73\u5747\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u5728\u4e32\u5217\u7684\u4e2d\u9593\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(1),\\mathcal{O}(n),\\mathcal{O}(n/2)\\)\u3002

\u800c\u4e32\u5217\u539f\u672c\u5c31\u6709\u4e00\u500b index \u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u9019\u4ef6\u4e8b\uff0c\u4ee5\u53ca\u4f60\u53ef\u4ee5\u7528 in \u95dc\u9375\u5b57\u4f86\u6aa2\u67e5\u76ee\u6a19\u503c\u662f\u5426\u5728\u4e32\u5217\u4e2d\u3002

a = [4, 8, 4, 3, 1, 2, 3, 0]\nprint(a.index(4))\nprint(4 in a)\n
Output
0\nTrue\n

\u6240\u4ee5\u9019\u500b\u6f14\u7b97\u6cd5\u5728\u5be6\u52d9\u4e0a\u4e26\u4e0d\u5e38\u7528\uff0c\u5c31\u7576\u4f5c\u71b1\u71b1\u8eab\u3002

"},{"location":"algorithm/basic_algo/linear_search/#practice","title":"Practice","text":""},{"location":"algorithm/basic_algo/linear_search/#leetcode_540_single_element_in_a_sorted_array","title":"LeetCode 540. Single Element in a Sorted Array","text":"

LeetCode 540. Single Element in a Sorted Array

Reference Code
class Solution:\n    def singleNonDuplicate(self, nums: list[int]) -> int:\n        if len(nums) == 1 or nums[0] != nums[1]:\n            return nums[0]\n        elif nums[-1] != nums[-2]:\n            return nums[-1]\n\n        for i in range(1, len(nums) - 1):\n            if nums[i] != nums[i - 1] and nums[i] != nums[i + 1]:\n                return nums[i]\n

\u5f88\u76f4\u89c0\uff0c\u53ea\u8981\u6aa2\u67e5\u76f8\u9130\u7684\u5169\u500b\u5143\u7d20\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u5c31\u662f\u7b54\u6848\u3002\u4f46\u9019\u6a23\u6211\u5011\u4e26\u6c92\u6709\u7528\u5230\u9019\u500b\u4e32\u5217\u662f\u6392\u5e8f\u904e\u7684\u9019\u500b\u7279\u6027\uff0c\u4e0b\u4e00\u7ae0\u4ecb\u7d39\u7684\u4e8c\u5143\u641c\u5c0b\u6cd5(Binary Search)\u53ef\u4ee5\u66f4\u5feb\u7684\u89e3\u6c7a\u9019\u500b\u554f\u984c\u3002

"},{"location":"blog/","title":"Blog","text":""},{"location":"blog/2024/02/06/hello-blog-/","title":"Hello Blog !","text":"

Hi, this is a test post.

For the better experience of the development, I think recording the process of the development is a good idea.

If you are interested in the project, you can follow the blog to get the latest information about the project.

Also, plaease contact me if you wanna join the project. I am looking forward to working with you.

"},{"location":"blog/2024/02/19/%E7%B6%B2%E7%AB%99%E7%9A%84%E6%96%B0%E5%90%8D%E5%AD%97/","title":"\u7db2\u7ad9\u7684\u65b0\u540d\u5b57","text":"

\u7db2\u7ad9\u7684\u65b0\u540d\u5b57\u662f ZestAlgo\uff0c\u53c3\u898b\u95dc\u65bc\u7db2\u7ad9\u540d\u7a31 #17

\u4f46\u9084\u662f\u53ef\u4ee5\u7e7c\u7e8c\u8a0e\u8ad6\uff0c\u800c\u539f\u672c\u7684 LMcps \u5c31\u53ef\u4ee5\u6b78\u985e\u70ba\u53c3\u8207\u6b64\u5c08\u6848\u7684\u7dad\u8b77\u793e\u7fa4\u4e4b\u4e00\u3002

"},{"location":"blog/2024/02/19/%E7%B6%B2%E7%AB%99%E7%9A%84%E6%96%B0%E5%90%8D%E5%AD%97/#logo","title":"\u95dc\u65bcLogo","text":"

\u56e0\u70ba Zest \u6709\u679c\u76ae\u7684\u610f\u601d\uff0c\u6240\u4ee5\u53ef\u4ee5\u671d\u5411\u6a58\u5b50\u3001\u6ab8\u6aac\u3001\u67f3\u6a59\u7b49\u7b49\u7684\u65b9\u5411\u53bb\u8a2d\u8a08\uff0c\u7b49\u6211\u6709\u6642\u9593\u7684\u6642\u5019\uff0c\u6211\u6703\u8a2d\u8a08\u4e00\u500b Logo \u51fa\u4f86\u3002

"},{"location":"data_structure/","title":"Data Structure","text":""},{"location":"fundamental/","title":"Fundamental","text":"

\u9019\u88e1\u5c07\u6703\u6559\u4f60\u57fa\u790e\u7684 Python \u8207 C++ \u7684\u57fa\u790e\u8a9e\u6cd5\u3002

"},{"location":"fundamental/cpp/","title":"C++","text":"

\u6211\u662fc++\u7cfb\u5217\u7684\u7b46\u8005bloodnighttw\uff0c\u6211\u9810\u8a08\u5728\u6b64\u6587\u7ae0\u5206\u4e0b\u5217\u5e7e\u500b\u7ae0\u7bc0\uff0c\u4e26\u76e1\u91cf\u4ee5\u521d\u5b78\u8005\u7684\u89d2\u5ea6\u5beb\u51fa\u5167\u5bb9\u3002

  1. \u74b0\u5883\u67b6\u8a2d
  2. Hello world (\u57fa\u672c\u8f38\u51fa\u5165)
  3. \u578b\u614b\u3001\u8b8a\u6578\u8207\u76f8\u95dc\u904b\u7b97
  4. \u6d41\u7a0b\u63a7\u5236
  5. \u8ff4\u5708
  6. \u51fd\u5f0f
  7. struct & class
  8. \u6307\u6a19\u8207\u53c3\u7167
  9. standard template libary

\u5982\u679c\u5728\u64b0\u5beb\u4e0a\u6709\u4efb\u4f55\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u5bc4\u96fb\u5b50\u90f5\u4ef6\u5230 bbeenn1227@gmail.com \u6216\u8005 emails@bntw.dev \u4f86\u505a\u8a62\u554f(\u4e0d\u904e\u8acb\u8868\u660e\u4f60\u7684\u8eab\u4efd\uff0c\u4e0d\u7136\u6211\u53ef\u80fd\u6703\u5ffd\u7565)\uff0c\u6216\u8005\u53ef\u4ee5\u5728\u6211\u66f4\u5e38\u51fa\u73fe\u7684discord\u8a62\u554f\u6211\u3002

\u6b64\u9023\u7d50\u70ba\u9ece\u660e\u8cc7\u8a0a\u793e\u7684discord\u7fa4\u7d44\u9023\u7d50\uff0c\u52a0\u5165\u4e4b\u5f8c\u53ef\u4ee5tag @bloodnighttw\uff0c\u53ea\u8981\u6211\u5728\u7dda\u4e0a\u4e14\u6709\u6642\u9593\u6211\u90fd\u6703\u76e1\u91cf\u5e6b\u4f60\u56de\u7b54\u3002

\u53e6\u5916\u6709\u4efb\u4f55\u932f\u5b57\u6216\u53ef\u4ee5\u6539\u5584\u7684\u5730\u65b9\u8acb\u767cissues\uff0c\u6211\u5011\u6703\u518d\u8a0e\u8ad6\u904e\u5f8c\u9069\u6642\u5730\u505a\u51fa\u4fee\u6b63\u3002

"},{"location":"fundamental/cpp/0/create_env/","title":"0. \u74b0\u5883\u67b6\u8a2d","text":"

\u6b63\u6240\u8b02\u5de5\u6b32\u5584\u5176\u4e8b\uff0c\u5fc5\u5148\u5229\u5176\u5668\uff0c\u6b64\u7ae0\u6211\u5011\u5206\u5225\u6703\u5728\u5206\u5225\u8b1b\u8ff0\u5982\u4f55\u5728windows\u3001macos\u4e0a\u5b89\u88dd\u76f8\u95dc\u5957\u4ef6\u3002

\u6211\u5011\u9810\u8a08\u8981\u4f7f\u7528GNU C++ Compiler\u9032\u884c\u7de8\u8b6f\uff0c\u4ee5\u53ca\u4f7f\u7528Visual studio code + clangd\u8207c++11\u7684\u6a19\u6e96\u64b0\u5beb\u7a0b\u5f0f\uff0c\u4e0b\u9762\u6703\u8a73\u8ff0\u5b89\u88dd\u6d41\u7a0b\u3002

\u7576\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u50cf\u662fdev c++\u3001codeblock\u9019\u985e\u7684\u8edf\u9ad4\u9032\u884c\u64b0\u5beb\uff0c\u53ea\u4e0d\u904e\u6709\u4e00\u9ede\u9700\u8981\u6ce8\u610f\uff0c\u820a\u7248\u7684dev c++\u9810\u8a2d\u6c92\u6709\u555f\u7528c++11\u6a19\u6e96\u7684\u652f\u63f4\uff0c\u4f60\u5fc5\u9808\u8981\u5728\u8a2d\u5b9a\u88e1\u9762\u5c0b\u627e\u958b\u555f\u7684\u65b9\u5f0f\u3002

"},{"location":"fundamental/cpp/0/create_env/#windows","title":"Windows","text":"

GNU C++ Compiler (G++)\u672c\u8eab\u662f\u6c92\u6709\u652f\u63f4Windows\u7684\uff0c\u4f46\u6211\u5011\u53ef\u4ee5\u900f\u904e\u4f7f\u7528wsl2\u6216\u8005\u5225\u4eba\u7528\u597d\u7684\u79fb\u690d\u7248\u672c\u9032\u884c\u4f7f\u7528\uff0c\u9019\u908a\u6211\u5011\u6703\u5b89\u88dd\u5225\u4eba\u5beb\u597d\u7684\u79fb\u690d\u7248\u3002

"},{"location":"fundamental/cpp/0/create_env/#1mingw","title":"1.\u4e0b\u8f09\u4e26\u5b89\u88ddmingw","text":"
  1. \u524d\u5f80\u9019\u500b\u9023\u7d50\u5b89\u88ddmingw
  2. \u52fe\u9078 mingw32-gcc-g++
  3. installation > apply change
  4. \u6309\u4e0bapply\u5b89\u88dd

"},{"location":"fundamental/cpp/0/create_env/#2","title":"2. \u52a0\u5165\u74b0\u5883\u8b8a\u6578","text":"
  1. \u5c0b\u627e\u5b89\u88dd\u4f4d\u7f6e\uff0c\u4ee5\u6211\u70ba\u4f8b\uff0c\u662f\u5b89\u88dd\u5728C:\\MinGW\u88e1\u9762\uff0c\u627e\u5230\u88e1\u9762\u7684bin\u8cc7\u6599\u593e\uff0c\u9ede\u9032\u53bb\u4e26\u8907\u88fd\u8def\u5f91\u3002
  2. \u641c\u5c0b\u74b0\u5883\u8b8a\u6578\uff0c\u4e26\u9032\u53bb\u7de8\u8f2f\u74b0\u5883\u8b8a\u6578\u3002
  3. \u9ede\u5165 \u9032\u968e >\u74b0\u5883\u8b8a\u6578\uff0c\u4e26\u5728Path\u4e2d\u8cbc\u4e0a\u525b\u624d\u8907\u88fd\u7684\u8b8a\u6578\uff0c\u7136\u5f8c\u4fdd\u5b58\u9000\u51fa\u3002

"},{"location":"fundamental/cpp/0/create_env/#3","title":"3. \u6e2c\u8a66\u6307\u4ee4","text":"

\u958b\u555f\u63d0\u793a\u547d\u4ee4\u5b57\u5143\u5f8c\uff0c\u6253\u5165g++ -v\uff0c\u5982\u679c\u6709\u7248\u672c\u8a0a\u606f\u4ee3\u8868\u5b89\u88dd\u6210\u529f\u3002

"},{"location":"fundamental/cpp/0/create_env/#macos","title":"macOS","text":"

macos\u672c\u8eab\u5c31\u6709\u63d0\u4f9bclangd\u4f5c\u70ba\u7de8\u8b6f\u5668\u4f7f\u7528\uff0c\u5728\u5b89\u88ddxcode\u5f8c\u61c9\u8a72\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982\u679c\u9084\u662f\u9700\u8981GNU C++ Compiler\uff0c\u8acb\u53c3\u8003\u4e0b\u9762\u6b65\u9a5f\u3002

"},{"location":"fundamental/cpp/0/create_env/#1homwbrew","title":"1.\u5b89\u88ddhomwbrew\u5957\u4ef6\u7ba1\u7406\u5668","text":"

\u6839\u64da\u5b98\u7db2\u7684\u6307\u793a\u5b89\u88ddhomebrew\u3002

"},{"location":"fundamental/cpp/0/create_env/#2g","title":"2.\u4f7f\u7528\u7ba1\u7406\u5668\u5b89\u88ddg++\u5957\u4ef6","text":"
  1. \u5148\u4f7f\u7528brew search gcc\u641c\u5c0b\u76f8\u95dc\u5957\u4ef6
  2. \u4f7f\u7528 brew install gcc\u5b89\u88ddCompiler
  3. \u4f7f\u7528gcc-13 -v\u6216g++-13\u78ba\u8a8d\u662f\u5426\u6210\u529f\u5b89\u88dd

Warning

\u8acb\u6ce8\u610f\uff0c\u5728macos\u4e0a\u9810\u8a2d\u4f7f\u7528\u7684g++\u6307\u4ee4\u70baclang\u800c\u4e0d\u662fGNU C++ Compiler\uff0c\u8acb\u6539\u4f7f\u7528g++-XX \u4f86\u64cd\u4f5cg++\u7de8\u8b6f\u5668\u3002

"},{"location":"fundamental/cpp/0/create_env/#visual_studio_code","title":"Visual Studio Code","text":""},{"location":"fundamental/cpp/0/create_env/#1_visual_studio_code","title":"1. \u5b89\u88ddVisual Studio Code","text":"

\u524d\u5f80\u9019\u500b\u7db2\u7ad9\u4e0b\u8f09\u5c6c\u65bc\u4f60\u7cfb\u7d71\u7684visual studio code\u5b89\u88dd\u6a94\u3002

"},{"location":"fundamental/cpp/0/create_env/#2_extension","title":"2. \u5b89\u88dd extension","text":"
  1. \u9ede\u5165extension\u9801\u9762\u5206\u5225\u5b89\u88ddC/C++ clangd \u9019\u5169\u500bextension\uff0c\u4e26\u91cd\u555fvscode
  2. \u4f60\u53ef\u80fd\u9047\u5230\u9019\u6a23\u7684\u8b66\u544a\u8a0a\u606f\uff0c\u8acb\u6309\u4e0b Disable Intellisence
"},{"location":"fundamental/cpp/0/create_env/#_1","title":"\u5176\u4ed6","text":"

\u5982\u679c\u9047\u5230\u5b57\u9ad4\u975e\u7b49\u5bec\uff0c\u8acb\u5c0b\u627e\u4e00\u500b\u7b49\u5bec\u5b57\u9ad4\u4e26\u4f7f\u7528\uff0c\u7b49\u5bec\u5b57\u9ad4\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002

"},{"location":"fundamental/cpp/1/hello_world/","title":"1. Hello World","text":""},{"location":"fundamental/cpp/1/hello_world/#hello_world","title":"Hello World (\u8f38\u51fa)","text":""},{"location":"fundamental/cpp/1/hello_world/#_1","title":"\u521d\u5b78\u8005\u7684\u7b2c\u4e00\u6b65","text":"

\u9996\u5148\uff0c\u8acb\u5728vscode (Visual studio code)\u4e2d\u65b0\u589e\u4e00\u500b\u540d\u70bahello_world.cpp\uff0c\u4e26\u6253\u5165\u4e0b\u5217\u7a0b\u5f0f\u78bc\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\" << endl;\n    return 0;\n}\n
\u78ba\u8a8d\u597d\u5b58\u6a94\u904e\u5f8c\uff0c\u6309\u4e0b\u53f3\u4e0a\u89d2\u7684\u57f7\u884c\u6309\u9375\uff0c\u4f60\u5c31\u6703\u6210\u529f\u770b\u5230\u4f60\u5beb\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\u3002

\u63a5\u8457\uff0c\u6211\u5011\u5c07\u66f4\u6df1\u5165\u8a0e\u8ad6\u9019\u6bb5\u7a0b\u5f0f\uff0c\u5230\u5e95\u767c\u751f\u4e86\u4ec0\u9ebc\u4e8b\u60c5\u3002

"},{"location":"fundamental/cpp/1/hello_world/#hello_world_1","title":"Hello World\u7a0b\u5f0f\u89e3\u91cb","text":"

\u5728\u9019\u6bb5\u7a0b\u5f0f\u4e2d\uff0c\u5df2\u7d93\u6709\u8a31\u591a\u7a0d\u5fae\u8907\u96dc\u7684\u6982\u5ff5\uff0c\u5148\u5225\u6015\uff0c\u6211\u5011\u5c31\u53ea\u8981\u4e86\u89e3\u5230:

  1. \u7a0b\u5f0f\u4e00\u5b9a\u662f\u5f9eint main(){......}\u88e1\u9762\u958b\u59cb\u57f7\u884c\u7684
  2. \u6700\u5f8c\u4e00\u5b9a\u6703\u6709\u500breturn 0; \u4ee3\u8868\u7a0b\u5f0f\u6210\u529f\u7d50\u675f\u3002

\u8b93\u6211\u5011\u7126\u9ede\u653e\u5728\u7b2c\u516d\u884c:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\" << endl;\n    return 0;\n}\n

\u9019\u908a\u6709\u4e86\u6211\u5011\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\uff0c\u4f60\u53ef\u4ee5\u628acout\u7576\u6210\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\uff0c\u628a<<\u7576\u6210\u50b3\u905e\u7684\u7bad\u982d\uff0c\u800c\u9019\u6bb5\u7a0b\u5f0f\u5c31\u662f\u8981\u628a\u9019\u6bb5\u5b57\u50b3\u7d66\u96fb\u8166\u5370\u51fa\u3002

\u9019\u908a\u7684endl\u4ee3\u8868\u8457\u63db\u884c\u7684\u610f\u601d\uff0c\u900f\u904e<<\u50b3\u7d66\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\u3002

\u53e6\u5916\u4e00\u500b\u5beb\u6cd5

\u9019\u908a\u63d0\u4f9b\u53e6\u5916\u4e00\u500b\u63db\u884c\u7684\u5beb\u6cd5\uff0c\u5177\u9ad4\u5dee\u7570\u5728\u719f\u6089\u8a9e\u6cd5\u5f8c\uff0c\u6703\u5728\u53e6\u5916\u4e00\u7bc7\u6587\u7ae0\u505a\u8aaa\u660e\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\\n\";\n    return 0;\n}\n
\\n\u70ba\u63db\u884c\u5b57\u5143\uff0c\u5728\u5b57\u5143\u4e2d\u6709\u8a31\u591a\u5b57\u5143\u6703\u6709\u7279\u5b9a\u529f\u7528\uff0c\u6703\u4ee5\\\u958b\u982d\uff0c\u9023\u7d50\u4e00\u500b\u5b57\u6bcd\u6216\u6578\u5b57\uff0c\u4ee3\u8868\u8457\u8df3\u812b\u5b57\u5143\uff0c\u4f5c\u70ba\u7279\u6b8a\u7528\u9014\u4f7f\u7528\u3002

\u7d30\u5fc3\u5982\u4f60\uff0c\u9019\u662f\u53c8\u8981\u63d0\u554f\u4e86\uff0c\u5206\u865f;\u53c8\u4ee3\u8868\u4ec0\u9ebc\u610f\u601d\uff1f

\u5206\u865f;\u662f\u544a\u8a34\u96fb\u8166\u8aaa\uff0c\u9019\u4e9b\u6307\u4ee4\u8ddf\u4e0b\u4e00\u500b\u5b57\u5143\u6216\u4e0b\u4e00\u884c\u662f\u5206\u958b\u7684\uff0c\u4e0d\u8981\u628a\u4ed6\u9023\u5728\u4e00\u8d77\uff0c\u4e5f\u56e0\u70ba\u6709\u9019\u6771\u897f\uff0c\u6211\u5011\u7a0b\u5f0f\u53ef\u4ee5\u9019\u6a23\u5beb\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\\n\"; cout << endl;\n    return 0;\n}\n

Danger

\u5341\u5206\u4e0d\u5efa\u8b70\u4f60\u9019\u6a23\u5beb\uff0c\u5c0d\u65bc\u7a0b\u5f0f\u719f\u7df4\u7684\u4eba\u4f86\u8aaa\uff0c\u9019\u6a23\u7684\u6392\u7248\uff0c\u7a0b\u5f0f\u78bc\u8b80\u8d77\u4f86\u6703\u975e\u5e38\u75db\u82e6\uff0c\u5c0d\u65bc\u521d\u5b78\u8005\uff0c\u5247\u5bb9\u6613\u8aa4\u89e3\u7a0b\u5f0f\u78bc\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_2","title":"\u52a0\u5165\u8b8a\u6578 (\u8f38\u5165)","text":""},{"location":"fundamental/cpp/1/hello_world/#_3","title":"\u8b80\u53d6\u5b57\u4e32","text":"
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    string str;\n    cin >> str;\n    cout << str << endl;\n    return 0;\n}\n

\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u4e32\u6587\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u6587\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_4","title":"\u8b80\u53d6\u6578\u5b57","text":"

\u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp;\n    cin >> temp;\n    cout << temp << endl;\n    return 0;\n}\n

\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u500b\u6578\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u4e00\u4e32\u6578\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002

Info

\u5982\u679c\u4eca\u5929\u6253\u5165\u7684\u6578\u5b57\u975e\u5e38\u975e\u5e38\u7684\u5927\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u5370\u51fa\u4f86\u7684\u6578\u5b57\u8ddf\u6253\u5165\u7684\u6578\u5b57\u4e0d\u4e00\u6a23\uff0c\u5176\u539f\u56e0\u662f\u6ea2\u4f4d\uff0c\u9019\u9ede\u6211\u5011\u6703\u5728\u4e0b\u4e00\u7ae0\u8ac7\u5230\u5176\u539f\u56e0\u8207\u89e3\u6cd5\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_5","title":"\u8f38\u5165\u7a0b\u5f0f\u89e3\u91cb","text":"

\u6b63\u5982cout\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u628acin\u7576\u6210\u4f60\u7684\u9375\u76e4\uff0c\u900f\u904e>>\u544a\u8a34\u96fb\u8166\u5f9e\u9375\u76e4\u8f38\u5165\u81f3\u53f3\u908a\u7684\u8b8a\u6578\u3002

\u5e38\u898b\u7684\u8aa4\u5340

\u5c0d\u65bc\u521d\u5b78\u8005\u4f86\u8aaa\uff0c\u53ef\u80fd\u6703\u4e0d\u5c0f\u5fc3\u5beb\u51fa\u9019\u6a23\u7684\u7a0b\u5f0f\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp1,temp2;\n    cin << temp1 << temp2;\n    cout << temp1 << \"!=\" << temp2 << endl;\n    return 0;\n}\n

\u6b64\u6642\u4f60\u6703\u767c\u73fe\u7121\u6cd5\u7de8\u8b6f\uff0c\u5176\u539f\u56e0\u662f\u4f60\u7684cin\u7684<<\u61c9\u8a72\u8981\u662f>>\u624d\u5c0d \u3002

\u5e38\u898b\u7684\u932f\u8aa4\u4e5f\u5305\u62ec\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a

  1. cout\u7684<< \u65b9\u5411\u932f\u8aa4
    #include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp1,temp2;\n    cin >> temp1 >> temp2;\n    cout >> temp1 >> temp2;\n    return 0;\n}\n
  2. cout\u7684\u4f4d\u7f6e\u932f\u8aa4

    #include <iostream>\n\nusing namespace std;\n\nint main(){\n    \"error\" >> cout;\n    return 0;\n}\n
    cout \u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002

  3. cin\u7684\u4f4d\u7f6e\u932f\u8aa4

    #include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a;\n    a << cin;\n    return 0;\n}\n
    \u539f\u56e0\u540c\u4e0a\uff0ccin\u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_6","title":"\u9023\u7e8c\u8b80\u53d6\u6578\u5b57/\u5b57\u4e32","text":"

\u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u4e00\u9023\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp1;\n    string temp2;\n    cin >> temp1 >> temp2;\n    cout << temp1 << \"!=\" << temp2 << endl;\n    return 0;\n}\n
input
1234 4321\n
output
1234!=4321\n

\u4f60\u6703\u767c\u73fe\u4e00\u4ef6\u4e8b\u60c5\uff0ccin\u662f\u900f\u904e\u7a7a\u683c\u6216\u8005\u63db\u884c\u4f86\u5206\u958b\u7684\uff0c\u4eca\u5929\u5982\u679c\u60f3\u8981\u8b80\u53d6\u4e00\u884c\uff0c\u8acb\u53c3\u8003\u4e0b\u65b9\u7a0b\u5f0f\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_7","title":"\u8b80\u53d6\u4e00\u884c","text":"

\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff0c\u5176\u4e2dtemp2\u8acb\u6539\u6210\u8b8a\u6578\u7684\u540d\u7a31\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    string temp2;\n    getline(cin,temp2);\n    cout << temp2 << endl;\n    return 0;\n}\n
"},{"location":"fundamental/cpp/1/hello_world/#practice","title":"Practice","text":"

\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:

Info

  1. \u5982\u4f55\u4f7f\u7528 cout\u8207cin
  2. \u63a5\u89f8\u8b8a\u6578\u7684\u6982\u5ff5\u3002

\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002

ZeroJudge - a001. \u54c8\u56c9

Reference code
#include <iostream>\n\nint main(){\n    string word;\n    cin >> word;\n    cout << \"hello, \" << word << endl;\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/","title":"\u578b\u614b\u3001\u8b8a\u6578\u8207\u76f8\u95dc\u904b\u7b97","text":"

\u5728\u524d\u4e00\u7ae0\u4e2d\uff0c\u4f60\u61c9\u8a72\u6703\u5c0d\u8b8a\u6578\u6709\u76f8\u95dc\u7684\u6982\u5ff5\uff0c\u9019\u4e00\u7ae0\u6211\u5011\u5c07\u66f4\u6df1\u5165\u8a0e\u8ad6\u9019\u4e9b\u6771\u897f\u3002

"},{"location":"fundamental/cpp/2/#_2","title":"\u578b\u614b\u5ba3\u544a","text":"

\u6b63\u5982\u524d\u4e00\u7bc7\u6240\u8ff0\uff0c\u7576\u6211\u5011\u9700\u8981\u4f7f\u7528\u4e00\u500b\u8b8a\u6578\uff0c\u8981\u5148\u505a\u5ba3\u544a\uff0c\u505a\u5ba3\u544a\u7684\u65b9\u5f0f\u70ba:

<\u578b\u614b\u540d\u7a31> <\u8b8a\u6578\u540d\u7a31>;\n
\u4f8b\u5982\u4eca\u5929\u6211\u5011\u8981\u5ba3\u544a\u4e00\u500b\u6574\u6578\u578b\u614b\u7684\u8b8a\u6578\uff0c\u5c31\u8981\u9019\u6a23\u5beb:
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int number;\n    return 0;\n}\n

\u5982\u679c\u4eca\u5929\u8981\u6307\u5b9a\u4e00\u500b\u521d\u59cb\u503c\uff0c\u5247\u8981\u9019\u6a23\u5beb:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int number = 0;\n    return 0;\n}\n

\u5370\u51fa\u4e00\u500b\u6c92\u6709\u7d66\u503c\u7684\u8b8a\u6578

\u8b93\u6211\u5011\u8a66\u8457\u5370\u51fa\u5b83:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int number;\n    cout << nunber << endl;\n    return 0;\n}\n
\u8acb\u591a\u57f7\u884c\u5e7e\u6b21\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u6bcf\u6b21\u57f7\u884c\u7684\u7d50\u679c\u597d\u50cf\u90fd\u6c92\u6709\u898f\u5247\u3002

\u9019\u662f\u56e0\u70ba\u5728\u5728c/c++\u4e2d\uff0c\u4e00\u500b\u8b8a\u6578\u5982\u679c\u6c92\u6709\u7d66\u521d\u59cb\u503c\uff0c\u90a3\u9ebc\u4ed6\u7684\u503c\u5c31\u662f\u672a\u5b9a\u7684\uff0c\u800c\u4e0d\u6703\u81ea\u52d5\u7d660\uff0c\u4ee5\u4e0a\u9762\u7684\u7a0b\u5f0f\u70ba\u4f8b\uff0c\u5982\u679c\u6c92\u6709\u7d66\u5b9a\u521d\u59cb\u503c\uff0c\u90a3\u4ed6\u6709\u53ef\u80fd\u662f1 \uff0c\u4e5f\u53ef\u80fd\u6703\u662f-10235\uff0c\u6240\u4ee5\u9664\u975e\u4f60\u975e\u5e38\u78ba\u5b9a\u9019\u500b\u8b8a\u6578\u5728\u5f8c\u9762\u6703\u99ac\u4e0a\u6539\u8b8a\u6210\u4e00\u500b\u53ef\u77e5\u7684\u72c0\u614b(\u50cf\u662fcin\u8f38\u5165\u9032\u53bb\u8b8a\u6578)\uff0c\u4e0d\u7136\u8acb\u4e00\u5f8b\u53c3\u8003\u6709\u7d66\u521d\u59cb\u503c\u7684\u5beb\u6cd5\uff0c\u4ee5\u907f\u514d\u51fa\u932f\u3002

"},{"location":"fundamental/cpp/2/#_3","title":"\u578b\u614b\u985e\u5225","text":"

\u4e00\u822c\u800c\u8a00\uff0c\u5e38\u4f7f\u7528\u7684\u578b\u614b\u6709\u53ef\u5206\u70ba\u4e94\u985e\uff0c\u5206\u5225\u70ba\u6574\u6578\u578b\u614b\u3001\u5c0f\u6578\u578b\u614b\u3001\u5b57\u5143\u578b\u614b\u3001\u9663\u5217\u578b\u614b\u3001\u548c\u5b57\u4e32\u578b\u614b\uff0c\u518d\u63a5\u4e0b\u4f86\u7684\u5e7e\u7bc7\u6587\u7ae0\uff0c\u6211\u5011\u5c07\u6703\u8457\u58a8\u65bc\u4ed6\u5011\u662f\u5982\u4f55\u5b58\u5728\u96fb\u8166\u7684\uff0c\u4ee5\u53ca\u4ed6\u5011\u7684\u76f8\u95dc\u7279\u6027\u3002

"},{"location":"fundamental/cpp/2/array/","title":"\u9663\u5217","text":"

\u5728\u524d\u9762\u7684\u7ae0\u7bc0\uff0c\u6211\u5011\u4e86\u89e3\u5982\u4f55\u5ba3\u544a\u8b8a\u6578\uff0c\u90a3\u8b93\u6211\u5011\u8a2d\u60f3\u4e00\u500b\u72c0\u6cc1\uff0c\u6211\u5011\u4eca\u5929\u8981\u5ba3\u544a100000\u500b\u8b8a\u6578\uff0c\u8a72\u4e0d\u6703\u53ea\u80fd\u9019\u6a23\u505a\u5427\uff1f

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a1,a2,a3,a4,......,a99999,a100000;\n\n    return 0;\n}\n

\u60f3\u4e5f\u77e5\u9053\u6211\u5011\u4e0d\u53ef\u80fd\u9019\u6a23\u4f5c\u5c0d\u5427\uff01 \u800c\u4e14\u9019\u6a23\u9084\u6709\u5ef6\u4f38\u4e00\u500b\u554f\u984c\uff0c\u8981\u5982\u4f55\u53d6\u5f97\u7b2cn\u500b\u8b8a\u6578\u7684\u503c\uff1f

"},{"location":"fundamental/cpp/2/array/#_2","title":"\u9663\u5217\u7684\u6982\u5ff5","text":"

\u5728\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5ba3\u544a\u4e00\u9023\u4e32\u7684\u8a18\u61b6\u9ad4\uff0c\u4f86\u7d66\u7a0b\u5f0f\u4f7f\u7528\uff0c\u6211\u5011\u7a31\u5176\u70ba\u9663\u5217\u3002

"},{"location":"fundamental/cpp/2/array/#_3","title":"\u4e00\u7dad\u9663\u5217","text":""},{"location":"fundamental/cpp/2/array/#_4","title":"\u5ba3\u544a","text":"

\u90a3\u8b93\u6211\u5011\u56de\u5230\u4e00\u958b\u59cb\u7684\u554f\u984c\uff0c\u5ba3\u544a100000\u500b\u8b8a\u6578\uff0c\u6211\u5011\u53ea\u8981\u9019\u6a23\u505a\uff0c\u4e26\u5b58\u53d6\u5b83\u3002

\u7b2c\u4e00\u7a2e\u5ba3\u544a\u65b9\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[0] = 1; //write into memory\n\n    cout << a[0] << endl;\n\n    return 0;\n}\n
\u7b2c\u4e8c\u7a2e\u5ba3\u544a\u65b9\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[] = {1,2,3,4,5};\n\n    cout << a[0] << endl;\n\n    return 0;\n}\n
\u8a3b\u89e3

\u9019\u908a//\u70ba\u8a3b\u89e3\uff0c\u5b83\u6703\u7701\u7565//\u5f8c\u9762\u7684\u5167\u5bb9\uff0c\u76f4\u5230\u4e0b\u884c\uff0c\u4e0d\u505a\u57f7\u884c\u3002\u540c\u6a23\u7684\u6771\u897f\u6709\u7528/* */\u5305\u7236\u7684\u7a0b\u5f0f\uff0c\u7528\u9019\u5169\u500b\u6771\u897f\u5305\u8986\u7684\u7247\u6bb5\u4e5f\u4e0d\u6703\u88ab\u57f7\u884c\uff0c\u4e0b\u65b9\u70ba\u7bc4\u4f8b\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[0] = 1; //write into memory\n\n    /*cout << a[0] << endl;*/\n\n    return 0;\n}\n
\u5e38\u898b\u7684\u932f\u8aa4

\u5982\u679c\u4f60\u6c92\u6709\u7d66\u5b9a\u503c\u5c31\u505a\u5b58\u53d6\uff0c\u90a3\u4f60\u4e5f\u6703\u9047\u5230\u672a\u7d66\u503c\u5f97\u76f8\u95dc\u932f\u8aa4\uff0c\u5982\u540c\u524d\u9762\u5c0d\u8b8a\u6578\u7684\u63cf\u8ff0\u4e00\u6a23\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[0] = 1; //write into memory\n\n    cout << a[1] << endl;\n\n    return 0;\n}\n

\u9019\u908a\u7684\u8f38\u51fa\u6703\u4e0d\u56fa\u5b9a\uff01

\u9019\u908a\u6709\u500b\u9ede\u9700\u8981\u6ce8\u610f\uff0c\u5b58\u53d6array\u90fd\u662f\u5f9e0\u958b\u59cb\u7684\uff0c\u4ee5\u4e0a\u65b9\u7684\u7a0b\u5f0f\u70ba\u4f8b\uff0c\u8981\u5b58\u53d6\u6700\u5f8c\u4e00\u500b\u9663\u5217\u5143\u7d20\u7684\u65b9\u6cd5\u70ba\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[99999] = 1; //write into memory\n\n    cout << a[99999] << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/array/#_5","title":"\u8d85\u51fa\u9663\u5217\u7bc4\u570d","text":"

\u4e0b\u65b9\u7684\u7a0b\u5f0f\u70ba\u8d85\u51fa\u9663\u5217\u61c9\u8a72\u5b58\u53d6\u7684\u7bc4\u570d \uff080~99999\uff09\uff0c\u6703\u6839\u64da\u4e0d\u540c\u7684\u4f5c\u696d\u7cfb\u7d71\uff08\u53ca\u7248\u672c\uff09\uff0c\u51fa\u73fe\u4e0d\u540c\u7684\u8655\u7406\u65b9\u5f0f\uff0c\u6709\u5831\u932f\u7684\uff0c\u6709\u4e7e\u8106\u4e0d\u8655\u7406\u7684\u3002

\u932f\u8aa4\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[100000] = 1; //error\n\n    cout << a[100000] << endl; //error\n\n    return 0;\n}\n

\u8acb\u6ce8\u610f\uff0c\u9019\u500b\u932f\u8aa4\u5e38\u662f\u6211\u5011\u5beb\u7a0b\u5f0f\u7684\u8aa4\u5340\uff0c\u5728\u64b0\u5beb\u7a0b\u5f0f\u6642\uff0c\u8acb\u52d9\u5fc5\u78ba\u4fdd\u6703\u5b58\u53d6\u5230\u6709\u6548\u7684\u7bc4\u570d\u3002

"},{"location":"fundamental/cpp/2/array/#_6","title":"\u4e8c\u7dad\u9663\u5217","text":""},{"location":"fundamental/cpp/2/array/#_7","title":"\u5ba3\u544a","text":"

\u4e8b\u5be6\u4e0a\uff0c\u9663\u5217\u4e0d\u4fb7\u9650\u65bc\u4e00\u7dad\uff0c\u5b83\u4e5f\u53ef\u4ee5\u662f\u4e8c\u7dad\u7684\u72c0\u614b\uff0c\u4e0b\u65b9\u70ba\u7bc4\u4f8b\u7a0b\u5f0f\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100][100];\n\n    a[0][0] = 1; //write into memory\n\n    cout << a[0][0] << endl; \n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/array/#_8","title":"\u8d85\u51fa\u9663\u5217\u7bc4\u570d","text":"

\u932f\u8aa4\u7a0b\u5f0f

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    char a[100][100];\n\n    a[0][100] = 'a'; //write into memory\n\n    cout << a[1][0] << endl; // output not random, print 'a' here \n\n    return 0;\n}\n
\u6b64\u7a0b\u5f0f\u6703\u5370\u51fa a\uff0c\u800c\u4e0d\u662f\u6709\u76f8\u95dc\u932f\u8aa4\uff0c\u9019\u662f\u70ba\u4f55\uff1f

\u8a18\u4e0d\u8a18\u5f97\u524d\u9762\u6240\u8aaa\uff0c\u9663\u5217\u70ba\u4e00\u9023\u4e32\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u9023\u4e32\u7a0b\u5f0f\u80fd\u7528\u7684\u7a7a\u9593\uff0c[0][100]\u6240\u5728\u7684\u5be6\u969b\u4f4d\u7f6e\u4f4d\u65bc[1][0]\uff0c\u6240\u4ee5\u624d\u4e0d\u6703\u6709\u56b4\u91cd\u7684\u932f\u8aa4\uff0c\u4e0d\u904e\u8acb\u6ce8\u610f\uff0c\u5e73\u6642\u64b0\u5beb\u6642\uff0c\u4e5f\u76e1\u91cf\u4e0d\u8981\u51fa\u73fe\u9019\u6a23\u7684\u932f\u8aa4\uff0c\u7562\u7adf\u6703\u51fa\u73fe\u9019\u6a23\u7684\u5beb\u6cd5\uff0c\u516b\u6210\u662f\u4f60\u5beb\u932f\u4e86\uff01

\u800c\u4e0b\u65b9\u7a0b\u5f0f\u5c31\u6703\u51fa\u73fe\u8d85\u51fa\u9663\u5217\u7684\u932f\u8aa4\uff0c\u56e0\u70ba\u5df2\u7d93\u8d85\u51fa\u80fd\u4f7f\u7528\u7684\u7bc4\u570d\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[10][10];\n\n    a[10][10] = 1; //error\n\n    cout << a[10][10] << endl; //error\n\n    return 0;\n}\n

"},{"location":"fundamental/cpp/2/array/#_9","title":"\u591a\u7dad\u9663\u5217","text":""},{"location":"fundamental/cpp/2/array/#_10","title":"\u5ba3\u544a","text":"
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100][100][10];\n\n    a[0][99][9] = 1; //write into memory\n\n    cout << a[0][99][9] << endl; \n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/array/#_11","title":"\u5e38\u898b\u932f\u8aa4","text":"

\u4f60\u6216\u8a31\u5728\u5176\u4ed6\u5730\u65b9\u770b\u904e\u9019\u6a23\u7684\u7a0b\u5f0f\uff0c\u7528\u65bc\u5ba3\u544a\u4e00\u500b\u4e0d\u56fa\u5b9a\u5927\u5c0f\u7684\u9663\u5217\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int n;\n    cin >> n;\n    int arr[n];\n\n    return 0;\n}\n

\u7136\u800c\uff0c\u9019\u500b\u5beb\u6cd5\u5b58\u5728\u4e00\u500b\u554f\u984c\uff0c\u9019\u7a2e\u7a0b\u5f0f\u7de8\u8b6f\u5668\u901a\u5e38\u6703\u81ea\u52d5\u628aarr\u5206\u914d\u7684\u5927\u4e00\u9ede\uff0c\u56e0\u70ba\u7de8\u8b6f\u5668\u4e0d\u77e5\u9053n\u9577\u600e\u6a23\uff0c\u6240\u4ee5\u53ea\u597d\u914d\u4e00\u500b\u6709\u9ede\u5927\u7684\u5927\u5c0f\u7d66\u9019\u500b\u9663\u5217\uff0c\u6211\u5011\u6703\u5728\u672a\u4f86\u7684\u8a18\u61b6\u9ad4\u7bc7\u7ae0\u4f86\u8a73\u7d30\u6558\u8ff0\u70ba\u5565\u6703\u51fa\u73fe\u9019\u500b\u60c5\u6cc1\u3002

\u800c\u4f60\u5728\u9019\u908a\u8981\u77e5\u9053\uff0c\u9019\u500b\u5beb\u6cd5\uff0c\u4e0d\u4e00\u5b9a\u6703\u6b63\u78ba\u3002

"},{"location":"fundamental/cpp/2/char/","title":"\u5b57\u5143\u578b\u614b","text":"

\u5728\u524d\u9762\u7684\u7ae0\u7bc0\uff0c\u6211\u5011\u6709\u63d0\u5230char\u662f\u5b57\u5143\u578b\u614b\uff0c\u5b57\u5143\uff0c\u60f3\u5fc5\u662f\u8868\u793a\u5b57\u7684\uff0c\u90a3\u9019\u578b\u614b\u5230\u5e95\u662f\u600e\u6a23\u8868\u793a\u5b57\uff1f

"},{"location":"fundamental/cpp/2/char/#ascii","title":"ASCII","text":"

\u4ee5\u524d\u7f8e\u570b\u5b9a\u7fa9\u4e86\u4e00\u500b\u6578\u503c\u8f49\u63db\u6210\u5b57\u7684\u4e00\u5f35\u8868\uff0c\u4f5c\u70ba\u6a19\u6e96\uff0c\u6211\u5011\u7a31\u5176\u70baASCII\uff0c\u4e0b\u9762\u662f\u8a73\u7d30\u5167\u5bb9\u3002

\u7531\u65bc\u7528\u7684\u7bc4\u570d\u8f03\u5c0f\uff0cascii\u7de8\u78bc\u53ea\u9700\u89811byte\uff0c\u6240\u4ee5char\u9019\u578b\u614b\u70ba1byte\u578b\u614b\u3002

"},{"location":"fundamental/cpp/2/char/#_2","title":"\u975e\u82f1\u6587\u5b57\u5143","text":"

\u9019\u908a\u4f60\u80af\u5b9a\u767c\u73fe\u5230\uff0c\u9019\u908a\u53ea\u6709\u5b9a\u7fa9\u82f1\u6587\u5b57\u6bcd\uff0c\u4e26\u6c92\u6709\u4e2d\u6587\u5b57\uff0c\u5c0d\u7684\uff0c\u4e2d\u6587\u81ea\u6709\u53e6\u5916\u4e00\u5957\u7de8\u78bc\u6a19\u6e96\uff0c\u53ef\u80fd\u4f7f\u7528\u8d85\u904e1\u500bbyte\u5132\u5b58\u5b57\u5143\uff0c\u50cf\u662fBig-5\u963f\uff0c\u4ee5\u53ca\u73fe\u5728\u5927\u5bb6\u5e38\u7528\u7684Unicode\u842c\u570b\u78bc\uff0c\u8a73\u7d30\u5167\u5bb9\u9019\u908a\u4e0d\u505a\u904e\u591a\u8d05\u8ff0\u3002

"},{"location":"fundamental/cpp/2/char/#_3","title":"\u5ba3\u544a\u65b9\u5f0f","text":"
#include <iostream>\n\nint main(){\n\n    char A_test = 41;\n    char a_test = 'a';\n\n    cout << A << a << endl;\n\n    return 0;\n}\n
output
Aa\n

\u7b2c\u4e00\u500b\u65b9\u5f0f\u70ba\u76f4\u63a5\u7d66\u4e88\u5ba3\u544aascii\u7684\u503c\uff0ccout\u5728\u8f38\u51fa\u6642\uff0c\u6703\u628a\u5b83\u8f49\u6210ascii\u5c0d\u61c9\u7684\u5b57\u5143\uff0c\u4e5f\u5c31\u662fA\u3002

\u7b2c\u4e8c\u7a2e\u65b9\u5f0f\u70ba\u544a\u8a34\u7de8\u8b6f\u5668\u6211\u9019\u908a\u8981\u7d66\u7684\u503c\u70ba\u5b57\u5143\uff0c\u6211\u5011\u628a\u8981\u8f38\u5165\u7684\u5b57\u5143\u7528\u55ae\u5f15\u865f\\'\u5305\u8d77\u4f86\uff0c\u5c31\u544a\u8a34\u7de8\u8b6f\u5668\u9019\u662f\u5b57\u5143\uff0c\u800c\u8f38\u51fa\u6642\u6703\u628a\u9019\u500b\u5b57\u5143\u5370\u51fa\u4f86\u3002

\u6240\u4ee5\u7d50\u679c\u5c31\u5982\u4e0b\u65b9output\u6240\u8ff0\u3002

"},{"location":"fundamental/cpp/2/char/#_4","title":"\u8df3\u812b\u5b57\u5143","text":"

\u5728\u5b57\u4e32\u7576\u4e2d\uff0c\u6211\u5011\u7528\u4e86\"\"\u8868\u793a\u5b57\u4e32\u7684\u5167\u5bb9\u7bc4\u570d\uff0c\u4f46\u662f\u5982\u679c\u4eca\u5929\u6211\u5011\u60f3\u5370\"\uff0c\u8a72\u600e\u6a23\u505a\uff1f

\u4f7f\u7528\u8df3\u812b\u5b57\u5143\u963f\uff01

\u8df3\u812b\u5b57\u5143\u7bc4\u4f8b
#include <iostream>\n\nint main(){\n\n\n    cout << '\\\"' << endl;\n\n    return 0;\n}\n

\u9664\u4e86\u9019\u500b\uff0c\u9084\u6709\u4e0b\u9762\u9019\u4e9b\u4f8b\u5b50\uff1a

\u63db\u884c\u7bc4\u4f8b
#include <iostream>\n\nint main(){\n\n\n    cout << '\\n' << endl;\n\n    return 0;\n}\n
\u55ae\u5f15\u865f\u7bc4\u4f8b
#include <iostream>\n\nint main(){\n\n\n    cout << '\\'' << endl;\n\n    return 0;\n}\n

reference

  1. https://www.commfront.com/pages/ascii-chart
"},{"location":"fundamental/cpp/2/floating/","title":"\u6d6e\u9ede\u6578\u578b\u614b","text":"

\u5728\u524d\u9762\u7684\u7bc7\u7ae0\uff0c\u76f8\u4fe1\u4f60\u5c0d\u5c0f\u6578\u9ede\u7684\u5132\u5b58\u4e0d\u964c\u751f\u4e86\uff0c\u90a3\u5982\u679c\u4eca\u5929\u8981\u5b58\u7684\u662f\u6709\u5c0f\u6578\u9ede\u7684\u6578\u5b57\uff1f

\u5728\u5f88\u4e45\u5f88\u4e45\u5f88\u4e45\u4ee5\u524d\uff0c\u5c0f\u6578\u9ede\u7684\u5b58\u6cd5\u662f\u6c92\u6709\u6a19\u6e96\u7684\uff0c\u4f46\u7d93\u904e\u6642\u9593\u7684\u6f14\u8b8a\u904e\u5f8c\uff0c\u4eba\u5011\u767c\u73fe\u904e\u65bc\u6df7\u4e82\uff0c\u65bc\u662f\u5171\u540c\u5236\u5b9a\u4e86\u4e00\u500b\u5171\u540c\u7684\u5b58\u6cd5\uff0c\u800c\u9019\u500b\u6a19\u6e96\u5c31\u662f IEEE 754\u3002

"},{"location":"fundamental/cpp/2/floating/#_2","title":"\u79d1\u5b78\u8a18\u865f","text":"

\u5728\u8ac7\u5230IEEE 754\u7684\u6a19\u6e96\u524d\uff0c\u6211\u5011\u5148\u4f86\u8ac7\u4e00\u4e0b\u79d1\u5b78\u8a18\u865f\uff0c\u56e0\u70ba\u79d1\u5b78\u8a18\u865f\u7684\u8a31\u591a\u7cbe\u9ad3\u8207IEEE 754\u76f8\u8fd1\u3002

\u5982\u679c\u6211\u5011\u8981\u7528\u5fc5\u9808\u7528\u79d1\u5b78\u8a18\u865f\u8868\u793a\u4e00\u500b\u8b8a\u6578\uff0c\u5fc5\u7136\u6703\u662f\u9019\u7a2e\u5f62\u5f0f\u3002

\\[ -1001 = -1.001 * 10^3 \\] \\[ 0.001 = 1*10^{-3} \\]

\u9019\u662f\u5c0d\u65bc\u5341\u9032\u4f4d\u6642\u7684\u72c0\u6cc1\uff0c\u5982\u679c\u4eca\u5929\u662f\u4e8c\u9032\u4f4d\u7684\u8a71\uff0c\u4e8c\u9032\u4f4d\u7684\u79d1\u5b78\u8a18\u865f\u8981\u9577\u9019\u6a23\uff1a

\\[ 0.5_{10} = {1 * 2^{-1}} = 0.1_2\\]

\u800cIEEE 754\u5c31\u662f\u4f9d\u7167\u9019\u500b\u5f62\u5f0f\u5b9a\u7fa9\u7684\u3002

"},{"location":"fundamental/cpp/2/floating/#ieee_754","title":"IEEE 754","text":""},{"location":"fundamental/cpp/2/floating/#_3","title":"\u55ae\u7cbe\u5ea6","text":"

\u55ae\u7cbe\u5ea6\u4f7f\u75284byte\u505a\u5b58\u5132\uff0c\u7b2c\u4e00\u500bbit\u5132\u5b58\u6b63\u8ca0\u7684\u8cc7\u8a0a\uff0c\u63a5\u4e0b\u4f86\u76847\u500bbit\u5132\u5b58\u6307\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u7684^{-1}\\)\uff09\uff0c\u5269\u4e0b\u7684\u90e8\u4efd\u5132\u5b58\u5be6\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u76841\\)\uff09\u3002 \u4e0b\u65b9\u70ba\\(1.0*2^{-1}\\)\u5728\u8a18\u61b6\u9ad4\u4e2d\u7684\u72c0\u614b\u3002

0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

\u311f\uff0c\u9ec3\u8272\u7684\u90e8\u4efd\u61c9\u8a72\u70ba-1\u55ce\uff1f\u70ba\u4f55\u8f49\u63db\u621010\u9032\u4f4d\u70ba62?

\u9019\u662f\u56e0\u70ba\u898f\u7bc4\u898f\u5b9a\u4e86\uff0c\u9ec3\u8272\u5167\u7684\u503c\u9084\u8981\u518d\u526a\u4e0a\\(2^{7-1}-1=63\\)\u624d\u6703\u662f\u6211\u5011\u8981\u7684\u6307\u6578\uff0c\u9019\u4e03\u683c\u4e26\u4e0d\u7167\u8457\u524d\u4e00\u7ae0\u8ca0\u6578\u898f\u5247\u8d70(\u4e5f\u6c92\u6709\u5fc5\u8981)\u3002

\u63a5\u8457\u4f60\u61c9\u8a72\u6703\u6709\u7b2c\u4e8c\u500b\u7591\u554f\uff1f\u70ba\u4f55\u5269\u9918\u7684\u5be6\u6578\u90e8\u4efd\u7686\u70ba0\u3002

\u8b93\u6211\u5011\u56de\u60f3\u770b\u770b\u79d1\u5b78\u8a18\u865f\uff0c\u5176\u5be6\u5b83\u9084\u53ef\u4ee5\u7c21\u5316\u6210\\((1+a)*10^b\\space_{0 \\le a < 1}\\)\u9019\u500b\u5f62\u5f0f\uff0c\u800c\u6211\u5011\u5176\u5be6\u53ea\u8981\u5132\u5b58a\u7684\u90e8\u4efd\u5c31\u597d\u4e86\uff0c\u9019\u908a\u4e5f\u662f\u9019\u500b\u9053\u7406\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c0.0\u5728IEEE\u4e2d\u5b9a\u7fa9\u70ba\u5168\u90e8\u7684bit\u90fd\u70ba0\uff0c\u5982\u8868\u683c\u6240\u793a\uff1a

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

\u6d6e\u9ede\u6578\u7576\u4e2d\u9084\u6709\u4e00\u500b\u7279\u5225\u7684\u6578\uff0c\u6211\u5011\u7a31\u70ba\u5b83\u70baNot a Number\uff0c\u6216\u8005\u53c8\u53eb\u505aNaN\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u70ba\u6240\u6709\u7684bit\u90fd\u70ba1\uff0c\u5982\u8868\u683c\u6240\u793a:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ..."},{"location":"fundamental/cpp/2/floating/#_4","title":"\u8aa4\u5dee","text":"

\u7d30\u5fc3\u7684\u4f60\u5728\u9019\u908a\u61c9\u8a72\u6703\u6ce8\u610f\u5230\uff0c\u6709\u4e9b\u6578\u5176\u5be6\u662f\u4e0d\u80fd\u88ab\u6709\u9650\u7684\u4e8c\u9032\u4f4d\u5c0f\u6578\u8868\u793a\u7684\uff0c\u50cf\u662f0.3\uff0c\u8f49\u6210\u4e8c\u9032\u4f4d\u7684\u8a71\u6703\u662f\u7121\u9650\u5c0f\u6578,\u800c\u9019\u7a2e\u5b58\u6cd5\u662f\u6c92\u8fa6\u6cd5\u5b58\u7121\u9650\u591a\u500b\u5c0f\u6578\u7684\uff0c\u6240\u4ee5\u67d0\u4e9b\u4f4d\u5143\u5f8c\u52e2\u5fc5\u6703\u88ab\u6368\u68c4\uff0c\u800c\u9020\u6210\u8207\u8f38\u5165\u4e0d\u4e00\u6a23\uff0c\u9020\u6210\u8aa4\u5dee\u7684\u767c\u751f\uff0c\u4e0b\u65b9\u70ba\u9019\u985e\u8aa4\u5dee\u7684\u6700\u7d93\u5178\u554f\u984c\u76f8\u95dc\u9023\u7d50\uff1a

(0.1+0.2=0.30000000000000004)

\u7e3d\u800c\u8a00\u4e4b\uff0c\u7121\u8ad6\u5982\u4f55\u8acb\u8a18\u4f4f\u9019\u53e5\u8a71:

\u7b97\u9322\u7528\u6d6e\u9ede\uff0c\u9072\u65e9\u88ab\u4eba\u6241\uff01

\u90a3\u6709\u54ea\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u5176\u7b97\u7684\u66f4\u6e96\u4e00\u4e9b\uff1f

\u52a0\u5927\u7cbe\u6e96\u5ea6\uff0c\u800c\u4e8b\u5be6\u4e0a\uff0c\u6211\u5011\u53ef\u4ee5\u52a0\u5927\u4ed6\u7684\u7cbe\u6e96\u5ea6\u3002

"},{"location":"fundamental/cpp/2/floating/#_5","title":"\u96d9\u7cbe\u5ea6","text":"

\u55ae\u7cbe\u5ea6\u4f7f\u75284byte\u505a\u5132\u5b58\uff0c\u7b2c\u4e00\u500bbit\u5132\u5b58\u6b63\u8ca0\u7684\u8cc7\u8a0a\uff0c\u63a5\u4e0b\u4f86\u76847+4=11\u500bbit\u5132\u5b58\u6307\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u7684^{-1}\\)\uff09\uff0c\u5269\u4e0b\u7684\u90e8\u4efd\u5132\u5b58\u5be6\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u76841.0\\)\uff09\u3002 \u4e0b\u65b9\u70ba\\(1*2^{-1}\\)\u5728\u8a18\u61b6\u9ad4\u4e2d\u7684\u72c0\u614b\u3002

0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 ...

\u9ec3\u8272\u7684\u90e8\u4efd\u8f49\u621010\u9032\u4f4d\u70ba1022\uff0c\u518d\u526a\u4e0a\u526a\u4e0a\\(2^{11-1}-1=1023\\)\uff0c\u6703\u7b49\u65bc-1\u3002

"},{"location":"fundamental/cpp/2/floating/#_6","title":"\u7e3d\u7d50","text":"

\u4f9d\u64daIEEE 754\u7684\u898f\u7bc4\uff0c\u6d6e\u9ede\u6578\u7684\u503c\u6703\u7b49\u65bc

\\[ \u6b63\u8ca0\uff08\u7d05\u8272\u7684\u90e8\u4efd\uff09* (1.\u85cd\u8272\u5f97\u90e8\u4efd)*2^(\u9ec3\u8272\u7684\u90e8\u4efd\u526a\u4e0a\u7279\u5b9a\u503c)\\]"},{"location":"fundamental/cpp/2/floating/#cc","title":"C/C++\u4e2d\u7684\u6d6e\u9ede\u6578","text":"

C/C++\u9664\u4e86\u63d0\u4f9b\u4e86\u5169\u7a2e\u7cbe\u5ea6\u7684\u6d6e\u9ede\u6578\uff0c\u5206\u5225\u70bafloat\u548cdouble\uff0c\u5206\u5225\u5c0d\u61c9\u55ae\u7cbe\u5ea6\u8207\u96d9\u7cbe\u5ea6\uff0c\u9084\u6709\u63d0\u4f9b\u66f4\u5927\u7bc4\u570d\u7684\u7cbe\u5ea6\uff0c\u4e00\u822c\u800c\u8a00\uff0cdouble\u5c31\u5df2\u7d93\u5f88\u5920\u7528\u4e86\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u66f4\u5927\u7684\u7cbe\u6e96\u5ea6\uff0c\u6216\u8005\u66f4\u5927\u7684\u7bc4\u570d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528long double\u3002

reference

  1. https://en.cppreference.com/w/cpp/language/types#Range_of_values
"},{"location":"fundamental/cpp/2/interger/","title":"\u6574\u6578","text":"

\u5728\u96fb\u8166\u7684\u4e16\u754c\u4e2d\uff0c\u6211\u5011\u4f7f\u75280\u548c1\u4f86\u5132\u5b58\u8cc7\u6599\uff0c\u6211\u5011\u628a\u6b64\u7a31\u70ba1\u500bbit\uff0c\u6709\u4e86\u4e00\u9023\u4e32\u7684bit\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u4e8c\u9032\u4f4d\u4f86\u5132\u5b58\u4e00\u500b\u6578\u5b57\u3002

\u800c\u96fb\u8166\u4e2d\u7684\u57fa\u672c\u55ae\u4f4d\u70ba 8bits \uff0c\u6211\u5011\u7a31\u5176\u70ba1\u500bbyte\u3002

\u4e86\u89e3\u5230\u4e86\u9019\u4e9b\u57fa\u672c\u77e5\u8b58\uff0c\u8b93\u6211\u5011\u770b\u770b\u6574\u6578\u662f\u5982\u4f55\u5b58\u7684\u3002

"},{"location":"fundamental/cpp/2/interger/#_2","title":"\u6b63\u6574\u6578","text":"

\u6b63\u5982\u524d\u9762\u6240\u8aaa\uff0c\u6211\u5011\u628a\u4e00\u500b\u6578\u5b57\u5f9e\u5341\u9032\u4f4d\u8f49\u6210\u4e8c\u9032\u4f4d\u5f8c\uff0c\u5c31\u53ef\u4ee5\u653e\u9032\u56fa\u5b9a\u5927\u5c0f\u7684\u8a18\u61b6\u9ad4\u5b58\u4e86\uff0c\u4e0b\u9762\u70ba\u5177\u9ad4\u904e\u7a0b\u3002

"},{"location":"fundamental/cpp/2/interger/#_3","title":"\u6ea2\u4f4d","text":"

\u5047\u5982\u4eca\u5929\u8981\u5b58\u7684\u6578\u653e\u9032\u56fa\u5b9a\u5927\u5c0f\u7684\u8a18\u61b6\u9ad4\u592a\u5927\u4e86\uff0c\u5b58\u4e0d\u4e0b\u600e\u8fa6\uff1f

\u4e1f\u6389\u4e0d\u7ba1\u5c31\u597d\u4e86\u963f\uff01

\u8b8a\u6578\u7684\u503c\u5982\u679c\u8d85\u904e\u8a18\u61b6\u9ad4\u80fd\u5b58\u7684\u4e0a\u9650\uff0c\u5247\u7a31\u70ba\u6ea2\u4f4d\uff0c\u6ea2\u4f4d\u5728\u6578\u503c\u904b\u7b97\u4e0a\u662f\u4e00\u500b\u7cbe\u5999\u7684\u8a2d\u8a08\uff0c\u5728\u7b49\u7b49\u7684\u7bc4\u4f8b\u6703\u5728\u8aaa\u660e\u3002

"},{"location":"fundamental/cpp/2/interger/#_4","title":"\u8ca0\u6578","text":"

\u7d93\u904e\u524d\u9762\u7684\u5f71\u7247\u7bc4\u4f8b\uff0c\u60f3\u5fc5\u4f60\u5c0d\u6b63\u6574\u6578\u7684\u5132\u5b58\u6709\u4e00\u5b9a\u7684\u6982\u5ff5\uff0c\u90a3\u5982\u679c\u4eca\u5929\u8981\u5b58\u7684\u662f\u8ca0\u6578\uff0c\u8981\u600e\u6a23\u505a\uff1f

\u5728\u8ac7\u53ca\u600e\u6a23\u505a\u4e4b\u524d\uff0c\u6211\u5011\u5148\u8ac7\u4e00\u4e0b\u88dc\u6578\u7684\u6982\u5ff5\u3002

"},{"location":"fundamental/cpp/2/interger/#_5","title":"\u4e00\u88dc\u6578","text":"

\u5728\u96fb\u8166\u7576\u4e2d\uff0c\u6211\u5011\u8b93\u6700\u5de6\u908a\u7684bit\u4ee3\u8868\u8457\u6b63\u8ca0\u3002\u5982\u679c\u662f0\u4ee3\u8868\u6b64\u6578\u70ba\u6b63\uff0c\u5982\u679c\u70ba\u8ca0\u5247\u5176\u9918bit 0 -> 1 \u3001 1 -> 0\uff0c\u4f8b\u5982\uff1a

\\[121_{10} = 01111110_{2}\\] \\[-121_{10} = 100000001_{2}\\]"},{"location":"fundamental/cpp/2/interger/#_6","title":"\u4e8c\u88dc\u6578","text":"

\u4e8c\u88dc\u6578\u7684\u7279\u6027\u8207\u4e00\u88dc\u6578\u76f8\u8fd1\uff0c\u552f\u4e00\u7684\u5340\u5225\u662f\u7576\u4ed6\u662f\u8ca0\u6578\u6642\uff0c\u9700\u8981\u5728\u8f49\u6210\u4e00\u88dc\u6578\u5f8c+1\uff0c\u4f8b\u5982\uff1a

\\[121_{10} = 01111110_{2''}\\] \\[-121_{10} = 10000010_{2''}\\] \\[\uff08\u5047\u8a2d\u6211\u5011\u53ea\u67091byte=8bits\u4f86\u5b58\u8cc7\u6599\uff09\\]

\u63a5\u4e0b\u4f86\u4f60\u61c9\u8a72\u6703\u6709\u4e00\u500b\u7591\u554f\uff0c\u70ba\u4f55\u6211\u5011\u9700\u8981\u4e8c\u88dc\u6578\uff1f\u9019\u4e0d\u662f\u591a\u4e00\u500b\u6b65\u9a5f\u55ce\uff1f \u8b93\u6211\u5011\u8a66\u8457\u628a 121 \u548c -121 \u7684\u4e8c\u88dc\u6578\u76f8\u52a0\uff0c\u4e26\u5c0e\u5165\u6ea2\u4f4d\u7684\u6982\u5ff5\uff1a

\\[ 01111110_{2''} + 10000010_{2''} = _{1}00000000_{2''}\\]

\u6700\u5de6\u908a\u76841\u767c\u751f\u6ea2\u4f4d\uff0c\u6703\u6d88\u5931\uff08\u6545\u4f7f\u5176\u5b57\u9ad4\u7e2e\u5c0f\uff09\uff0c\u7d50\u679c\u70ba\u6b63\u78ba\u7b54\u6848\uff0c\u795e\u5947\u5c0d\u5427\uff01

\u4e8b\u5be6\u4e0a\uff0c\u96fb\u8166\u4e2d\u6211\u5011\u53ea\u6709\u8a2d\u8a08\u52a0\u6cd5\u5668\uff0c\u4e26\u6c92\u6709\u8a2d\u8a08\u6e1b\u6cd5\u5668\uff0c\u5c31\u662f\u56e0\u70ba\u6211\u5011\u53ef\u4ee5\u900f\u904e\u4e8c\u88dc\u6578+\u6ea2\u4f4d\u9019\u5169\u500b\u5de7\u5999\u7684\u8a2d\u8a08\uff0c \u4f86\u9054\u6210\u6574\u6578\u7684\u5feb\u901f\u904b\u7b97\u3002

"},{"location":"fundamental/cpp/2/interger/#c","title":"C++\u4e2d\u7684\u6574\u6578\u578b\u614b","text":"

\u5728\u4e86\u89e3\u5230\u9019\u4e9b\u80cc\u5f8c\u77e5\u8b58\u5f8c\uff0c\u6211\u5011\u4f86\u8a0e\u8ad6C++\u6240\u6709\u7684\u6574\u6578\u578b\u614b\u3002 \u7531\u65bc\u6bcf\u500b\u8b8a\u6578\u6240\u9700\u8981\u7684\u7bc4\u570d\u4e0d\u540c\uff0cC++\u7279\u5225\u8a2d\u8a08\u4e86\u597d\u5e7e\u7a2e\u7684\u578b\u614b\uff0c\u4f86\u8b93\u4f60\u4f7f\u7528\u3002

\u800c\u6bcf\u500b\u578b\u614b\u7684\u7bc4\u570d\uff0c\u7c21\u5316\u904e\u5f8c\u5927\u81f4\u4e0a\u53ef\u4ee5\u5206\u6210\u4e0b\u9762\u5e7e\u500b\u72c0\u6cc1\uff08\u8a73\u7d30\u8acb\u53c3\u8003reference\u7684\u9023\u7d501\uff09:

type 32 bits compiler 64 bits compiler unsigned char \\(-2^7\\sim2^7-1\\) \\(-2^7\\sim2^7-1\\) unsigned short \\(-2^{15}\\sim2^{15}-1\\) \\(-2^{15}\\sim2^{15}-1\\) unsigned int \\(-2^{31}\\sim2^{31}-1\\) \\(-2^{31}\\sim2^{31}-1\\) unsigned long \\(-2^{31}\\sim2^{31}-1\\) \\(-2^{63}\\sim2^{63}-1\\) unsigned long long \\(-2^{63}\\sim2^{63}-1\\) \\(-2^{63}\\sim2^{63}-1\\)

\u4e00\u822c\u800c\u8a00\uff0c\u5728\u4f7f\u7528\u6574\u6578\u578b\u614b\uff0c\u6211\u5011\u6703\u5ba3\u544aint\u505a\u4f7f\u7528\u3002 \u90a3C++\u540c\u6642\u4e5f\u63d0\u4f9b\u6c92\u6709\u8ca0\u6578\u7684\u578b\u614b\uff0c\u53ea\u8981\u5728\u9019\u4e9b\u578b\u614b\u524d\u9762\u52a0\u4e0aunsigned\u5373\u53ef\u3002

type 32 bits compiler 64 bits compiler char \\(0\\sim2^8-1\\) \\(0\\sim2^{8}-1\\) short \\(0\\sim2^{16}-1\\) \\(0\\sim2^{16}-1\\) int \\(0\\sim2^{32}-1\\) \\(0\\sim2^{32}-1\\) long \\(0\\sim2^{32}-1\\) \\({0}\\sim2^{64}-1\\) long long \\({0}\\sim2^{64}-1\\) \\({0}\\sim2^{64}-1\\)

\u9019\u908a\u6709\u500b\u9ede\u8981\u6ce8\u610f\u4e00\u4e0b\uff0c\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u6cc1\u4e0b\uff0c\u4f60\u7684\u7de8\u8b6f\u5668\u53ef\u80fd\u6703\u662f32\u4f4d\u5143\u7684\uff0clong\u7684\u5927\u5c0f\u6703\u6bd4\u8f03\u5c0f\uff0c\u6240\u4ee5\u5728\u5ba3\u544a8byte(64bits)\u5927\u5c0f\u7684\u6574\u6578\u578b\u614b\u6642\uff0c\u8acb\u76e1\u91cf\u4f7f\u7528long long\uff0c\u4ee5\u907f\u514d\u51fa\u932f\u3002

* \u8acb\u6ce8\u610f\uff0cchar\u540c\u6642\u70ba\u5b57\u5143\u578b\u614b\uff0c\u8a73\u7d30\u6703\u5728\u5f8c\u9762\u8aaa\u660e\u3002

"},{"location":"fundamental/cpp/2/interger/#_7","title":"\u6574\u6578\u578b\u614b\u7684\u76f8\u95dc\u904b\u7b97","text":"

C++ \u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u56db\u5247\u904b\u7b97\u3002

+ \u4ee3\u8868\u52a0\u865f\u3002

-\u4ee3\u8868\u6e1b\u6cd5\u904b\u7b97\u3002

*\u4ee3\u8868\u4e58\u6cd5\u904b\u7b97\u3002

/\u4ee3\u8868\u9664\u6cd5\u904b\u7b97\u3002 \u8acb\u6ce8\u610f\uff0c\u6574\u6578\u7684\u9664\u6cd5\u4e0d\u6703\u6709\u5c0f\u6578\u9ede\u3002

%\u4ee3\u8868\u53d6\u9918\u6578\u3002

\u8acb\u8a66\u8457\u57f7\u884c\u4e0b\u65b9\u7bc4\u4f8b\u7a0b\u5f0f\uff0c\u4e26\u5617\u8a66\u53bb\u7406\u89e3\u5b83\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a = 126, b = -126;\n    int ans1 = a + b;\n    int ans2 = a - b ;\n    int ans3 = a * b * a * b * a * b ;\n    int ans4 = a / b;\n    int ans5 = (a*b)%2;\n\n    cout << ans1 << \" \" << ans2 << \" \"  << ans3 << \" \"  << ans4 << \" \"  << ans5 << endl;\n\n\n    return 0;\n}\n
reference
  1. https://en.cppreference.com/w/cpp/language/types
"},{"location":"fundamental/cpp/2/practice/","title":"\u7df4\u7fd2","text":"

\u5728\u7d93\u904e\u524d\u9762\u7684\u7bc7\u7ae0\uff0c\u76f8\u4fe1\u4f60\u5c0d\u8b8a\u6578\u578b\u614b\u5df2\u7d93\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\uff01\u63a5\u4e0b\u4f86\u6211\u5011\u5c07\u7df4\u7fd2\u76f8\u95dc\u984c\u76ee\uff0c\u4ee5\u78ba\u4fdd\u4f60\u771f\u7684\u6709\u5b78\u9032\u53bb\uff01

\u5148\u8a66\u8457\u5beb\u5beb\u770b\uff0c\u4e0d\u6703\u7684\u5148\u8a66\u8457\u900f\u904e\u7db2\u8def\u67e5\u8a62\u4e26\u7406\u89e3\uff0c\u771f\u7684\u4e0d\u884c\u6216\u8005\u5beb\u5b8c\u518d\u4f86\u770b\u8a73\u89e3\uff01

"},{"location":"fundamental/cpp/2/practice/#_2","title":"\u7c21\u55ae\u52a0\u6cd5","text":"\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a, b;\n    cin >> a >> b;\n    cout << a + b << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/practice/#_3","title":"\u7cdf\u7cd5\uff0c\u6211\u767c\u71d2\u4e86\uff01","text":"

\u9019\u908a\u7684fixed\u662f\u5f37\u5236cout\u4f7f\u7528\u5c0f\u6578\u9ede\u8f38\u51fa\u6d6e\u9ede\u6578\uff0csetprecision(3) \u5247\u662f\u544a\u8a34cout\uff0c\u5370\u5230\u5c0f\u6578\u9ede\u4e0b\u7b2c\u4e09\u4f4d\uff01

\u800c\u7b2c11\u884c\u7684 (double)(n-32)\uff0c\u4ee3\u8868\u5f37\u5236\u8f49\u578b\uff0c\u628an-32\u63db\u6210\u6d6e\u9ede\u6578\u7684\u578b\u614b\uff0c\u8b93\u5f8c\u9762\u7684\u8a08\u7b97\u6703\u6709\u5c0f\u6578\u9ede\uff0c\u9019\u908a\u8acb\u8a18\u4f4f\uff0c\u6574\u6578\u7684\u56db\u5247\u904b\u7b97\u53ea\u6703\u6709\u6574\u6578\uff0c\u800c\u6d6e\u9ede\u6578\u7684\u56db\u5247\u904b\u7b97\u4e00\u6a23\u4e5f\u53ea\u6709\u6d6e\u9ede\u6578\uff01\uff0c\u6574\u6578\u9664\u4e0a\u4efb\u4f55\u4e00\u500b\u6574\u6578\uff0c\u662f\u4e0d\u53ef\u80fd\u51fa\u73fe\u6d6e\u9ede\u6578\u7684\uff01

\u7bc4\u4f8b\u7a0b\u5f0f
// https://zerojudge.tw/ShowProblem?problemid=d051\n\n#include<iostream>\n#include<iomanip>\n\nusing namespace std;\n\nint main(){\n    int n;\n    cin >> n;\n    double ans = (double)(n-32) * 5 / 9;\n    cout<< fixed\n        << setprecision(3) \n        << ans\n        << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/practive/","title":"\u7df4\u7fd2","text":"

\u5728\u7d93\u904e\u524d\u9762\u7684\u7bc7\u7ae0\uff0c\u76f8\u4fe1\u4f60\u5c0d\u8b8a\u6578\u578b\u614b\u5df2\u7d93\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\uff01\u63a5\u4e0b\u4f86\u6211\u5011\u5c07\u7df4\u7fd2\u76f8\u95dc\u984c\u76ee\uff0c\u4ee5\u78ba\u4fdd\u4f60\u771f\u7684\u6709\u5b78\u9032\u53bb\uff01

\u5148\u8a66\u8457\u5beb\u5beb\u770b\uff0c\u4e0d\u6703\u7684\u5148\u8a66\u8457\u900f\u904e\u7db2\u8def\u67e5\u8a62\u4e26\u7406\u89e3\uff0c\u771f\u7684\u4e0d\u884c\u6216\u8005\u5beb\u5b8c\u518d\u4f86\u770b\u8a73\u89e3\uff01

"},{"location":"fundamental/cpp/2/practive/#_2","title":"\u7c21\u55ae\u52a0\u6cd5","text":"\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a, b;\n    cin >> a >> b;\n    cout << a + b << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/practive/#_3","title":"\u7cdf\u7cd5\uff0c\u6211\u767c\u71d2\u4e86\uff01","text":"

\u9019\u908a\u7684fixed\u662f\u5f37\u5236cout\u4f7f\u7528\u5c0f\u6578\u9ede\u8f38\u51fa\u6d6e\u9ede\u6578\uff0csetprecision(3) \u5247\u662f\u544a\u8a34cout\uff0c\u5370\u5230\u5c0f\u6578\u9ede\u4e0b\u7b2c\u4e09\u4f4d\uff01

\u800c\u7b2c11\u884c\u7684 (double)(n-32)\uff0c\u4ee3\u8868\u5f37\u5236\u8f49\u578b\uff0c\u628an-32\u63db\u6210\u6d6e\u9ede\u6578\u7684\u578b\u614b\uff0c\u8b93\u5f8c\u9762\u7684\u8a08\u7b97\u6703\u6709\u5c0f\u6578\u9ede\uff0c\u9019\u908a\u8acb\u8a18\u4f4f\uff0c\u6574\u6578\u7684\u56db\u5247\u904b\u7b97\u53ea\u6703\u6709\u6574\u6578\uff0c\u800c\u6d6e\u9ede\u6578\u7684\u56db\u5247\u904b\u7b97\u4e00\u6a23\u4e5f\u53ea\u6709\u6d6e\u9ede\u6578\uff01\uff0c\u6574\u6578\u9664\u4e0a\u4efb\u4f55\u4e00\u500b\u6574\u6578\uff0c\u662f\u4e0d\u53ef\u80fd\u51fa\u73fe\u6d6e\u9ede\u6578\u7684\uff01

\u7bc4\u4f8b\u7a0b\u5f0f
// https://zerojudge.tw/ShowProblem?problemid=d051\n\n#include<iostream>\n#include<iomanip>\n\nusing namespace std;\n\nint main(){\n    int n;\n    cin >> n;\n    double ans = (double)(n-32) * 5 / 9;\n    cout<< fixed\n        << setprecision(3) \n        << ans\n        << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/string/","title":"\u5b57\u4e32","text":""},{"location":"fundamental/cpp/2/string/#_2","title":"\u5b57\u5143\u9663\u5217","text":"

\u4e00\u6bb5\u5b57\uff0c\u662f\u8907\u6578\u500b\u5b57\u6bcd\uff08\u6216\u7a31\uff1a\u5b57\u5143\uff09\u6240\u7d44\u6210\uff0c\u6240\u4ee5\u6211\u5011\u8981\u986f\u793a\u4e00\u6bb5\u5b57\uff0c\u53ef\u4ee5\u5ba3\u544a\u5b57\u5143\u9663\u5217\u4f86\u5132\u5b58\u3002

\u5b57\u5143\u9663\u5217\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    char arr[] = {'g','o','o','d'};\n\n    cout << arr << endl;\n\n    return 0;\n}\n
Output
good\n
"},{"location":"fundamental/cpp/2/string/#_3","title":"\u5b57\u4e32","text":"

\u5728C++\u4e2d\uff0c\u5b57\u4e32\u4f86\u7684\u6bd4\u5b57\u5143\u9663\u5217\u597d\u7528\uff0c\u4ed6\u8207\u5b57\u5143\u9663\u5217\u76f8\u4f3c\uff0c\u4f46\u6bd4\u5b57\u5143\u9663\u5217\u597d\u7528\u8a31\u591a\uff01

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u4e32\u7684\u7d50\u5c3e\uff0c\u5fc5\u5b9a\u662f\u7528\u8df3\u812b\u5b57\u5143\\0\u505a\u7d50\u5c3e\uff0c\u7528\u4ee5\u8868\u793a\u9019\u908a\u662f\u5b57\u4e32\u7684\u6700\u5f8c\u9762\u3002

H e l l o W o r l d \\0

\u4e0b\u65b9\u70ba\u76f8\u95dc\u7a0b\u5f0f\u7684\u7bc4\u4f8b\uff1a

\u5b57\u4e32\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    string str = \"Hello World\";\n\n    a[0] = 1; //write into memory\n\n    cout << \"Size: \" << str.size() << endl; // \u8b80\u53d6\u5b57\u4e32\u6709\u591a\u5c11\u5b57\n    cout << \"str[11]: \" << str[11] << endl; // \u5b58\u53d6\u5b57\u4e32\u7684\u7b2c11\u500b\u5b57\u5143\uff0c\u4e5f\u5c31\u662f\u8df3\u812b\u5b57\u5143'\\0'\n    cout << \"str[0]: \" << str[0] << endl; // \u5b58\u53d6\u5b57\u4e32\u7684\u7b2c0\u500b\u5b57\u5143\n\n    return 0;\n}\n
Output
11\n\nH\n
"},{"location":"fundamental/python/","title":"Python","text":"

\u55e8\uff0c\u6211\u662f cheung4843\uff0c\u662f Python \u7cfb\u5217\u7684\u4f5c\u8005\u3002\u5728\u4e00\u958b\u59cb\u57fa\u790e\u8a9e\u6cd5\u7684\u90e8\u5206\u6703\u6559\u5f97\u7279\u5225\u8a73\u7d30\uff0c\u6587\u7ae0\u4e2d\u7684\u5c08\u6709\u540d\u8a5e\u5c07\u6703\u4ee5\u9ec3\u8272\u8207\u7da0\u8272\u6a19\u793a\uff0c\u8acb\u4f60\u7a0d\u5fae\u6ce8\u610f\u4e00\u4e0b\uff0c\u82e5\u4e0d\u61c2\u4e5f\u6c92\u95dc\u4fc2\uff0c\u6709\u500b\u5927\u6982\u7684\u6982\u5ff5\u5c31\u597d\u3002

\u5728\u6bcf\u4e00\u7ae0\u7684\u6700\u5f8c\u90fd\u6703\u653e\u4e0a\u4e00\u4e9b\u7df4\u7fd2\u984c\uff0c\u8acb\u5617\u8a66\u5b8c\u6210\u4ed6\u5011!

\u4f60\u6703\u5728\u6587\u7ae0\u4e2d\u770b\u5230\u5f88\u591a\u6b4c\uff0c\u9019\u4e9b\u591a\u534a\u662f\u6211\u5728\u64b0\u5beb\u6587\u7ae0\u7684\u6642\u5019\u525b\u597d\u5728\u807d\u7684\u6b4c\u3002

Tears For Fears - Everybody Wants To Rule The World

\u56e0\u70ba\u9019\u500b\u5c08\u6848\u6b63\u5728\u65e9\u671f\u958b\u767c\u968e\u6bb5\uff0c\u6240\u4ee5\u52d5\u756b\u8207\u5716\u7247\u5c07\u6703\u5728\u672a\u4f86\u52a0\u5165\u3002

\u6700\u5f8c\uff0c\u795d\u4f60\u5b78\u7fd2\u6109\u5feb!

"},{"location":"fundamental/python/classes/","title":"Classes","text":""},{"location":"fundamental/python/classes/#introduction","title":"Introduction","text":"

\u56de\u60f3 Variable and Input - Variable \u4e2d\u6240\u8b1b\u7684:

Quote

\u5728 Python \u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u7269\u4ef6\u3002

\u5728 Python \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u5df1\u5b9a\u7fa9\u4e00\u500b\u65b0\u7684\u7269\u4ef6\uff0c\u9019\u500b\u7269\u4ef6\u7684\u5b9a\u7fa9\u65b9\u5f0f\u5c31\u662f\u985e\u5225(Class)\uff0c\u5b83\u5c31\u50cf\u662f\u4e00\u500b\u85cd\u5716\uff0c\u53ef\u4ee5\u7528\u4f86\u5275\u5efa\u7269\u4ef6\u3002\u800c\u4f9d\u6b64\u5275\u9020\u51fa\u7684\u7269\u4ef6\u5c31\u662f\u5be6\u9ad4(Instance)\u3002

"},{"location":"fundamental/python/classes/#define_a_class","title":"Define a Class","text":"

\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u60f3\u8981\u5b9a\u7fa9\u4e00\u500b\u985e\u5225 Ship\uff0c\u64c1\u6709\u5c6c\u6027(Attribute) hp, name, speed\uff0c\u4ee5\u53ca\u65b9\u6cd5(Method) move \u548c stop\u3002

class Ship:\n    # Class attribute\n    max_no_flags = 3\n\n    def __init__(self, hp, name, speed):\n        # Instance attribute\n        self.hp = hp\n        self.name = name\n        self.speed = speed\n\n    # Method\n    def move(self):\n        print(f\"{self.name} is moving at speed {self.speed}.\")\n\n    def stop(self):\n        self.speed = 0\n        print(f\"{self.name} has stopped.\")\n\n\n# Create instances\nTitanic = Ship(120, \"Titanic\", 21)\nCarpathia = Ship(100, \"RMS Carpathia\", 14)\n# Access class attributes\nprint(Ship.max_no_flags)\nprint(Titanic.max_no_flags)\nShip.max_no_flags = 4\nprint(Carpathia.max_no_flags)\n# Access instance attributes\nprint(Titanic.name)\nprint(Carpathia.name)\n# Call methods\nTitanic.move()\nCarpathia.stop()\n
Output
3\n3\n4\nTitanic\nRMS Carpathia\nTitanic is moving at speed 21.\nRMS Carpathia has stopped.\n

C\u00e9line Dion - My Heart Will Go On

max_no_flags \u662f\u4e00\u500b\u985e\u5225\u5c6c\u6027(Class attribute)\uff0c\u5b83\u662f\u5c6c\u65bc\u985e\u5225 Ship \u7684\uff0c\u53ea\u8981\u662f Ship \u7684\u5be6\u9ad4\u90fd\u53ef\u4ee5\u5b58\u53d6\uff0c\u7136\u800c\u53ea\u8981 Ship \u4fee\u6539\u4e86\u5b83\uff0c\u6240\u6709\u7684\u5be6\u9ad4\u90fd\u6703\u53d7\u5230\u5f71\u97ff\u3002

hp, name, speed \u662f\u5be6\u9ad4\u5c6c\u6027(Instance attribute)\uff0c\u5b83\u5011\u662f\u5c6c\u65bc Ship \u7684\u5be6\u9ad4\uff0c\u6bcf\u500b\u5be6\u9ad4\u90fd\u6709\u81ea\u5df1\u7684 name \u548c speed\u3002

move \u548c stop \u662f\u5be6\u9ad4\u65b9\u6cd5(Instance method)\uff0c\u5b83\u5011\u662f\u5c6c\u65bc Ship \u7684\u5be6\u9ad4\uff0c\u53ef\u4ee5\u88ab Ship \u7684\u5be6\u9ad4\u547c\u53eb\u3002

self \u662f\u4e00\u500b\u7279\u6b8a\u7684\u53c3\u6578\uff0c\u5b83\u4ee3\u8868\u4e86\u5be6\u9ad4\u672c\u8eab\u3002

__init__ \u662f\u4e00\u500b\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u7576\u6211\u5011\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5be6\u9ad4\u6642\uff0c\u5b83\u6703\u5728\u5275\u5efa\u5be6\u9ad4\u6642\u81ea\u52d5\u88ab\u547c\u53eb\u4f86\u521d\u59cb\u5316\u5be6\u9ad4\u3002\u4f46\u5b83\u4e26\u975e\u662f\u5fc5\u8981\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4e0d\u5beb\u3002

\u4f60\u53ef\u80fd\u6709\u5b78\u904e\u5176\u4ed6\u8a9e\u8a00\uff0c\u807d\u904e\u5efa\u69cb\u5b50(Constructor)\u9019\u500b\u540d\u8a5e\uff0c\u6982\u5ff5\u985e\u4f3c\u3002

"},{"location":"fundamental/python/classes/#inheritance","title":"Inheritance","text":"

\u5982\u679c\u6211\u9084\u60f3\u8981\u518d\u5b9a\u7fa9\u4e00\u500b\u985e\u5225 BattleShip\uff0c\u5b83\u64c1\u6709\u6240\u6709 Ship \u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u662f\u9084\u6709\u4e00\u4e9b\u984d\u5916\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u6211\u53c8\u4e0d\u60f3\u6574\u500b\u91cd\u5beb\u4e00\u6b21\uff0c\u9019\u6642\u5019\u5c31\u53ef\u4ee5\u7528\u5230\u7e7c\u627f(Inheritance)\u3002

from random import random\n\n\nclass Ship:\n    max_no_flags = 3\n\n    def __init__(self, hp, name, speed):\n        # Instance attribute\n        self.hp = hp\n        self.name = name\n        self.speed = speed\n\n    def move(self):\n        print(f\"{self.name} is moving at speed {self.speed}.\")\n\n    def stop(self):\n        self.speed = 0\n        print(f\"{self.name} has stopped.\")\n\n\nclass BattleShip(Ship):\n    def __init__(self, hp, name, speed, max_speed, damage, defense, acc):\n        super().__init__(hp, name, speed)\n        self.max_speed = max_speed\n        self.damage = damage\n        self.defense = defense\n        self.acc = acc\n\n    # Overriding the move method\n    def move(self, acceleration=1):\n        if self.speed < self.max_speed:\n            self.speed += acceleration\n        self.speed = min(self.speed, self.max_speed)\n        print(f\"{self.name} is moving at speed {self.speed}.\")\n\n    def stop(self):\n        self.speed = 0\n        print(f\"{self.name} has stopped.\")\n\n    # New method\n    def fire(self, target):\n        if random() <= self.acc:\n            damage = self.damage - target.defense\n            print(f\"{self.name} hit {target.name} for {damage} damage.\")\n            target.hp -= damage\n            if target.hp <= 0:\n                print(f\"{target.name} has been destroyed.\")\n        else:\n            print(f\"{self.name} missed {target.name}.\")\n\n\nBismarck = BattleShip(500, \"Bismarck\", 0, 30, 30, 10, 0.8)\nYamato = BattleShip(650, \"Yamato\", 0, 27, 41, 12, 0.6)\n\nBismarck.move(5)\nBismarck.fire(Yamato)\nYamato.fire(Bismarck)\nBismarck.stop()\n\nprint(type(Bismarck))\n
Possible Output
Bismarck is moving at speed 5.\nBismarck hit Yamato for 18 damage.\nYamato missed Bismarck.\nBismarck has stopped.\n<class '__main__.BattleShip'>\n

SABATON - Bismarck

\u53ea\u8981\u5728\u5b9a\u7fa9 BattleShip \u6642\uff0c\u628a Ship \u653e\u5728\u62ec\u865f\u88e1\uff0c\u5c31\u53ef\u4ee5\u8b93 BattleShip \u7e7c\u627f Ship \u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\u3002

\u9019\u88e1\u6211\u5f9e random \u6a21\u7d44\u4e2d\u5f15\u5165\u4e86 random \u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u96a8\u6a5f\u7522\u751f\u4e00\u500b \\([0, 1)\\) \u4e4b\u9593\u7684\u6d6e\u9ede\u6578\uff0c\u4f86\u6a21\u64ec\u5c04\u64ca\u7684\u6e96\u78ba\u5ea6\u3002

BattleShip \u7e7c\u627f\u4e86 Ship\uff0c\u6240\u4ee5\u5b83\u64c1\u6709 Ship \u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u9084\u591a\u4e86\u4e00\u4e9b\u81ea\u5df1\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u5176\u4e2d move \u65b9\u6cd5\u88ab\u8986\u5beb(Overriding)\u4e86\uff0c\u9019\u6a23 BattleShip \u7684\u5be6\u9ad4\u547c\u53eb move \u65b9\u6cd5\u6642\uff0c\u6703\u547c\u53eb BattleShip \u7684 move \u65b9\u6cd5\uff0c\u800c\u4e0d\u662f Ship \u7684\u3002\u800c\u65b0\u65b9\u6cd5 fire \u53ea\u6709 BattleShip \u6709\u3002

super() \u662f\u4e00\u500b\u7279\u6b8a\u7684\u5de5\u5177\uff0c\u5b83\u53d6\u5f97\u7236\u985e\u5225\uff0c\u56e0\u6b64\u6211\u5011\u5728 BattleShip \u7684 __init__ \u65b9\u6cd5\u4e2d\u53ef\u4ee5\u547c\u53eb Ship \u7684 __init__ \u65b9\u6cd5\u3002

"},{"location":"fundamental/python/classes/#encapsulation","title":"Encapsulation","text":"

\u5728 Python \u4e2d\uff0c\u6211\u5011\u7121\u6cd5\u50cf\u5176\u4ed6\u8a9e\u8a00\u4e00\u6a23\uff0c\u5c0d\u5c6c\u6027\u8a2d\u5b9a\u5b58\u53d6\u6b0a\u9650\uff0c\u4f46\u662f\u6211\u5011\u53ef\u4ee5\u7528\u547d\u540d\u898f\u5247(Naming Convention)\u4f86\u544a\u8a34\u4f7f\u7528\u8005\uff0c\u54ea\u4e9b\u5c6c\u6027\u662f\u4e0d\u61c9\u8a72\u76f4\u63a5\u5b58\u53d6\u7684\u3002

\u5728 Python \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u5c6c\u6027\u540d\u7a31\u91cd\u6574(Name Mangling)\u4f86\u9054\u5230\u9019\u500b\u76ee\u7684\uff0c\u53ea\u8981\u5728\u5c6c\u6027\u540d\u7a31\u524d\u9762\u52a0\u4e0a\u5169\u500b\u5e95\u7dda __\uff0cPython \u5c31\u6703\u628a\u5b83\u6539\u540d\u6210 _\u985e\u5225\u540d\u7a31__\u5c6c\u6027\u540d\u7a31\uff0c\u9019\u6a23\u5c31\u4e0d\u5bb9\u6613\u88ab\u5b58\u53d6\u5230\u3002

class Ship:\n    def __init__(self, hp, name, speed):\n        self.__hp = hp\n        self.__name = name\n        self.__speed = speed\n\n    def get_hp(self):\n        return self.__hp\n\n    def set_hp(self, hp):\n        if hp > 0:\n            self.__hp = hp\n        else:\n            print(\"HP must be greater than 0.\")\n

To be continued...

"},{"location":"fundamental/python/classes/#practice","title":"Practice","text":"

\u5176\u5be6\u9019\u7ae0\u6211\u4e0d\u592a\u6e05\u695a\u8981\u6559\u5230\u54ea\u500b\u5730\u6b65\uff0c\u56e0\u70ba\u9019\u662f\u504f\u958b\u767c\u7684\u6771\u897f\uff0c\u4f46\u6211\u662f\u60f3\u6559\u4f60\u89e3\u984c\uff0c\u6240\u4ee5\u5167\u5bb9\u9084\u6703\u518d\u8b8a\u52d5\uff0c\u4f46\u81f3\u5c11\u4f60\u61c9\u8a72\u77e5\u9053\u600e\u9ebc\u5b9a\u7fa9\u985e\u5225\uff0c\u4ee5\u53ca\u600e\u9ebc\u7528\u5c6c\u6027\u548c\u65b9\u6cd5\u3002

\u4e0b\u9762\u7684\u7df4\u7fd2\uff0c\u5176\u5be6\u4e5f\u53ef\u4ee5\u4e0d\u7528\u985e\u5225\u4f86\u5beb\uff0c\u4f46\u6211\u60f3\u8b93\u4f60\u719f\u6089\u4e00\u4e0b\u985e\u5225\u7684\u4f7f\u7528\u3002

Itsa - [C_AR76-\u6613] \u63d0\u6b3e\u6a5f\u7a0b\u5f0f

Reference code
class ATM:\n    def __init__(self, data):\n        self.accounts = {}\n        for account, password, money in data:\n            self.accounts[(account, password)] = money\n\n    def get_money(self, account, password):\n        if (account, password) in self.accounts:\n            print(self.accounts[(account, password)])\n        else:\n            print('error')\n\n\ndata = [[123, 456, 9000], [456, 789, 5000], [789, 888, 6000], [336, 558, 10000], [775, 666, 12000],\n        [566, 221, 7000]]\n\natm = ATM(data)\nN = int(input())\n\nfor _ in range(N):\n    account, password = map(int, input().split())\n    atm.get_money(account, password)\n

Itsa - [C_AR97-\u6613] \u77e9\u9663\u5206\u7d20\u4e58\u7a4d

Reference code
class Matrix:\n    def __init__(self, matrix):\n        self.matrix = matrix\n        self.rows = len(matrix)\n        self.cols = len(matrix[0])\n\n    def entrywise_product(self, other):\n        result = [[0 for _ in range(self.cols)] for _ in range(self.rows)]\n\n        for i in range(self.rows):\n            for j in range(self.cols):\n                result[i][j] = self.matrix[i][j] * other.matrix[i][j]\n\n        return Matrix(result)\n\n\nm, n = map(int, input().split())\nmat1 = [list(map(int, input().split())) for _ in range(m)]\nmat2 = [list(map(int, input().split())) for _ in range(m)]\n\nmatrix1 = Matrix(mat1)\nmatrix2 = Matrix(mat2)\n\nresult = matrix1.entrywise_product(matrix2)\n\nfor row in result.matrix:\n    print(*row)\n

* \u5728\u9019\u88e1\u7a31\u70ba Unpacking Operator

"},{"location":"fundamental/python/classes/#assignment","title":"Assignment","text":"

Itsa - [C_AR74-\u4e2d] \u5b78\u751f\u8cc7\u6599\u641c\u5c0b\u7a0b\u5f0f

Tip

\u4e0d\u7528\u6015\uff0c\u4f60\u53ef\u4ee5\u7684\u3002

"},{"location":"fundamental/python/dictionaries/","title":"Dictionaries","text":""},{"location":"fundamental/python/dictionaries/#introduction","title":"Introduction","text":"

\u4f86\u4ecb\u7d39\u672c\u4e3b\u984c\u4e2d\u6700\u5f8c\u4e00\u500b\u8cc7\u6599\u7d50\u69cb\uff1a\u5b57\u5178(Dictionaries)\uff0c\u662f\u7531\u9375(Key)\u548c\u503c(Value)\u6240\u7d44\u6210\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u7d93\u7531\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u9375\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u800c\u503c\u5247\u53ef\u4ee5\u662f\u4efb\u4f55\u578b\u614b\u7684\u8cc7\u6599\u3002

Question

  1. \u5e8f\u5c0d\u53ef\u4ee5\u505a\u70ba\u5b57\u5178\u7684\u9375\u55ce?
  2. \u4e32\u5217\u53ef\u4ee5\u505a\u70ba\u5b57\u5178\u7684\u9375\u55ce?
  3. \u5b57\u5178\u53ef\u4ee5\u505a\u70ba\u5b57\u5178\u7684\u503c\u55ce?

\u7559\u7d66\u4f60\u601d\u8003\uff0c\u4f60\u4e00\u5b9a\u77e5\u9053\u7b54\u6848\uff0c\u5c31\u7b97\u4f60\u6c92\u8fa6\u6cd5\u99ac\u4e0a\u77e5\u9053\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5beb\u7a0b\u5f0f\u4f86\u6e2c\u8a66\u3002

"},{"location":"fundamental/python/dictionaries/#create_a_dictionary","title":"Create a Dictionary","text":"

\u4f60\u53ef\u4ee5\u7528{}\u3001dict() \u6216\u8005 Comprehension \u4f86\u5efa\u7acb\u4e00\u500b\u5b57\u5178\u3002

scores = {\"Compiler\": 100, \"AWS\": 95, \"Data Science\": 92}\nprint(scores)\n\n# when keys are strings, using keyword arguments\ndirections = dict(North=\"\u2191\", South=\"\u2193\", East=\"\u2192\", West=\"\u2190\")\nprint(directions)\n\ncapital = dict([(\"Taiwan\", \"Taipei\"), (\"Japan\", \"Tokyo\"), (\"Korea\", \"Seoul\")])\nprint(capital)\n\n# comprehension\ncube = {x: x ** 3 for x in range(-2, 3)}\nprint(cube)\n
Output
{'Compiler': 100, 'AWS': 95, 'Data Science': 92}\n{'North': '\u2191', 'South': '\u2193', 'East': '\u2192', 'West': '\u2190'}\n{'Taiwan': 'Taipei', 'Japan': 'Tokyo', 'Korea': 'Seoul'}\n{-2: -8, -1: -1, 0: 0, 1: 1, 2: 8}\n

\u90a3\u70ba\u4ec0\u9ebc\u8aaa\u5b57\u5178\u662f\u6709\u5e8f\u7684\u5462?\u4f60\u591a\u57f7\u884c\u5e7e\u6b21\uff0c\u8f38\u51fa\u7684\u9806\u5e8f\u8207\u4f60\u5efa\u7acb\u7684\u9806\u5e8f\u662f\u4e00\u6a23\u7684\u55ce?

"},{"location":"fundamental/python/dictionaries/#operations","title":"Operations","text":""},{"location":"fundamental/python/dictionaries/#accessing_and_modifying_elements","title":"Accessing and Modifying elements","text":"

\u4f60\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u4e5f\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u4fee\u6539\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u6703\u5f97\u5230 KeyError\u3002

recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"King Gnu\"] = \"SPECIALZ\"\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"HEALTH\"] = \"Blue Monday\"\nprint(recent_listening[\"HEALTH\"])\n\nprint(recent_listening[\"ALI\"])\n
Output
\u98db\u884c\u8247\nSPECIALZ\nBlue Monday\nKeyError: 'ALI'\n

King Gnu - \u98db\u884c\u8247

Blue Monday

\u90a3\u9ebc\u8a72\u5982\u4f55\u6aa2\u67e5\u9375\u662f\u5426\u5b58\u5728\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 in \u4f86\u6aa2\u67e5\uff0c\u4f86\u907f\u514d\u932f\u8aa4\u3002

recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\n\nprint(\"King Gnu\" in recent_listening)\nprint(\"ALI\" in recent_listening)\n
Output
True\nFalse\n

Question

\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u5b57\u5178\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u9375\u5b58\u5728?

"},{"location":"fundamental/python/dictionaries/#removing_elements","title":"Removing elements","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 del \u4f86\u522a\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002

\u4f8b\u5982\u6211\u9019\u9663\u5b50\u90fd\u4e0d\u5beb Java \u4e86\uff0c\u6240\u4ee5\u628a Java \u5f9e\u6211\u7684\u5fc3\u4e2d\u522a\u9664\u3002

my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\nprint(my_love)\n\ndel my_love[\"Java\"]\nprint(my_love)\n
Output
{'python': 100, 'Java': 70, 'Js': 60}\n{'python': 100, 'Js': 60}\n
"},{"location":"fundamental/python/dictionaries/#iterating","title":"Iterating","text":"

\u76f4\u63a5\u4f86\u770b\u4f8b\u5b50\u5427\u3002

my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love:\n    print(key, my_love[key])\n
Output
python 100\nJava 70\nJs 60\n

\u5982\u679c\u4f60\u53ea\u60f3\u8981\u53d6\u5f97\u9375\u6216\u8005\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 keys() \u6216\u8005 values()\u3002

my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love.keys():\n    print(key)\n\nprint(my_love.keys())\n\nfor value in my_love.values():\n    print(value)\n\nprint(list(my_love.values()))\n
Output
python\nJava\nJs\ndict_keys(['python', 'Java', 'Js'])\n100\n70\n60\n[100, 70, 60]\n

\u90a3\u5982\u679c\u4f60\u60f3\u8981\u540c\u6642\u53d6\u5f97\u9375\u548c\u503c\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 items()\u3002

my_cat = {\"name\": \"Fat Orange\", \"age\": 12, \"is_cute\": True}\n\nfor key, value in my_cat.items():\n    print(f\"{key}: {value}\")\n
Output
name: Fat Orange\nage: 12\nis_cute: True\n

\u6211\u611b\u6211\u7684\u6a58\u8c93\u3002

"},{"location":"fundamental/python/dictionaries/#methods","title":"Methods","text":""},{"location":"fundamental/python/dictionaries/#get","title":"get","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 get() \u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u5f88\u65b9\u4fbf\u5594\u3002

my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info['name'])\nprint(my_info.get('hobbies')[1])\n\n# print(my_info['is_handsome']) # This will cause an error\nprint(my_info.get('is_handsome', 'Of course I am!'))\n
Output
Sean\nworking out\nOf course I am!\n

\u5e0c\u671b\u4f60\u4e0d\u8981\u89ba\u5f97\u6211\u81ea\u6200\u3002

"},{"location":"fundamental/python/dictionaries/#pop","title":"pop","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 pop() \u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u540c\u6642\u522a\u9664\u8a72\u9375\u503c\u5c0d\u3002

my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info.pop('age'))\nprint(my_info)\n
Output
20\n{'name': 'Sean', 'hobbies': ['coding', 'working out']}\n
"},{"location":"fundamental/python/dictionaries/#clear","title":"clear","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 clear() \u4f86\u6e05\u7a7a\u5b57\u5178\u3002

my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nmy_info.clear()\nprint(my_info)\n
Output
{}\n

umm... \u518d\u904e\u4e0d\u4e45\u6211\u5c31\u8981 21 \u6b72\u4e86\u3002

\u6211\u8a8d\u70ba\u9019\u4e9b\u65b9\u6cd5\u5c31\u5920\u7528\u4e86\uff0c\u5176\u4ed6\u7684\u4f60\u53ef\u4ee5\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u56c9~

"},{"location":"fundamental/python/dictionaries/#practice","title":"Practice","text":"

Itsa - [C_ST19-\u6613] \u6642\u9593\u8f49\u63db

Reference code

n = int(input())\n\ntime_diff = {\"TW\": 0, \"JA\": 60, \"USE\": -720, \"USC\": -780, \"USW\": -840, \"UK\": -480}\n\nfor _ in range(n):\n    time, cur, to = input().split()\n    minutes = int(time[:2]) * 60 + int(time[3:])\n    minutes += time_diff[to] - time_diff[cur]\n\n    if minutes < 0:\n        minutes += 1440\n    minutes %= 1440\n\n    h, m = divmod(minutes, 60)\n    print(f\"{h:02d}{m:02d} {to}\")\n
\u4e00\u5f8b\u5148\u8f49\u63db\u6210\u5206\u9418\uff0c\u518d\u9032\u884c\u6642\u5340\u8f49\u63db\uff0c\u6700\u5f8c\u518d\u8f49\u63db\u6210\u5c0f\u6642\u548c\u5206\u9418\u3002

divmod \u56de\u50b3\u4e00\u500b\u5e8f\u5c0d\uff0c\u5546\u8207\u9918\u6578\u3002

"},{"location":"fundamental/python/dictionaries/#assignment","title":"Assignment","text":"

Itsa - [C_AR111-\u6613] \u5c0d\u8a71\u6a5f\u5668\u4eba

Itsa - [C_AR152-\u6613] \u6b63\u6574\u6578\u7d71\u8a08

Tip

sorted() and key

Itsa - [C_AR42-\u6613] \u904e\u534a\u5143\u7d20

Itsa - [C_AR188-\u6613] \u9663\u5217\u5143\u7d20

Tip

\u9664\u4e86\u7528\u5b57\u5178\u4f86\u89e3\u984c\u4e4b\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5b78 Boyer\u2013Moore majority vote algorithm

"},{"location":"fundamental/python/functions/","title":"Functions","text":""},{"location":"fundamental/python/functions/#introduction","title":"Introduction","text":"

\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u56de\u60f3 Repetition Structures - Nested Loop \u4e2d\u7684\u4f8b\u5b50:

\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\([1, n]\\) \u9593\u7684\u6700\u5927\u8cea\u6578\u3002

n = int(input())\n\nfor i in range(n, 0, -1):\n    is_prime = True\n    for j in range(2, i):\n        if i % j == 0:\n            is_prime = False\n            break\n\n    if is_prime:\n        print(f\"{i} is the largest prime in [1, {n}]\")\n        break\n

\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u529f\u80fd\u662f\u6b63\u78ba\u7684\uff0c\u4f46\u662f\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u4e0d\u9ad8\uff0c\u56e0\u70ba\u7a0b\u5f0f\u78bc\u7684\u9577\u5ea6\u592a\u9577\uff0c\u4e26\u4e14\u7a0b\u5f0f\u78bc\u7684\u908f\u8f2f\u4e0d\u5920\u6e05\u6670\u3002

\u6211\u5011\u53ef\u4ee5\u628a\u5224\u65b7\u8cea\u6578\u7684\u90e8\u5206\u62bd\u51fa\u4f86\uff0c\u4e26\u4e14\u5c07\u4ed6\u5305\u88dd\u6210\u4e00\u500b\u51fd\u5f0f:

def is_prime(x):\n    for i in range(2, x):\n        if x % i == 0:\n            return False\n    return True\n\n\nn = int(input())\n\nfor i in range(n, 0, -1):\n    if is_prime(i):\n        print(f\"{i} is the largest prime in [1, {n}]\")\n        break\n

\u9019\u6a23\u7684\u597d\u8655\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u76f8\u540c\u7684\u7a0b\u5f0f\u78bc\u91cd\u8907\u4f7f\u7528\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u3002

\u90a3\u5982\u679c\u6211\u60f3\u8981\u9032\u4e00\u6b65\u7a0b\u5f0f\u78bc\u5c01\u88dd\u6210\u4e00\u500b\u63a5\u53d7\u6b63\u6574\u6578 \\(n\\) \u7684\u51fd\u5f0f\uff0c\u4e26\u4e14\u56de\u50b3 \\([1, n]\\) \u9593\u7684\u6700\u5927\u8cea\u6578\u5462?

def is_prime(x):\n    for i in range(2, x):\n        if x % i == 0:\n            return False\n    return True\n\n\ndef largest_prime(n):\n    for i in range(n, 0, -1):\n        if is_prime(i):\n            return i\n\n\nn = int(input())\n\nprint(f\"{largest_prime(n)} is the largest prime in [1, {n}]\")\n

\u4e0d\u66c9\u5f97\u4f60\u6709\u6c92\u6709\u611f\u53d7\u5230\uff0c\u7576\u7a0b\u5f0f\u78bc\u8d8a\u4f86\u8d8a\u9577\uff0c\u6211\u5011\u5c31\u8d8a\u9700\u8981\u51fd\u5f0f\u4f86\u5e6b\u52a9\u6211\u5011\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5\u66f4\u5bb9\u6613\u7684\u95b1\u8b80\u7a0b\u5f0f\u78bc\u3002

\u9019\u500b\u5f15\u5b50\u544a\u8a34\u6211\u5011\uff0c\u51fd\u5f0f\u662f\u4e00\u500b\u53ef\u4ee5\u5c07\u7a0b\u5f0f\u78bc\u5305\u88dd\u6210\u4e00\u500b\u7368\u7acb\u7684\u55ae\u4f4d\uff0c\u4e26\u4e14\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u7684\u5de5\u5177\u3002

"},{"location":"fundamental/python/functions/#define_a_function","title":"Define a Function","text":"

\u51fd\u5f0f\u7684\u5b9a\u7fa9\u662f\u4ee5 def \u958b\u982d\uff0c\u5f8c\u9762\u63a5\u8457\u51fd\u5f0f\u7684\u540d\u7a31\uff0c\u4ee5\u53ca\u62ec\u865f\u5167\u7684\u53c3\u6578(Parameter)\uff0c\u7e2e\u6392\u5167\u7684\u7a0b\u5f0f\u78bc\u5c31\u662f\u51fd\u5f0f\u7684\u5167\u5bb9\u3002

\u4f60\u53ef\u4ee5\u9078\u64c7\u662f\u5426\u8981\u5728\u51fd\u5f0f\u4e2d\u52a0\u4e0a return \u4f86\u56de\u50b3\u503c\uff0c\u5982\u679c\u6c92\u6709\uff0c\u51fd\u5f0f\u5247\u6703\u56de\u50b3 None\u3002

\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f greet\uff0c\u4ed6\u63a5\u53d7\u4e00\u500b\u53c3\u6578 name\uff0c\u4e26\u5370\u51fa Hello, {name}:

def greet(name):\n    print(f\"Hello, {name}\")\n\n\nprint(greet(\"World\"))\n
Output
Hello, World\nNone\n

greet(\"World\") \u6703\u5148\u5370\u51fa Hello, World\uff0c\u63a5\u8457\u56de\u50b3 None\u3002

\u5b57\u4e32 \"World\" \u88ab\u7a31\u70ba\u5f15\u6578(Argument)\uff0c\u800c name \u5247\u88ab\u7a31\u70ba\u53c3\u6578(Parameter)\u3002\u4f46\u4e5f\u4e0d\u7528\u592a\u62d8\u6ce5\u3002

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f freq(x) \u63a5\u53d7\u4e00\u500b\u6578\u5b57\u5b57\u4e32 x \uff0c\u56de\u50b3 0-9 \u7684\u51fa\u73fe\u6b21\u6578\uff0c\u4ee5\u5e8f\u5c0d\u7684\u65b9\u5f0f\u56de\u50b3:

def freq(x: str) -> tuple:\n    table = [0] * 10\n    for digit in x:\n        table[int(digit)] += 1\n\n    return tuple(table)\n\n\na, b = \"114514\", \"111445\"\nprint(freq(a) == freq(b))\n
Output
True\n

\u6211\u5011\u53ef\u4ee5\u5728\u53c3\u6578\u5f8c\u9762\u52a0\u4e0a : \u4f86\u6307\u5b9a\u53c3\u6578\u7684\u578b\u5225\uff0c\u4e26\u4e14\u5728\u51fd\u5f0f\u5f8c\u9762\u52a0\u4e0a -> \u4f86\u6307\u5b9a\u56de\u50b3\u503c\u7684\u578b\u5225\u3002

"},{"location":"fundamental/python/functions/#default_argument_values","title":"Default Argument Values","text":"

\u6709\u6642\u5019\u6211\u5011\u6703\u5e0c\u671b\u51fd\u5f0f\u7684\u53c3\u6578\u6709\u9810\u8a2d\u503c\uff0c\u9019\u6a23\u5728\u547c\u53eb\u51fd\u5f0f\u6642\u5c31\u4e0d\u9700\u8981\u586b\u5165\u5f15\u6578\u3002

\u4f8b\u5982\uff0c\u51fd\u5f0f max_of(x, k=1) \u63a5\u53d7\u5169\u500b\u53c3\u6578 x \u4e32\u5217\u8207 k \u6578\u5b57\uff0c\u56de\u50b3 x \u4e2d\u6700\u5927\u7684 k \u500b\u6578\u5b57\uff0c\u4ee5\u4e32\u5217\u7684\u65b9\u5f0f\u56de\u50b3\uff0c\u800c k \u7684\u9810\u8a2d\u503c\u70ba 1:

def max_of(x: list[int], k=1):\n    return sorted(x, reverse=True)[:k]\n\n\ny = [4, 8, 5, 3, 9]\nprint(max_of(y))\nprint(max_of(y, 2))\nprint(max_of(y, k=3))\n
Output
[9]\n[9, 8]\n[9, 8, 5]\n

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u51fd\u5f0f weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5) \u63a5\u53d7\u4e09\u500b\u53c3\u6578 math, english, programming \u4ee5\u53ca\u4e09\u500b\u9810\u8a2d\u503c math_weight=0.25, english_weight=0.25, programming_weight=0.5\uff0c\u56de\u50b3\u52a0\u6b0a\u5206\u6578:

def weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5):\n    return math * math_weight + english * english_weight + programming * programming_weight\n\n\nsean_score = weight_score(80, 90, 100)\nyaris_score = weight_score(math=82, math_weight=0.3, english=90, english_weight=0.3, programming=100,\n                           programming_weight=0.4)\nprint(sean_score, yaris_score)\n
Output
92.5 91.6\n

\u6709\u9810\u8a2d\u503c\u7684\u53c3\u6578\u5fc5\u9808\u653e\u5728\u6c92\u6709\u9810\u8a2d\u503c\u7684\u53c3\u6578\u5f8c\u9762\u3002\u4f46\u5982\u679c\u4f60\u5728\u547c\u53eb\u51fd\u5f0f\u7684\u6642\u5019\uff0c\u6307\u5b9a\u4e86\u53c3\u6578\u7684\u540d\u7a31\uff0c\u5247\u53ef\u4ee5\u4e0d\u7528\u9075\u5b88\u9019\u500b\u898f\u5247\u3002

\u5176\u5be6\u9084\u6709\u4e00\u4e9b\u795e\u5947\u7684\u7528\u6cd5\uff0c\u4f46\u5c31\u4e0d\u5728\u9019\u88e1\u8a0e\u8ad6\u4e86\u3002

"},{"location":"fundamental/python/functions/#lambda_function","title":"Lambda Function","text":"

\u9084\u8a18\u5f97 map \u55ce? \u4ed6\u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u61c9\u7528\u5230\u4e00\u500b\u4e32\u5217\u4e0a\u3002

\u5148\u56de\u61b6\u4e00\u4e0b\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u4f7f\u7528 map:

def square(x):\n    return x ** 2\n\n\nlst = [1, 2, 3, 4, 5]\nprint(list(map(square, lst)))\n\nsong_name = [\"I\", \"Really\", \"Want\", \"to\", \"Stay\", \"At\", \"Your\", \"House\"]\nprint(list(map(len, song_name)))\n
Output
[1, 4, 9, 16, 25]\n[1, 6, 4, 2, 4, 2, 4, 5]\n

I Really Want to Stay At Your House\u201d by Rosa Walton

\u9019\u7a2e\u63a5\u53d7\u51fd\u5f0f\u70ba\u53c3\u6578\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u9ad8\u968e\u51fd\u5f0f(Higher-Order Function)\u3002

\u4f46\u662f\uff0c\u5982\u679c\u51fd\u5f0f\u53ea\u6703\u88ab\u4f7f\u7528\u4e00\u6b21\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 lambda \u4f86\u5b9a\u7fa9\u4e00\u500b\u533f\u540d\u51fd\u5f0f:

lst = [1, 2, 3, 4, 5]\nprint(list(map(lambda x: x ** 2, lst)))\n
Output
[1, 4, 9, 16, 25]\n

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u7528\u904e min \u55ce? \u4ed6\u53ef\u4ee5\u627e\u51fa\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u6700\u5c0f\u503c\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u81ea\u8a02\u898f\u5247\uff0c\u4f8b\u5982\u6709\u4e00\u500b\u5ea7\u6a19\u4e32\u5217 points\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u627e\u51fa\u6700\u9760\u8fd1\u539f\u9ede\u7684\u9ede:

points = [(9, 2), (3, 4), (5, 6), (7, 8), (4, 8), (1, 3)]\nprint(min(points, key=lambda x: x[0] ** 2 + x[1] ** 2))\n
Output
(1, 3)\n

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u5169\u500b\u4e32\u5217 a \u8207 b\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u8a08\u7b97\u5169\u500b\u4e32\u5217\u7684\u5167\u7a4d:

a, b = [1, 2, 3], [4, 5, 6]\nprint(sum(map(lambda x, y: x * y, a, b)))\n
Output
32\n
"},{"location":"fundamental/python/functions/#factory_function","title":"Factory Function","text":"

\u6709\u6642\u5019\u6211\u5011\u6703\u5e0c\u671b\u51fd\u5f0f\u56de\u50b3\u53e6\u4e00\u500b\u51fd\u5f0f\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u5de5\u5ee0\u51fd\u5f0f(Factory Function)\u3002

\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f make_power_fun(n) \uff0c\u4ed6\u63a5\u53d7\u4e00\u500b\u6578\u5b57 n\uff0c\u56de\u50b3\u4e00\u500b\u51fd\u5f0f\uff0c\u9019\u500b\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u6578\u5b57 x\uff0c\u56de\u50b3 x \u7684 n \u6b21\u65b9:

def make_power_fun(n):\n    return lambda x: x ** n\n\n\npower_fun_2 = make_power_fun(2)\npower_fun_3 = make_power_fun(4)\n\nprint(power_fun_2(3))\nprint(power_fun_3(3))\n
Output
9\n81\n

\u9019\u6a23\u7684\u597d\u8655\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5730\u65b9\u4f7f\u7528\u4e0d\u540c\u7684 n\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u8907\u5b9a\u7fa9\u51fd\u5f0f\u3002

"},{"location":"fundamental/python/functions/#nested_function","title":"Nested Function","text":"

\u51fd\u5f0f\u53ef\u4ee5\u88ab\u5b9a\u7fa9\u5728\u53e6\u4e00\u500b\u51fd\u5f0f\u7684\u5167\u90e8\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u5de2\u72c0\u51fd\u5f0f(Nested Function)\u3002

\u4f8b\u5982\uff0c\u51fd\u5f0f collect_anagrams(words, target) \u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u4e32\u5217 words \u8207\u4e00\u500b\u5b57\u4e32 target\uff0c\u56de\u50b3 words \u4e2d\u8207\u4e92\u70ba target \u662f\u76f8\u540c\u5b57\u6bcd\u7570\u5e8f\u8a5e\u7684\u4e32\u5217:

\u65bc\u662f\u6211\u5728\u51fd\u5f0f\u7684\u5167\u90e8\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u8f14\u52a9\u51fd\u5f0f(Helper Function) is_anagram(x, y)\uff0c\u4ed6\u63a5\u53d7\u5169\u500b\u5b57\u4e32 x \u8207 y\uff0c\u56de\u50b3 x \u8207 y \u662f\u5426\u70ba\u76f8\u540c\u5b57\u6bcd\u7570\u5e8f\u8a5e:

def collect_anagrams(words, target):\n    def is_anagram(x, y):\n        return sorted(x) == sorted(y)\n\n    return [word for word in words if is_anagram(word, target)]\n\n\nstrs = [\"now\", \"won\", \"own\", \"no\", \"on\", \"www\"]\nprint(collect_anagrams(strs, \"onw\"))\n
Output
['now', 'won', 'own']\n

\u9019\u500b\u5728\u5237 LeeCode \u6642\u6703\u5e38\u5e38\u7528\u5230\u3002

"},{"location":"fundamental/python/functions/#recursion","title":"Recursion","text":"

\u4f86\u5230\u672c\u7ae0\u7684\u91cd\u982d\u6232\uff0c\u905e\u8ff4(Recursion)\u3002

\u905e\u8ff4\u662f\u4e00\u7a2e\u51fd\u5f0f\u547c\u53eb\u81ea\u5df1\u7684\u6280\u5de7\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u905e\u8ff4\u51fd\u5f0f(Recursive Function)\u3002

\u5148\u4f86\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(n!\\)\uff0c\u8ff4\u5708\u7684\u5beb\u6cd5\u662f\u9019\u6a23\u7684:

def factorial(n):\n    result = 1\n    for i in range(1, n + 1):\n        result *= i\n    return result\n\n\nprint(factorial(0))\nprint(factorial(5))\n
Output
1\n120\n

\u8a18\u5f97\u9ad8\u4e2d\u6578\u5b78\u8ab2\u672c\u4e0a\u7684\u5b9a\u7fa9\u55ce?

\\[ \\text{factorial}(n) = \\begin{cases} 1 & \\text{if } n = 0 \\text{, base case}\\\\ n \\times \\text{factorial}(n-1) & \\text{if } n > 0 \\text{, recursive case}\\end{cases} \\]

\u9019\u6a23\u7684\u5b9a\u7fa9\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ffb\u8b6f\u6210\u7a0b\u5f0f\u78bc:

def factorial(n):\n    if n == 0:\n        return 1\n    else:\n        return n * factorial(n - 1)\n\n\nprint(factorial(0))\nprint(factorial(5))\n
Output
1\n120\n

\u7576 n == 0 \u6642\uff0c\u6211\u5011\u7a31\u70ba\u57fa\u672c\u60c5\u6cc1(Base Case)\uff0c\u9019\u662f\u905e\u8ff4\u7684\u7d42\u6b62\u689d\u4ef6\uff0c\u7576 n > 0 \u6642\uff0c\u6211\u5011\u7a31\u70ba\u905e\u8ff4\u60c5\u6cc1(Recursive Case)\uff0c\u9019\u662f\u905e\u8ff4\u7684\u57f7\u884c\u689d\u4ef6\u3002

\u6bcf\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f\u90fd\u61c9\u8a72\u6709\u4e00\u500b\u57fa\u672c\u60c5\u6cc1\uff0c\u9019\u6a23\u7684\u905e\u8ff4\u624d\u6703\u7d42\u6b62\uff0c\u5426\u5247\u5c31\u6703\u9677\u5165\u7121\u7aae\u905e\u8ff4\uff0c\u54a6?\u6709\u6c92\u6709\u5f88\u719f\u6089?\u9084\u8a18\u5f97\u7121\u7aae\u8ff4\u5708\u55ce?

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97\u8cbb\u6c0f\u6578\u5217\u7684\u7b2c \\(n\\) \u9805\uff0c\u8ff4\u5708\u7684\u5beb\u6cd5\u662f\u9019\u6a23\u7684:

def fibonacci(n):\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\n\nprint(fibonacci(0))\nprint(fibonacci(10))\n
Output
0\n55\n

\u540c\u6a23\u7684\uff0c\u9084\u8a18\u5f97\u9ad8\u4e2d\u6578\u5b78\u8ab2\u672c\u4e0a\u7684\u5b9a\u7fa9\u55ce?

\\[ \\text{fibonacci}(n) = \\begin{cases} 0 & \\text{if } n = 0 \\text{, base case}\\\\ 1 & \\text{if } n = 1 \\text{, base case}\\\\ \\text{fibonacci}(n-1) + \\text{fibonacci}(n-2) & \\text{if } n > 1 \\text{, recursive case}\\end{cases} \\]

\u9019\u6a23\u7684\u5b9a\u7fa9\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ffb\u8b6f\u6210\u7a0b\u5f0f\u78bc:

def fibonacci(n):\n    if n == 0:\n        return 0\n    elif n == 1:\n        return 1\n    else:\n        return fibonacci(n - 1) + fibonacci(n - 2)\n\n\nprint(fibonacci(0))\nprint(fibonacci(10))\n
Output
0\n55\n

\u4f46\u662f\uff0c\u905e\u8ff4\u7684\u6548\u7387\u901a\u5e38\u6bd4\u8ff4\u5708\u4f4e\uff0c\u56e0\u70ba\u905e\u8ff4\u6703\u9020\u6210\u5927\u91cf\u7684\u51fd\u5f0f\u547c\u53eb\uff0c\u800c\u4e14\u5bb9\u6613\u9020\u6210\u5927\u91cf\u7684\u91cd\u8907\u8a08\u7b97\u3002

\u518d\u4f86\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8a08\u7b97\u6574\u6578 a, b \u7684\u6700\u5927\u516c\u56e0\u6578\uff0c\u4f60\u53ef\u80fd\u6709\u807d\u904e\u8f3e\u8f49\u76f8\u9664\u6cd5\uff0c\u53c8\u7a31\u70ba\u6b50\u5e7e\u91cc\u5f97\u6f14\u7b97\u6cd5(Euclidean algorithm)\uff0c\u4ed6\u7684\u5b9a\u7fa9\u662f\u9019\u6a23\u7684:

\\[ \\text{gcd}(a, b) = \\begin{cases} a & \\text{if } b = 0 \\text{, base case}\\\\ \\text{gcd}(b, a \\mod b) & \\text{if } b > 0 \\text{, recursive case}\\end{cases} \\]

\u76f4\u63a5\u5beb\u6210\u7a0b\u5f0f\u78bc:

def gcd(a, b):\n    if b == 0:\n        return a\n    else:\n        return gcd(b, a % b)\n\n\nprint(gcd(12, 18))\nprint(gcd(18, 12))\nprint(gcd(4843, 1234))\n
Output
6\n6\n1\n

\u9019\u500b\u8981\u5beb\u6210\u8fed\u4ee3(Iterative)\u7248\u672c\u9084\u6bd4\u8f03\u96e3\u3002

\u95dc\u65bc\u905e\u8ff4\uff0c\u5c31\u5148\u8b1b\u5230\u9019\u88e1\uff0c\u672a\u4f86\u9032\u5165\u6f14\u7b97\u6cd5\u7684\u7ae0\u7bc0\u6642\uff0c\u6703\u518d\u6df1\u5165\u8a0e\u8ad6\uff0c\u800c\u4e14\u6703\u6709\u66f4\u591a\u7684\u4f8b\u5b50\u3002

"},{"location":"fundamental/python/functions/#practice","title":"Practice","text":"

Itsa - [C_MM48-\u6613] F91

Reference code
def f91(n):\n    if n <= 100:\n        return f91(f91(n + 11))\n    elif n >= 101:\n        return n - 10\n\n\nk = int(input())\nn = list(map(int, input().split()))\nfor x in n:\n    print(f91(x))\n

\u4f60\u4ed4\u7d30\u89c0\u5bdf\u4e00\u4e0b\uff0c\u5176\u5be6\u53ef\u4ee5\u5beb\u6210\u9019\u6a23:

def f91(z): \n    if z >= 101: \n        return z - 10 \n    else: \n        return 91 \n\n\nk = int(input()) \nn = list(map(int, input().split())) \nfor x in n: \n    print(f91(x)) \n

Itsa - [C_RU13-\u6613] \u5927\u4e00\u9ede\u7684Fibonacci

Reference code

MAX = 47\nfibonacci = [0] * MAX\nfibonacci[1] = 1\n\nfor i in range(2, MAX):\n    fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]\n\nwhile True:\n    n = int(input())\n    if n == -1:\n        break\n    print(fibonacci[n + 1])\n
\u9019\u7a2e\u5beb\u6cd5\u53eb\u505a\u52d5\u614b\u898f\u5283(Dynamic Programming)\uff0c\u672a\u4f86\u6703\u518d\u8a0e\u8ad6\uff0c\u800c\u4e14\u6703\u5f88\u982d\u75db\u3002

"},{"location":"fundamental/python/functions/#assignment","title":"Assignment","text":"

Itsa - [C_MM103-\u6613] \u8cbb\u5f0f\u6578\u5217

Itsa - [C_MM143-\u6613] \u6c42Emirp

Itsa - [C_MM144-\u6613] \u6c42\u7d44\u5408\u6578C(n,r)

Itsa - \u984c\u76ee10. \u8f3e\u8f49\u76f8\u9664\u6cd5

Itsa - [C_RU10-\u4e2d] \u722c\u6a13\u68af

Itsa - [C_RU14-\u6613] \u597d\u9ad8\u7684\u6c34\u6676\u5854

Itsa - [C_RU23-\u6613] \u905e\u8ff4\u7df4\u7fd22f(n)=f(n-1)+2

Itsa - [C_RU24-\u6613] \u905e\u8ff4\u7df4\u7fd2f(n)\uff1d2f(n-1)/(1+2f(n-1))

Itsa - [C_RU25-\u6613] \u905e\u8ff4\u7df4\u7fd2f(n)\uff1d3f(n-1)+g(n-1)\ufe50g(n)\uff1d-f(n-1)+g(n-1)

"},{"location":"fundamental/python/lists/","title":"Lists","text":""},{"location":"fundamental/python/lists/#introduction","title":"Introduction","text":"

\u4e0d\u66c9\u5f97\u4f60\u770b\u5230\u9019\u88e1\u6709\u6c92\u6709\u767c\u73fe\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u90fd\u662f\u7528\u4e00\u500b\u4e00\u500b\u7684\u8b8a\u6578\u4f86\u5132\u5b58\u8cc7\u6599\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u8981\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u9019\u6a23\u7684\u65b9\u5f0f\u5c31\u6703\u8b8a\u5f97\u5f88\u9ebb\u7169\u3002

\u6240\u4ee5\u9019\u88e1\u8ddf\u4f60\u4ecb\u7d39 List(\u4e32\u5217)\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u800c\u4e14\u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u4f8b\u5982: \u6578\u5b57\u3001\u5b57\u4e32\u3001\u5e03\u6797\u503c\u7b49\u7b49\u3002

"},{"location":"fundamental/python/lists/#create_a_list","title":"Create a list","text":"

\u8981\u5efa\u7acb\u4e00\u500b List\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u6211\u5011\u8981\u5132\u5b58\u7684\u8cc7\u6599\uff0c\u8cc7\u6599\u4e4b\u9593\u7528\u9017\u865f , \u9694\u958b\u3002

\u4f86\u8209\u500b\u4f8b\u5b50\uff0c\u5efa\u7acb\u4e09\u500b List\uff0c\u4e00\u500b\u90fd\u5132\u5b58\u6574\u6578\uff0c\u4e00\u500b\u662f\u90fd\u5132\u5b58\u5b57\u4e32\uff1b\u6700\u5f8c\u4e00\u500b\u5247\u653e\u4e86\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\u3002

numbers = [1, 2, 3, 4, 5]\nprint(numbers)\n\nwhat_i_learned = [\"C\", \"C++\", \"Python\", \"Java\", \"C#\", \"JavaScript\", \"TypeScript\"]\nprint(what_i_learned)\n\nstudent_info = [\"Sean\", 20, \"Computer Science\", 4.0, True, \"aLIEz\"]\nprint(student_info)\n\nprint(type(numbers), type(what_i_learned), type(student_info))\n
ouput
[1, 2, 3, 4, 5]\n['C', 'C++', 'Python', 'Java', 'C#', 'JavaScript', 'TypeScript']\n['Sean', 20, 'Computer Science', 4.0, True, 'aLIEz']\n<class 'list'> <class 'list'> <class 'list'>\n

Sawano Hiroyuki - aLIEz Aldnoah.Zero Full Lyrics

\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u6703\u807d\u904e Array(\u9663\u5217)\uff0cList \u5c31\u662f Python \u4e2d\u7684 Array\uff0c\u4f46\u53c8\u66f4\u5f37\u5927\uff0c\u56e0\u70ba List \u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u800c\u901a\u5e38 Array \u53ea\u80fd\u5132\u5b58\u4e00\u7a2e\u8cc7\u6599\u578b\u614b\u3002

\u4f46 Python \u4e5f\u6709\u63d0\u4f9b\u771f\u6b63\u7684\u300c\u9663\u5217\u300d\uff0c\u4f46\u6211\u5148\u4e0d\u63d0\uff0c\u8acb\u4f60\u7a0d\u5fae\u6709\u500b\u5370\u8c61\u5c31\u597d\u3002

\u984c\u5916\u8a71\uff0c\u7576\u521d\u5f9e C, C++, Java \u8f49\u5230 Python \u7684\u6642\u5019\uff0c\u771f\u5fc3\u89ba\u5f97\u600e\u9ebc\u53ef\u4ee5\u9019\u9ebc\u96a8\u4fbf\uff0c\u9023\u578b\u614b\u90fd\u4e0d\u7528\u5ba3\u544a\uff0c\u73fe\u5728\u89ba\u5f97\u771f\u9999\u3002

\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u6709\u63d0\u5230 range \u9019\u500b\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6574\u6578\u7684\u5e8f\u5217\uff0c\u4f8b\u5982: range(5) \u6703\u7522\u751f\u4e00\u500b\u5f9e \\(0\\) \u5230 \\(4\\) \u7684\u6574\u6578\u5e8f\u5217\u3002

list(range(5)) \u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 range \u7269\u4ef6\u8f49\u63db\u6210 List\u3002

x = range(5)\nprint(type(x))\nnumbers = list(x)\nprint(numbers)\n
ouput
<class 'range'>\n[0, 1, 2, 3, 4]\n

\u90a3\u80fd\u5426\u628a\u5b57\u4e32\u8f49\u63db\u6210 List \u5462? \u7576\u7136\u53ef\u4ee5!

x = \"Signals\"\nprint(type(x))\nwords = list(x)\nprint(words)\n
ouput
<class 'str'>\n['S', 'i', 'g', 'n', 'a', 'l', 's']\n

Lazer Boomerang - Signals (Official Audio)

\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u4f55\u521d\u59cb\u5316\u4e00\u500b\u5143\u7d20\u90fd\u662f \\(0\\) \u7684\u4e32\u5217\u3002

n = 5\nzeros = [0] * n\nprint(zeros)\n
ouput
[0, 0, 0, 0, 0]\n

\u5982\u4f55\u5c07\u5169\u500b\u4e32\u5217\u5408\u4f75\u6210\u4e00\u500b\u4e32\u5217\u5462?

a = [1, 2, 3]\nb = [4, 5, 6]\nc = a + b\nprint(c)\n
ouput
[1, 2, 3, 4, 5, 6]\n

\u9084\u6709\u4e00\u500b\u65b9\u6cd5\uff0c\u5c31\u662f\u4f7f\u7528 extend \u9019\u500b\u65b9\u6cd5\uff0c\u9019\u5728\u5f8c\u9762\u6703\u4ecb\u7d39\u5230\u3002

\u6211\u76f8\u4fe1\u4ee5\u4e0a\u9019\u4e9b\u4f8b\u5b50\u80fd\u8b93\u4f60\u6293\u5230\u4e00\u4e9b\u6982\u5ff5\uff0c\u63a5\u4e0b\u4f86\u5c07\u4ecb\u7d39\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u8207\u65b9\u6cd5\uff0c\u8b93\u4f60\u66f4\u719f\u6089 List\u3002

"},{"location":"fundamental/python/lists/#operations","title":"Operations","text":""},{"location":"fundamental/python/lists/#accessing_elements","title":"Accessing elements","text":"

\u8981\u5b58\u53d6 List \u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7d22\u5f15\u503c\u5f9e \\(0\\) \u958b\u59cb\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528\u8ca0\u6578\uff0c\u8ca0\u6578\u7684\u7d22\u5f15\u503c\u662f\u5f9e\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u958b\u59cb\u7b97\u8d77\u3002

numbers = [1, 2, 3, 4, 5]\n\n# First element\nprint(numbers[0])\n# Last element\nprint(numbers[-1])\nprint(numbers[4])\n
ouput
1\n5\n5\n

\u8b93\u6211\u5011\u914d\u5408\u8ff4\u5708\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\n\nfor i in range(len(numbers)):\n    print(numbers[i], end=' ')\n
ouput
1 2 3 4 5\n

len \u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u4e32\u5217\u7684\u9577\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e32\u5217\u4e2d\u5143\u7d20\u7684\u500b\u6578\u3002

\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97 Repetiton Structures - Foreach \u7684\u5167\u5bb9\uff0c\u6211\u5011\u7528\u9019\u500b\u6982\u5ff5\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\n\nfor number in numbers:\n    print(number, end=' ')\n
ouput
1 2 3 4 5\n
"},{"location":"fundamental/python/lists/#slicing","title":"Slicing","text":"

\u5207\u7247(Slicing)\u53ef\u4ee5\u7528\u4f86\u53d6\u5f97\u4e32\u5217\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982:

numbers = [1, 2, 3, 4, 5, 6]\n\nprint(numbers[0:4])\nprint(numbers[:4])\nprint(numbers[:-2])\nprint(numbers[1:3])\nprint(numbers[:6:2])\nprint(numbers[::-1])\nprint(numbers[:])\n
ouput
[1, 2, 3, 4]\n[1, 2, 3, 4]\n[1, 2, 3, 4]\n[2, 3]\n[1, 3, 5]\n[6, 5, 4, 3, 2, 1]\n[1, 2, 3, 4, 5, 6]\n

@EditTime : 2024-01-31 21:56

\u4f60\u5f88\u8070\u660e\uff0c\u61c9\u8a72\u767c\u73fe\u4e86\u7528\u6cd5\u8207 range \u4e00\u6a23\uff0c\u5206\u5225\u662f\u8d77\u59cb\u7d22\u5f15\u503c\u3001\u7d50\u675f\u7d22\u5f15\u503c\u3001\u9593\u9694\uff0c\u5982\u679c\u4e0d\u5beb\u7684\u8a71\uff0c\u9810\u8a2d\u503c\u5206\u5225\u662f \\(0\\)\u3001\u4e32\u5217\u9577\u5ea6\u3001\\(1\\)\u3002

"},{"location":"fundamental/python/lists/#modifying_elements","title":"Modifying elements","text":"

\u8981\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7136\u5f8c\u518d\u6307\u5b9a\u65b0\u7684\u503c\u3002

music_info = [\"Time_To_Pretend\", \"Lazer Boomerang\", \" 2019/4/26\", 1, False]\nmusic_info[0] = \"Time To Pretend\"\nmusic_info[3] = 2\nmusic_info[-1] = True\nprint(music_info)\n
ouput
['Time To Pretend', 'Lazer Boomerang', ' 2019/4/26', 2, True]\n

Lazer Boomerang - Time To Pretend (Official Audio)

\u6211\u5011\u53ef\u4ee5\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u503c\uff0c\u9019\u7a31\u70ba\u53ef\u8b8a\u52d5\u7684(mutable)\uff0c\u800c\u5b57\u4e32\u5247\u4e0d\u884c\uff0c\u9019\u7a31\u70ba\u4e0d\u53ef\u8b8a\u52d5\u7684(immutable)\u3002

sad_cat = \"\ud83d\ude3f\"\nsad_cat[0] = \"\ud83d\ude38\"\n
ouput
TypeError: 'str' object does not support item assignment\n
"},{"location":"fundamental/python/lists/#checking_elements","title":"Checking elements","text":"

\u8981\u6aa2\u67e5\u4e32\u5217\u4e2d\u662f\u5426\u6709\u67d0\u500b\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 in \u9019\u500b\u904b\u7b97\u5b50\u3002

numbers = [1, 2, 3, 4, 5]\nprint(1 in numbers)\nprint(6 not in numbers)\n
ouput
True\nTrue\n

Question

\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u4e32\u5217\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u6700\u58de\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?

\u7b54\u6848\u662f \\(n\\) \u6b21\u5594\uff0c\u56e0\u70ba\u4e00\u500b\u4e00\u500b\u627e\uff0c\u76f4\u5230\u627e\u5230\u6216\u662f\u627e\u5b8c\u70ba\u6b62\uff0c\u50cf\u662f\u4f8b\u5b50\u4e2d\u7684 6\u3002

\u5728\u6f14\u7b97\u6cd5(Algorithm)\u7684\u7ae0\u7bc0\u4e2d\u6703\u63d0\u5230\u6642\u9593\u8907\u96dc\u5ea6(Time Complexity)\u9019\u500b\u89c0\u5ff5\uff0c\u9019\u88e1\u5c31\u5148\u7d66\u4f60\u4e00\u500b\u5370\u8c61\u5c31\u597d\u4e86~

"},{"location":"fundamental/python/lists/#methods","title":"Methods","text":"

\u4f86\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u4e32\u5217\u65b9\u6cd5\u3002

"},{"location":"fundamental/python/lists/#append","title":"append","text":"

append \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u4e00\u500b\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.append(6)\nprint(numbers)\nnumbers.append([7, 8, 9])\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5, 6]\n[1, 2, 3, 4, 5, 6, [7, 8, 9]]\n

\u5e0c\u671b\u7b2c\u4e8c\u500b append \u6c92\u6709\u5687\u5230\u4f60\uff0c\u5b83\u53ef\u4ee5\u65b0\u589e\u4e00\u500b\u4e32\u5217\uff0c\u4f46\u662f\u9019\u500b\u4e32\u5217\u6703\u8b8a\u6210\u4e32\u5217\u4e2d\u7684\u4e00\u500b\u5143\u7d20\u3002\u5982\u679c\u4f60\u662f\u60f3\u8981\u628a 7\u30018\u30019 \u9019\u4e09\u500b\u5143\u7d20\u52a0\u5230\u4e32\u5217\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 extend \u9019\u500b\u65b9\u6cd5\u3002

"},{"location":"fundamental/python/lists/#extend","title":"extend","text":"

extend \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u53e6\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.extend([6, 7, 8])\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5, 6, 7, 8]\n
"},{"location":"fundamental/python/lists/#pop","title":"pop","text":"

pop \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u88ab\u79fb\u9664\u7684\u5143\u7d20\u3002

fruits = [\"apple\", \"banana\", \"cherry\", \"durian\", \"elderberry\"]\nprint(fruits.pop())\nprint(fruits)\nprint(fruits.pop(1))\nprint(fruits)\n
ouput
elderberry\n['apple', 'banana', 'cherry', 'durian']\nbanana\n['apple', 'cherry', 'durian']\n

\u901a\u5e38\uff0c\u6211\u5011\u6703\u4f7f\u7528 pop \u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u56e0\u70ba\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u7d22\u5f15\u503c\u8d85\u51fa\u7bc4\u570d\u7684\u932f\u8aa4\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7d22\u5f15\u503c\uff0c\u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u4e0d\u5efa\u8b70\u3002

"},{"location":"fundamental/python/lists/#clear","title":"clear","text":"

clear \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.clear()\nprint(numbers)\n
ouput
[]\n
"},{"location":"fundamental/python/lists/#reverse","title":"reverse","text":"

reverse \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u53cd\u8f49\u4e32\u5217\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.reverse()\nprint(numbers)\n
ouput
[5, 4, 3, 2, 1]\n
"},{"location":"fundamental/python/lists/#sort","title":"sort","text":"

sort \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u6392\u5e8f\u4e32\u5217\uff0c\u9810\u8a2d\u662f\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u5982\u679c\u8981\u7531\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u53ef\u4ee5\u6307\u5b9a reverse=True\u3002

numbers = [5, 4, 3, 2, 1]\nnumbers.sort()\nprint(numbers)\nnumbers.sort(reverse=True)\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5]\n[5, 4, 3, 2, 1]\n

\u751a\u81f3\u4f60\u53ef\u4ee5\u8a2d\u5b9a key\uff0c\u4f86\u6c7a\u5b9a\u6392\u5e8f\u7684\u4f9d\u64da\uff0c\u4f8b\u5982: key=len\uff0c\u5c31\u662f\u6839\u64da\u5143\u7d20 x \u7684 len(x) \u4f86\u6392\u5e8f\u3002

fruits = [\"apple\", \"banana\", \"watermelon\", \"pineapple\"]\nfruits.sort(key=len)\nprint(fruits)\n
ouput
['apple', 'banana', 'pineapple', 'watermelon']\n
"},{"location":"fundamental/python/lists/#copy","title":"copy","text":"

copy \u9019\u500b\u65b9\u6cd5\u56de\u50b3\u4e00\u500b\u6dfa\u8907\u88fd(Shallow Copy)\u7684\u4e32\u5217\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1] = 10\nprint(numbers_copy)\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5]\n[1, 2, 3, 4, 5]\nAfter changing numbers[1]\n[1, 2, 3, 4, 5]\n[1, 10, 3, 4, 5]\n

\u9019\u500b\u4f8b\u5b50\u9084\u4e0d\u80fd\u770b\u51fa\u6dfa\u8907\u88fd\uff0c\u4fee\u6539\u4e86 numbers \u7684\u7b2c\u4e8c\u500b\u5143\u7d20\uff0cnumbers_copy \u4e0d\u6703\u8ddf\u8457\u6539\u8b8a\u3002

numbers = [1, [2, 3], 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1][0] = 6\nprint(numbers_copy)\nprint(numbers)\n
ouput
[1, [2, 3], 4, 5]\n[1, [2, 3], 4, 5]\nAfter changing numbers[1]\n[1, [6, 3], 4, 5]\n[1, [6, 3], 4, 5]\n

\u90a3\u4ec0\u9ebc\u53c8\u662f\u6df1\u8907\u88fd(Deep Copy)\u5462? \u9019\u500b\u5c31\u8981\u7b49\u5230\u6211\u5011\u5b78\u5230\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08(Object-Oriented Programming)\u7684\u6642\u5019\uff0c\u518d\u4f86\u8ddf\u4f60\u4ecb\u7d39\uff0c\u6211\u4e0d\u61c9\u8a72\u6316\u5751\u7684\u3002

\u9084\u6709\u66f4\u591a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u53c3\u8003 Python Documentation - List\uff0c\u4f46\u6211\u4e0a\u9762\u4ecb\u7d39\u7684\u65b9\u6cd5\uff0c\u4f60\u81f3\u5c11\u8981\u6703\uff0c\u4f60\u4e5f\u8981\u990a\u6210\u770b\u5b98\u65b9\u6587\u4ef6\u7684\u7fd2\u6163\u3002

"},{"location":"fundamental/python/lists/#list_comprehension","title":"List Comprehension","text":"

\u4e32\u5217\u7d9c\u5408\u904b\u7b97(List comprehension)\u8b93\u4f60\u7528\u4e00\u884c\u7a0b\u5f0f\u78bc\uff0c\u5c31\u53ef\u4ee5\u5efa\u7acb\u51fa\u64c1\u6709\u67d0\u9805\u6027\u8cea\u5143\u7d20\u7684\u4e32\u5217\u3002

\u8209\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b\uff0c\u5176\u4e2d b[i] = a[i] ** 2\uff0c\u4e5f\u5c31\u662f b \u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0d\u61c9 a \u5143\u7d20\u7684\u5e73\u65b9\u3002

\u6211\u5011\u5148\u4f7f\u7528\u8ff4\u5708\u4f86\u5be6\u4f5c:

a = [1, 2, 3, 4, 5]\nb = []\n\nfor num in a:\n    b.append(num ** 2)\n\nprint(b)\n
ouput
[1, 4, 9, 16, 25]\n

\u518d\u4f86\u4f7f\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97:

a = [1, 2, 3, 4, 5]\nb = [num ** 2 for num in a]\n\nprint(b)\n
ouput
[1, 4, 9, 16, 25]\n

\u975e\u5e38\u5730\u512a\u96c5\uff0c\u6211\u5011\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b\uff0c\u5176\u4e2d b \u7684\u5143\u7d20\u90fd\u662f a \u5143\u7d20\u4e2d\u7684\u5076\u6578\u3002

a = [1, 2, 3, 4, 5]\nb = [num for num in a if num % 2 == 0]\n\nprint(b)\n
ouput
[2, 4]\n

\u9084\u8a18\u5f97 Operators - Bonus: map for input \u63d0\u5230\u7684 map \u55ce?

\u5148\u4f86\u8907\u7fd2\u4e00\u4e0b\uff0cmap \u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u5957\u7528\u5230\u4e00\u500b\u5e8f\u5217\u7684\u6bcf\u4e00\u500b\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u4e00\u500b map \u7269\u4ef6\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 list \u5c07 map \u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\u3002

a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = map(int, a)\nprint(type(b))\n\nc = list(b)\nprint(c)\n
ouput
<class 'map'>\n[1, 2, 3, 4, 5]\n

@EditTime : 2024-02-01 23:21

\u90a3\u80fd\u4e0d\u80fd\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97\u4f86\u5be6\u4f5c\u5462?

a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = [int(num) for num in a]\nprint(b)\n
ouput
[1, 2, 3, 4, 5]\n

\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002

\u9084\u6709\u5f88\u591a\u61c9\u7528\uff0c\u4f8b\u5982: \u4e32\u5217\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0f\u5beb\uff0c\u6211\u5011\u60f3\u8981\u628a\u5b83\u5011\u8f49\u63db\u6210\u5927\u5beb\u3002

lowercase = [\"a\", \"b\", \"c\", \"d\", \"e\"]\nuppercase = [letter.upper() for letter in lowercase]\nprint(uppercase)\n
ouput
['A', 'B', 'C', 'D', 'E']\n

\u6ce8\u610f\u56c9\uff0c\u9019\u908a\u7528\u5230\u7684\u662f\u65b9\u6cd5\u5594\u3002

"},{"location":"fundamental/python/lists/#nested_lists","title":"Nested Lists","text":"

\u5de2\u72c0\u4e32\u5217(Nested Lists)\uff0c\u5c31\u662f\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u4e5f\u662f\u4e32\u5217\u3002

"},{"location":"fundamental/python/lists/#2d","title":"2D","text":"

\u5148\u4f86\u8209\u4e00\u500b\u4f60\u53ef\u80fd\u807d\u904e\u7684\u540d\u8a5e\uff0c\u4e8c\u7dad\u9663\u5217(2D Array)\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5de2\u72c0\u4e32\u5217\u4f86\u5be6\u4f5c\u4e8c\u7dad\u9663\u5217\uff0c\u4f8b\u5982\u5169\u500b \\(3 \\times 3\\) \u7684\u4e8c\u7dad\u9663\u5217\uff0c\u4f86\u9032\u884c\u77e9\u9663\u52a0\u6cd5\u3002

matrix_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nmatrix_2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]\n\nmatrix_3 = [[0 for i in range(3)] for j in range(3)]\n\nfor i in range(3):\n    for j in range(3):\n        matrix_3[i][j] = matrix_1[i][j] + matrix_2[i][j]\n\nprint(matrix_3)\n
ouput
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]\n

\u6211\u5e0c\u671b matrix_3 \u7684\u521d\u59cb\u5316\u6c92\u6709\u5687\u5230\u4f60\uff0c\u8acb\u4f60\u628a [0 for i in range(3)] \u7576\u6210\u4e00\u500b\u6574\u9ad4 E\uff0c\u518d\u628a [E for j in range(3)] \u7576\u6210\u4e00\u500b\u6574\u9ad4\uff0c\u9019\u6a23\u5c31\u4e0d\u6703\u89ba\u5f97\u5f88\u8907\u96dc\u4e86\uff0ci, j \u90fd\u53ea\u662f\u8a08\u6578\u7528\u7684\u800c\u5df2\uff0c\u4e0d\u8981\u88ab\u5b83\u5011\u5687\u5230\u3002

"},{"location":"fundamental/python/lists/#jagged","title":"Jagged","text":"

\u92f8\u9f52\u72c0\u4e32\u5217(Jagged Lists)\uff0c\u5c31\u662f\u5141\u8a31\u5de2\u72c0\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7684\u9577\u5ea6\u4e0d\u4e00\u6a23\u3002

\u984c\u5916\u8a71\uff0c\u4e4b\u524d\u5728\u5b78 C \u7684\u6642\u5019\uff0c\u53ea\u80fd\u7528\u6307\u6a19\u4f86\u5be6\u4f5c\uff0c\u4f46\u7528\u8d77\u4f86\u771f\u7684\u5f88\u9ebb\u7169\u3002

jagged = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]\n\nfor row in jagged:\n    for elem in row:\n        print(elem, end=' ')\n    print()\n
ouput
1 2 3 \n4 5 \n6 7 8 9 \n

\u5e0c\u671b\u9019\u500b\u8ff4\u5708\u7684\u5beb\u6cd5\u4e0d\u6703\u5687\u5230\u4f60\uff0crow \u662f jagged \u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u4e32\u5217\uff0celem \u662f row \u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u6574\u6578\u3002

Python \u7684\u8a9e\u6cd5\u771f\u7684\u5f88\u9748\u6d3b\uff0c\u4f46\u4e5f\u8981\u5f88\u5c0f\u5fc3\uff0c\u4ed6\u63d0\u4f9b\u4e86\u5f88\u591a\u5de5\u5177\uff0c\u4f46\u5982\u679c\u4f60\u4e0d\u6e05\u695a\u4ed6\u662f\u600e\u9ebc\u904b\u4f5c\u7684\uff0c\u5c31\u6703\u5bb9\u6613\u6703\u5beb\u51fa\u6548\u7387\u4f4e\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u4e5f\u662f\u96d9\u9762\u5203\uff0c\u6240\u4ee5\u8981\u591a\u591a\u7df4\u7fd2\uff0c\u591a\u591a\u601d\u8003\uff0c\u4f60\u53ef\u4ee5\u7684\u3002

"},{"location":"fundamental/python/lists/#practice","title":"Practice","text":"

Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578

Reference code

table = []\nfor i in range(6, 2 ** 13, 2):\n    s = 0\n    for j in range(1, i):\n        if i % j == 0:\n            s += j\n    if s == i:\n        table.append(i)\n\nwhile True:\n    N = int(input())\n    print(' '.join([str(x) for x in table if x <= N]))\n
\u5efa\u7acb\u4e00\u500b\u4e32\u5217 table\uff0c\u88e1\u9762\u653e\u4e86 \\(6\\) \u5230 \\(2^{13}\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\uff0c\u63a5\u8457\u8f38\u5165\u4e00\u500b\u6574\u6578 \\(N\\)\uff0c\u8f38\u51fa \\(1\\) \u5230 \\(N\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\u3002

\u90a3\u9ebc \\(2^{13}\\) \u662f\u600e\u9ebc\u4f86\u7684?\u311c\uff0c\u6211\u5237\u5f8c\u53f0\u6e2c\u8cc7\u63a8\u6572\u4f86\u7684\u3002

join \u65b9\u6cd5\u53ef\u4ee5\u5c07\u5b57\u4e32\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7528\u6307\u5b9a\u7684\u5b57\u4e32\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u908a\u7528\u7a7a\u767d\u5b57\u4e32 ' ' \u4f86\u9023\u63a5\u3002

\u9019\u7a2e\u6280\u5de7\u7a31\u70ba\u300c\u5efa\u8868\u300d\uff0c\u4e8b\u5be6\u4e0a\u5b8c\u7f8e\u6578\u5f88\u758f\u6563\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u81ea\u5df1\u5efa\u5b8c\u8868\u5f8c\u76f4\u63a5\u5beb\u6b7b\uff0c\u4f46\u9019\u6a23\u7684\u8a71\uff0c\u7a0b\u5f0f\u78bc\u5c31\u4e0d\u5177\u6709\u901a\u7528\u6027\u3002

table = [6, 28, 496, 8128]\n\nN = int(input())\nprint(' '.join(str(x) for x in table if x <= N))\n

\u5982\u679c\u5c31\u55ae\u7d14\u89e3\u984c\u7684\u8a71\uff0c\u9019\u6a23\u4e5f\u662f\u53ef\u4ee5\u7684\u3002

\u6b63\u898f\u7684\u984c\u76ee\u90fd\u6703\u898f\u7bc4\u6e2c\u8cc7\u7bc4\u570d\uff0c\u5982\u679c\u4f60\u4ee5\u5f8c\u4e0a\u5927\u5b78\u5f8c\u9047\u5230\u4e0d\u592a\u9748\u5149\u7684\u52a9\u6559\u6c92\u6709\u6a19\u6e2c\u8cc7\u7bc4\u570d\uff0c\u8a18\u5f97\u96fb\u6b7b\u4ed6\u5011\u3002

Itsa - [C_MM44-\u6613] The Numbers

Reference code
N, M = input().split()\n\ncnt = 0\nfor i in range(0, 6):\n    if M[i] == N[0] and M[i + 1] == N[1]:\n        cnt += 1\n\nprint(cnt)\n

\u5b57\u4e32\u4e5f\u53ef\u4ee5\u50cf\u662f\u4e32\u5217\u4e00\u6a23\uff0c\u4f7f\u7528\u7d22\u5f15\u503c\u4f86\u5b58\u53d6\u5143\u7d20\u3002

Itsa - [C_AR01-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 I

Reference code
arr = input().split()\nprint(' '.join(arr[::-1]))\n

arr.reverse() \u4e5f\u884c\uff0c\u4f46\u662f\u9019\u6a23\u6703\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5c31\u4f7f\u7528 arr[::-1]\u3002

arr = input().split()\narr.reverse()\nprint(' '.join(arr))\n

Itsa - [C_AR03-\u6613] \u8a08\u7b97\u9663\u5217\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7acb\u65b9\u548c

Reference code
arr = map(int, input().split())\nans = 0\n\nfor num in arr:\n    ans += num ** 3\n\nprint(ans)\n

\u6211\u4e26\u6c92\u6709\u628a map \u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\uff0c\u56e0\u70ba\u6211\u53ea\u9700\u8981\u4e00\u6b21\u8fed\u4ee3\uff0c\u6240\u4ee5\u76f4\u63a5\u7528 map \u7269\u4ef6\u5c31\u597d\u3002

\u4f86\u770b\u4e00\u884c\u89e3:

print(sum(int(x) ** 3 for x in input().split()))\n

\u53c3\u8003\u5c31\u597d\u3002

Itsa - [C_AR09-\u6613] \u5169\u6578\u5dee\u503c

Reference code
arr1 = list(map(int, input().split(',')))\narr1.sort()\narr2 = arr1.copy()\narr2.sort(reverse=True)\n\nmax_num,min_num  = 0, 0\nfor i in range(len(arr1) - 1, -1, -1):\n    # convert list to number\n    max_num = max_num * 10 + arr1[i]\n    min_num = min_num * 10 + arr2[i]\n\nprint(max_num - min_num)\n

\u4e0d\u61c2\u6392\u5e8f\u6c92\u95dc\u4fc2\uff0c\u6211\u5011\u5148\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u4ee5\u5f8c\u6211\u6703\u6559\u4f60\u539f\u7406\u3002

\u800c\u9019\u984c\u7684\u91cd\u9ede\u662f\uff0c\u5982\u4f55\u5c07\u4e32\u5217\u8f49\u63db\u6210\u6578\u5b57\u3002\u5f9e\u500b\u4f4d\u6578\u958b\u59cb\uff0c\u6bcf\u6b21\u4e58 \\(10\\)\uff0c\u518d\u52a0\u4e0a\u4e0b\u4e00\u500b\u6578\u5b57\u3002

Itsa - [C_AR022-\u6613] \u5b57\u6bcd\u51fa\u73fe\u7684\u983b\u7387

Reference code
string = input()\n\nfreq = [0] * 26\n\nfor c in string:\n    if c.isalpha():\n        freq[ord(c.lower()) - ord('a')] += 1\n\nprint(' '.join(str(x) for x in freq))\n

\u9019\u984c\u9700\u8981\u4e00\u500b\u5148\u5099\u77e5\u8b58\uff0cASCII \u78bc\uff0cord \u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u5b57\u5143\u7684 ASCII \u78bc\u3002

\u518d\u5229\u7528 isalpha \u65b9\u6cd5\uff0c\u4f86\u5224\u65b7\u5b57\u5143\u662f\u5426\u70ba\u5b57\u6bcd\uff0c\u4ee5\u53ca lower \u65b9\u6cd5\uff0c\u4f86\u5c07\u5b57\u6bcd\u8f49\u63db\u6210\u5c0f\u5beb\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u5b57\u6bcd\u6620\u5c04\u5230 0 \u5230 25 \u4e4b\u9593\u3002

\u95dc\u65bc\u5b57\u4e32\u7684\u4e3b\u984c\uff0c\u5f8c\u9762\u6703\u518d\u8ddf\u4f60\u4ecb\u7d39\u3002

Itsa - [C_AR025-\u6613] \u8a08\u7b97ASCII\u5b57\u5143

Reference code
string = input()\n\nfreq = [0] * 128\n\n\nfor i in range(len(string)):\n    freq[ord(string[i])] += 1\n\nfor i in range(len(freq) - 1, -1, -1):\n    if freq[i] > 0:\n        print(f'{i} {freq[i]}')\n

\u9019\u984c\u8ddf\u4e0a\u4e00\u984c\u5f88\u50cf\u3002

Itsa - [C_AR029-\u96e3] \u6587\u5b57\u7de8\u78bc

Reference code
plain_text = input()\n\nN = len(plain_text)\nM = 1\nwhile M * M < N:\n    M += 1\n\narr = [[' ' for _ in range(M)] for _ in range(M)]\n\nfor i in range(N):\n    arr[i // M][i % M] = plain_text[i]\n\ncipher_text = []\nfor i in range(M):\n    for j in range(M):\n        cipher_text.append(arr[j][i])\n\nprint(''.join(cipher_text))\n

\u77e9\u9663\u8f49\u7f6e\uff0c\u6240\u4ee5 arr[j][i] \u8b8a\u6210 arr[i][j]\u3002

"},{"location":"fundamental/python/lists/#assignment","title":"Assignment","text":"

Itsa - [C_MM42-\u4e2d] \u6c42(-1)^(n+1)x[1/(2n-1)]\u7684\u548c

Itsa - [C_AR02-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 II

Itsa - [C_AR10-\u4e2d] \u65b0\u901a\u8a71\u8cbb\u7387

Itsa - [C_AR021-\u6613] \u6210\u7e3e\u7d71\u8a08

Itsa - [C_AR023-\u6613] \u5b57\u6839\u8207\u5b50\u5b57\u4e32

Itsa - [C_AR031-\u4e2d] \u4e00\u7dad\u77e9\u9663\u8868\u793a\u4e8c\u7dad\u5e73\u9762\u7a7a\u9593

Itsa - [C_AR33-\u6613] \u8f49\u7f6e\u77e9\u9663

Itsa - [C_AR34-\u6613] \u8eab\u5206\u8b49\u9a57\u8b49\u5668

Itsa - [C_AR35-\u6613] \u751f\u8096\u554f\u984c

Itsa - [C_AR41-\u6613] \u4e00\u6574\u6578\u5e8f\u5217\u6240\u542b\u4e4b\u6574\u6578\u500b\u6578\u53ca\u5e73\u5747\u503c

Itsa - [C_AR46-\u6613] \u9663\u5217\u5e73\u65b9\u548c\u554f\u984c

Itsa - [C_AR48-\u6613] \u6578\u5b57\u52a0\u5bc6

@EditTime : 2024-02-03 21:30

"},{"location":"fundamental/python/operators/","title":"Operators","text":""},{"location":"fundamental/python/operators/#introduction","title":"Introduction","text":"

\u5728\u9019\u500b\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u904b\u7b97\u5b50\uff0c\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u5c0d\u8b8a\u6578\u505a\u904b\u7b97\uff0c\u4f8b\u5982: \u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u7b49\u7b49\u3002

"},{"location":"fundamental/python/operators/#arithmetic_operators","title":"Arithmetic Operators","text":"

\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u6211\u5011\u719f\u6089\u7684\u56db\u5247\u904b\u7b97\u5c31\u662f\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u5206\u5225\u662f +\u3001-\u3001*\u3001/\uff0c\u5206\u5225\u4ee3\u8868\u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3002

x, y = 10, 3\nprint(x + y)\nprint(x - y)\nprint(x * y)\nprint(x / y)\nz = x // y\nprint(z)\nprint(type(z))\nprint(x % y)\nprint(x ** y)\n
ouput
13\n7\n30\n3.3333333333333335\n3\n<class 'int'>\n1\n1000\n

\u6211\u76f8\u4fe1\u770b\u5b8c\u8f38\u51fa\u5f8c\uff0c\u4f60\u80fd\u5927\u81f4\u660e\u77ad\u5404\u500b\u904b\u7b97\u5b50\u7684\u4f5c\u7528\uff0c\u5176\u4e2d\u60f3\u8acb\u4f60\u7559\u610f * \u4e58\u6cd5\u904b\u7b97\u5b50\uff0c\u8acb\u4f60\u4e0d\u8981\u6253\u6210 x\uff0c\u4ee5\u53ca / \u8207 // \u7684\u5dee\u5225\uff0c\u5f8c\u8005\u6703\u5c07\u7d50\u679c\u5411\u4e0b\u53d6\u6574\u3002\u9084\u6709\u6bd4\u8f03\u7279\u5225\u7684 % \uff0c\u7559\u610f\u5230 10 = 3 * 3 + 1\uff0c\u5c31\u662f\u53d6\u9918\u6578\uff1b\u800c\u770b\u8d77\u4f86\u6700\u5947\u7279\u7684 ** \uff0c\u4ed6\u7684\u4f5c\u7528\u662f\u6c42\u51aa\u3002

\u6211\u5011\u4f86\u8907\u7fd2\u4e00\u4e0b\u570b\u5c0f\u6578\u5b78:

# Upper case for constant\nPI = 3.14\nr = 2\narea = PI * (r ** 2)\nperimeter = 2 * PI * r\nprint(f'Area: {area}, Perimeter: {perimeter}')\n\n# Sum of 1 to 100\ntotal = (1 + 100) * 100 // 2\nprint(f'The sum of 1 to 100 is {total}')\n

ouput
Area: 12.56, Perimeter: 12.56\nThe sum of 1 to 100 is 5050\n

\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c\u904b\u7b97\u5b50\u4e4b\u9593\u6709\u512a\u5148\u9806\u5e8f\uff0c\u7576\u4f60\u4e0d\u78ba\u5b9a\u7d50\u679c\u7684\u6642\u5019\uff0c\u8acb\u5584\u7528\u62ec\u865f\u3002

x = 'A' + 'n' + 'i' + 'm' + 'a' + 'l' + 's'\nprint(x)\n
ouput
Animals\n

Martin Garrix - Animals (Official Video)

\u518d\u8acb\u4f60\u7559\u610f\u4e00\u4ef6\u4e8b\uff0c\u4e0d\u540c\u985e\u5225\u5c0d\u65bc\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4e0d\u540c\u7684\uff0c\u4f8b\u5982\u5b57\u4e32\u7684 + \u6703\u5c07\u5169\u500b\u5b57\u4e32\u76f8\u9023\uff0c\u800c\u6574\u6578\u7684 + \u5247\u6703\u5c07\u5169\u500b\u6574\u6578\u76f8\u52a0\u3002

Question

  1. 2 ** 3 ** 2 \u7684\u7d50\u679c\u662f\u591a\u5c11?
  2. 2 ** (3 ** 2) \u7684\u7d50\u679c\u662f\u591a\u5c11?
  3. (2 ** 3) ** 2 \u7684\u7d50\u679c\u662f\u591a\u5c11?
"},{"location":"fundamental/python/operators/#comparison_operators","title":"Comparison Operators","text":"

\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7d50\u679c\u6703\u662f True \u6216 False\uff0c\u9019\u500b\u7d50\u679c\u6211\u5011\u7a31\u70ba\u5e03\u6797\u503c\u3002

\u6211\u5011\u4f86\u4ee5\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u6027\u8cea\u4f86\u505a\u4e00\u4e9b\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7df4\u7fd2\u3002

a, b, c = 3, 4, 5\n\nprint(a < b)\nprint(a > b)\nprint(a <= b)\nprint(a >= b)\nprint(a ** 2 + b ** 2 == c ** 2)\nprint(a ** 2 + b ** 2 != c ** 2)\nprint((a + b) > c)\n
ouput
False\nTrue\nFalse\nTrue\nFalse\nTrue\n

\u9019\u908a\u8981\u63d0\u9192\u4f60\uff0c == \u662f\u6bd4\u8f03\u904b\u7b97\u5b50\uff0c\u800c = \u662f\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u7684\u610f\u601d\u662f\u4e0d\u4e00\u6a23\u7684\u3002

\u6700\u5f8c\u4f86\u770b\u4e00\u500b\u4f8b\u5b50:

age = 14\nprint(12 <= age <= 18)\n\nx, y, z = 1, 2, 3\nprint(x < y < z)\nprint(x < y and y < z)\n

ouput
True\nTrue\nTrue\n

\u5728Python\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6bd4\u8f03\u904b\u7b97\u5b50\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u6a23\u7684\u5beb\u6cd5\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002

Question

  1. print(1 < 2 < 3 < 4 < 5) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. print(1 < 2 < 3 < 4 > 5) \u6703\u5370\u51fa\u4ec0\u9ebc?
"},{"location":"fundamental/python/operators/#logical_operators","title":"Logical Operators","text":"

\u6211\u5011\u7e7c\u7e8c\u5f80\u4e0b\u770b\uff0c\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u908f\u8f2f\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u591a\u500b\u5e03\u6797\u503c\u7d50\u5408\u6210\u4e00\u500b\u5e03\u6797\u503c\u3002

has_license = True\nis_drunk = False\nage = 18\n\nprint(f\"Can I drive a car? {has_license and not is_drunk and age >= 18}\")\nprint(not has_license)\nprint(not is_drunk)\nprint(age >= 18 or is_drunk or not has_license)\n
ouput
Can I drive a car? True\nFalse\nTrue\nTrue\n

\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c and \u53ea\u6709\u5728\u6240\u6709\u5e03\u6797\u503c\u90fd\u662f True \u7684\u6642\u5019\uff0c\u7d50\u679c\u624d\u6703\u662f True\uff0c\u800c or \u53ea\u8981\u6709\u4e00\u500b\u5e03\u6797\u503c\u662f True\uff0c\u7d50\u679c\u5c31\u6703\u662f True\u3002

\u56e0\u6b64\u6709\u6240\u8b02\u7684\u77ed\u8def\u6c42\u503c(Short-circuit Evaluation)\uff0c\u7576 and \u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f False\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a08\u7b97\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f False\uff1b\u800c or \u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f True\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a55\u4f30\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f True\u3002

\u800c not \u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u53ea\u6703\u5c07\u5e03\u6797\u503c\u53cd\u8f49\u3002

Question

  1. print(not True and False) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. print(not True or False) \u6703\u5370\u51fa\u4ec0\u9ebc?
  3. print(not True and not False) \u6703\u5370\u51fa\u4ec0\u9ebc?
  4. print(not True or not False) \u6703\u5370\u51fa\u4ec0\u9ebc?
"},{"location":"fundamental/python/operators/#bitwise_operators","title":"Bitwise Operators","text":"

\u5728\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u4f4d\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u662f\u5c0d\u4e8c\u9032\u4f4d\u7684\u904b\u7b97\uff0c\u6211\u5011\u53ef\u4ee5\u7528 bin() \u4f86\u89c0\u5bdf\u4e8c\u9032\u4f4d\u7684\u7d50\u679c\u3002

a, b = 2, 3\nprint(f\"a={bin(a)}\")\nprint(f\"b={bin(b)}\")\n\nprint(a & b, bin(a & b))\nprint(a | b, bin(a | b))\nprint(a ^ b, bin(a ^ b))\nprint(~a, bin(~a))\n\nprint(a << 1, bin(a << 1))\nprint(a >> 1, bin(a >> 1))\n
ouput
a=0b10\nb=0b11\n2 0b10\n3 0b11\n1 0b1\n-3 -0b11\n4 0b100\n1 0b1\n

& \u662f\u4f4d\u5143\u7684 and\uff0c | \u662f\u4f4d\u5143\u7684 or\uff0c ^ \u662f\u4f4d\u5143\u9593\u7684\u9032\u884c\u4e92\u65a5\u6216\u904b\u7b97\uff0c ~ \u662f\u4f4d\u5143\u7684 not\uff0c << \u662f\u4f4d\u5143\u7684\u5de6\u79fb\uff0c >> \u662f\u4f4d\u5143\u7684\u53f3\u79fb\u3002

\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u60f3\u60f3\u6211\u5011\u8a72\u5982\u4f55\u5224\u65b7\u4e00\u500b\u6578\u5b57\u662f\u5947\u6578\u9084\u662f\u5076\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 % \u4f86\u5224\u65b7\uff0c\u4f46\u662f\u6211\u5011\u4e5f\u53ef\u4ee5\u7528\u4f4d\u5143\u904b\u7b97\u4f86\u5224\u65b7\u3002

a, b = 5678, 4843\nprint(f\"Is a even? {a % 2 == 0}\")\nprint(f\"Is b odd? {b & 1 == 1}\")\n
ouput
Is a even? True\nIs b odd? True\n

Question

  1. \u5982\u4f55\u5224\u65b7\u4e00\u500b\u6578\u5b57\u662f 2 \u7684\u51aa?
  2. \u5982\u4f55\u5f97\u5230\u4e00\u500b\u6578\u5b57\u7684 16 \u500d\uff0c\u4f46\u4e0d\u80fd\u7528 * \u4e58\u6cd5\u904b\u7b97\u5b50?
"},{"location":"fundamental/python/operators/#assignment_operators","title":"Assignment Operators","text":"

\u6211\u5011\u5df2\u7d93\u5b78\u6703\u4e86\u4e00\u4e9b\u904b\u7b97\u5b50\uff0c\u73fe\u5728\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u4e00\u4e9b\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u904b\u7b97\u7d50\u679c\u6307\u5b9a\u7d66\u8b8a\u6578\u3002

\u5148\u4f86\u770b\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 += \u4f86\u5c07\u8b8a\u6578\u52a0\u4e0a\u67d0\u500b\u503c\uff0c\u9019\u500b\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002

x = 1\nx = x + 2\ny = 1\ny += 2\nprint(x, y)\n
ouput
3 3\n

\u518d\u4f86\u770b\u770b\u5176\u4ed6\u7684\u6307\u6d3e\u904b\u7b97\u5b50\u3002

x = 1\nx += 2\nprint(x)\nx -= 1\nprint(x)\nx *= 3\nprint(x)\nx //= 2\nprint(x)\nx **= 2\nprint(x)\nx <<= 1\nprint(x)\n
ouput
3\n2\n6\n3\n9\n18\n

\u5c0d\u4e86\uff0c\u8acb\u4f60\u5225\u5fd8\u8a18 = \u4e5f\u662f\u4e00\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u3002

x = y = z = 1\nprint(x, y, z)\n
ouput
1 1 1\n

\u5e0c\u671b\u9019\u500b\u4f8b\u5b50\u53ef\u4ee5\u8b93\u4f60\u66f4\u719f\u6089\u6307\u6d3e\u904b\u7b97\u5b50\u3002

Question

  1. \u6709\u6c92\u6709 and= \u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50?
  2. &= \u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4ec0\u9ebc?
"},{"location":"fundamental/python/operators/#bonus_f-string_for_float","title":"Bonus: f-string for float","text":"

\u70ba\u4e86\u80fd\u8b93\u4f60\u7df4\u7fd2\u4e00\u4e9b\u984c\u76ee\uff0c\u6211\u5148\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u5370\u51fa\u6d6e\u9ede\u6578\u5230\u6307\u5b9a\u4f4d\u6578\u3002

\u5728 Say Hello to Python - Input \u4e2d\uff0c\u6709\u7a0d\u5fae\u63d0\u904e\uff0c\u5982\u679c\u4f60\u5b8c\u5168\u6c92\u5370\u8c61\uff0c\u8acb\u4f60\u56de\u53bb\u8907\u7fd2\u4e00\u4e0b\u3002

\u7d66\u4f60\u6d6e\u9ede\u6578 x \uff0c\u8acb\u4f60\u5370\u51fa x \u7684\u5e73\u65b9\u6839\u8207\u5e73\u65b9\uff0c\u4e26\u4e14\u53ea\u5370\u51fa\u5c0f\u6578\u9ede\u5f8c\u5169\u4f4d\u3002

x = 3.1415926\nprint(f\"The square root of {x} is {x ** 0.5:.2f}\")\nprint(f\"The square of {x} is {x ** 2:.2f}\")\n
ouput
The square root of 3.1415926 is 1.77\nThe square of 3.1415926 is 9.87\n

@EditTime : 2024-01-27 11:52

"},{"location":"fundamental/python/operators/#bonus_map_for_input","title":"Bonus: map for input","text":"

\u518d\u6b21\u8907\u7fd2 Variable and Input - Mutiple Input \u4e2d\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 split() \u4f86\u5c07\u8f38\u5165\u7684\u5b57\u4e32\u5207\u5272\u6210\u591a\u500b\u5b57\u4e32\u3002

\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u5c07\u9019\u4e9b\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u600e\u9ebc\u505a\u5462?

a, b, c = input().split()\nprint(int(a) + int(b) + int(c))\n
input
1 2 3\n
ouput
6\n

\u96d6\u7136\u9019\u6a23\u5beb\u4e5f\u53ef\u4ee5\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u8f38\u5165\u5f88\u591a\u500b\u6578\u5b57\uff0c\u9019\u6a23\u5beb\u5c31\u6703\u5f88\u9ebb\u7169\uff0c\u9019\u6642\u5019\u6211\u5011\u53ef\u4ee5\u7528 map() \u4f86\u5e6b\u52a9\u6211\u5011\u3002

a, b, c = map(int, input().split())\nprint(a + b + c)\n
input
4 5 6\n
ouput
15\n

map(function, iterable) \u6703\u5c07 iterable \u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u4e1f\u9032 function \u4e2d\uff0c\u5728\u9019\u88e1\u7684 iterable \u662f input().split()\uff0c\u800c function \u662f int\uff0c\u56e0\u6b64 map(int, input().split()) \u6703\u5c07 input().split() \u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u8f49\u63db\u6210\u6574\u6578\u3002

\u4f60\u53ef\u4ee5\u5617\u8a66\u5c07\u4f7f\u7528\u5225\u7684\u51fd\u5f0f\uff0c\u4f8b\u5982 float \u6216 str\uff0c\u4f46\u8acb\u4f60\u8a18\u5f97\u4e0d\u8981\u52a0\u4e0a\u62ec\u865f\uff0c\u56e0\u70ba\u6211\u5011\u53ea\u662f\u8981\u5c07\u51fd\u5f0f\u7684\u540d\u7a31\u50b3\u9032\u53bb\uff0c\u800c\u4e0d\u662f\u8981\u57f7\u884c\u51fd\u5f0f\u3002

\u6211\u5011\u4f7f\u7528 Unpacking \u7684\u65b9\u5f0f\u4f86\u5c07 map() \u7684\u7d50\u679c\u6307\u6d3e\u7d66\u8b8a\u6578\u3002\u6211\u76f8\u4fe1\u4f60\u9084\u8a18\u5f97\u4ec0\u9ebc\u662f Unpacking \u5427?

\u5982\u679c\u4f60\u4e0d\u592a\u80fd\u7406\u89e3\uff0c\u4e5f\u6c92\u95dc\u4fc2\uff0c\u5148\u5b78\u6703\u600e\u9ebc\u7528\u5c31\u597d\u3002

"},{"location":"fundamental/python/operators/#practice","title":"Practice","text":"

\u6709\u4e86\u672c\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u5176\u5be6\u5df2\u7d93\u53ef\u4ee5\u505a\u5f88\u591a\u984c\u76ee\u4e86\uff0c\u6211\u5011\u4f86\u505a\u4e00\u4e9b\u7df4\u7fd2\u984c\u5427!

Itsa - [C_MM01-\u6613] \u8a08\u7b97\u68af\u578b\u9762\u7a4d

Reference code
a, b, h = map(int, input().split())\narea = (a + b) * h / 2\nprint(f\"Trapezoid area:{area}\")\n

Itsa - [C_MM02-\u6613] \u8a08\u7b97\u4e09\u89d2\u5f62\u9762\u7a4d

Reference code
a, b = map(int, input().split())\narea = a * b / 2\nprint(area)\n

Itsa - [C_MM04-\u6613] \u8a08\u7b97\u7e3d\u548c\u3001\u4e58\u7a4d\u3001\u5dee\u3001\u5546\u548c\u9918\u6578

Reference code
a, b = map(int, input().split())\n\nprint(f\"{a}+{b}={a + b}\")\nprint(f\"{a}*{b}={a * b}\")\nprint(f\"{a}-{b}={a - b}\")\nprint(f\"{a}/{b}={a // b}...{a % b}\")\n

Itsa - [C_MM06-\u6613] \u82f1\u54e9\u8f49\u516c\u91cc

Reference code
mile = int(input())\nkm = mile * 1.6\nprint(f\"{km:.1f}\")\n
"},{"location":"fundamental/python/operators/#assignment","title":"Assignment","text":"

Itsa - [C_MM07-\u6613] \u8a08\u7b97\u5e73\u65b9\u503c\u8207\u7acb\u65b9\u503c

Itsa - [C_MM08-\u6613] \u8a08\u7b97\u5169\u6578\u548c\u7684\u5e73\u65b9\u503c

Itsa - [C_MM10-\u6613] \u651d\u6c0f\u6eab\u5ea6\u8f49\u83ef\u5f0f\u6eab\u5ea6

Itsa - [C_MM11-\u6613] \u8cfc\u7968\u8a08\u7b97

Itsa - [C_MM12-\u6613] \u76f8\u9047\u6642\u9593\u8a08\u7b97

Itsa - [C_MM14-\u6613] \u8a08\u7b97\u6642\u9593\u7684\u7d44\u5408

@EditTime : 2024-01-28 22:03

"},{"location":"fundamental/python/repetition_structures/","title":"Repetition Structures","text":""},{"location":"fundamental/python/repetition_structures/#introduction","title":"Introduction","text":"

\u96fb\u8166\u6700\u6703\u505a\u7684\u4e8b\uff0c\u5c31\u662f\u91cd\u8907\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u9019\u88e1\u8981\u8ddf\u4f60\u4ecb\u7d39\u8ff4\u5708(Loop)\uff0c\u8ff4\u5708\u53ef\u4ee5\u8b93\u6211\u5011\u91cd\u8907\u57f7\u884c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u800c\u4e0d\u7528\u4e00\u884c\u4e00\u884c\u7684\u5beb\u51fa\u4f86\u3002

\u5982\u679c\u4f60\u8981\u5370 \\(100\\) \u6b21 Hello World!\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?

# ...\nprint(\"Hello World!\")\nprint(\"Hello World!\")\nprint(\"Hello World!\")\n

\u4f60\u6703\u771f\u7684\u5beb \\(100\\) \u884c\u55ce?\u7576\u7136\u4e0d\u6703\uff0c\u4e0d\u7136\u4f60\u6703\u6c23\u6b7b\uff0c\u7136\u5f8c\u628a\u96fb\u8166\u7838\u4e86\u3002

"},{"location":"fundamental/python/repetition_structures/#for_loop","title":"For Loop","text":"

\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b for \u8ff4\u5708\u7684\u7528\u6cd5\u3002\u4e26\u8209\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5370\u51fa \\(1\\) \u5230 \\(5\\):

for i in range(1, 6, 1):\n    print(i, end=' ')\n
ouput
1 2 3 4 5\n

Time Loop - Purrple Cat

range\uff0c\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6578\u5217\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u503c\u3001\u7d50\u675f\u503c\u3001\u9593\u9694\u503c\u3002\u9019\u500b\u51fd\u5f0f\u7684\u7528\u6cd5\u5982\u4e0b:

\u5982\u679c\u53ea\u6709\u4e00\u500b\u53c3\u6578\uff0c\u90a3\u5c31\u662f\u7d50\u675f\u503c\uff0c\u8d77\u59cb\u503c\u9810\u8a2d\u70ba \\(0\\)\uff0c\u9593\u9694\u503c\u9810\u8a2d\u70ba \\(1\\)\u3002

range(start=0, stop, step=1) \u4e26\u4e14\u6ce8\u610f\uff0cstop \u662f\u4e0d\u5305\u542b\u5728\u6578\u5217\u4e2d\u7684\u3002

"},{"location":"fundamental/python/repetition_structures/#foreach","title":"Foreach","text":"

\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u60f3\u628a\u5b57\u4e32 \"Speed_of_Light\" \u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u90fd\u5370\u51fa\u4f86\u3002

for c in \"Speed_of_Light\":\n    print(c, end=' ')\n

ouput

S p e e d _ o f _ L i g h t \n
DJ OKAWARI feat. \u4e8c\u5bae\u611b \u300cSpeed of Light\u300d

\u9019\u88e1\u7684\u8ff4\u5708\u66f4\u63a5\u8fd1\u6982\u5ff5\u4e0a\u7684 Foreach\uff0c\u6bcf\u6b21\u8ff4\u5708\u90fd\u6703\u53d6\u51fa\u5b57\u4e32\u4e2d\u7684\u4e00\u500b\u5b57\u5143\uff0c\u4e26\u4e14\u5c07\u4ed6\u653e\u5230 c \u4e2d\uff0c\u9019\u500b\u6982\u5ff5\u8acb\u4f60\u8a18\u8d77\u4f86\u3002

"},{"location":"fundamental/python/repetition_structures/#break_and_continue","title":"Break and Continue","text":"

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(\\sum_{i=1}^n{i}=1+2+\\cdots+n\\)

n = int(input())\ntotal = 0\n\n# 1 + 2 + ... + n\nfor i in range(n + 1):\n    total += i\n\nprint(total)\n
input
100\n
ouput
5050\n

\u63a5\u4e0b\u4f86\uff0c\u8ddf\u4f60\u4ecb\u7d39 break \u8207 continue\uff0c\u9019\u5169\u500b\u95dc\u9375\u5b57\u53ef\u4ee5\u7528\u4f86\u63a7\u5236\u8ff4\u5708\u7684\u57f7\u884c\u3002

break \u53ef\u4ee5\u7528\u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u800c continue \u5247\u662f\u5f37\u5236\u8df3\u5230\u4e0b\u4e00\u6b21\u8ff4\u5708\u3002

for i in range(1, 11):\n    if i == 5:\n        break\n    print(i, end=' ')\n
ouput
1 2 3 4\n

\u53ef\u4ee5\u767c\u73fe\uff0ci == 5 \u7684\u6642\u5019\uff0c\u8ff4\u5708\u5c31\u88ab\u5f37\u5236\u4e2d\u65b7\u4e86\u3002

for i in range(1, 11):\n    if i == 5:\n        continue\n    print(i, end=' ')\n
ouput
1 2 3 4 6 7 8 9 10\n

\u53ef\u4ee5\u767c\u73fe\uff0c5 \u88ab\u8df3\u904e\u4e86\u3002

"},{"location":"fundamental/python/repetition_structures/#nested_loop","title":"Nested Loop","text":"

\u63a5\u8457\u4ecb\u7d39\u5de2\u72c0\u8ff4\u5708(Nested Loop)\uff0c\u4e5f\u5c31\u662f\u8ff4\u5708\u88e1\u9762\u9084\u6709\u8ff4\u5708\u3002\u505a\u500b\u6bd4\u55bb\u7684\u8a71\uff0c\u5982\u679c\u662f\u5169\u5c64\u7684\u8ff4\u5708\uff0c\u5c31\u50cf\u662f\u6642\u91dd\u8207\u5206\u91dd\uff0c\u5167\u5c64\u8ff4\u5708\u6bcf\u8dd1\u4e00\u5708\uff0c\u5916\u5c64\u8ff4\u5708\u624d\u8dd1\u4e00\u683c\u3002

\u6211\u5011\u4f86\u5370\u4e00\u500b\u76f4\u89d2\u4e09\u89d2\u5f62\u5427\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u5370\u51fa \\(n\\) \u5c64\u7684\u76f4\u89d2\u4e09\u89d2\u5f62\uff0c\u5c07\u6700\u9802\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(0\\)\uff0c\u6700\u5e95\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(n-1\\)\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u5c64\u6709 \\(i+1\\) \u500b\u661f\u661f *\u3002

n = int(input())\n\nfor i in range(n):\n    for j in range(i + 1):\n        print('*', end='')\n    print()\n

\u6211\u7a31\u5167\u5c64\u7684\u8ff4\u5708\u53eb\u505a j \u8ff4\u5708\uff0c\u5916\u5c64\u7684\u8ff4\u5708\u53eb\u505a i \u8ff4\u5708\u3002 j \u8ff4\u5708\u63a7\u5236\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\uff0c\u800c i \u8ff4\u5708\u5247\u63a7\u5236\u7e3d\u5171\u6709\u5e7e\u5c64\u3002\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\u90fd\u662f i + 1\uff0c\u6700\u5f8c\u6703\u63db\u884c\u3002

input
5\n
output
*\n**\n***\n****\n*****\n

\u518d\u4f86\u770b\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u4e5d\u4e5d\u4e58\u6cd5\u8868\u3002

for i in range(1, 10):\n    for j in range(1, 10):\n        print(f\"{i} * {j} = {i * j}\", end='\\t')\n    print()\n
output
1 * 1 = 1   1 * 2 = 2   1 * 3 = 3   1 * 4 = 4   1 * 5 = 5   1 * 6 = 6   1 * 7 = 7   1 * 8 = 8   1 * 9 = 9   \n2 * 1 = 2   2 * 2 = 4   2 * 3 = 6   2 * 4 = 8   2 * 5 = 10  2 * 6 = 12  2 * 7 = 14  2 * 8 = 16  2 * 9 = 18  \n3 * 1 = 3   3 * 2 = 6   3 * 3 = 9   3 * 4 = 12  3 * 5 = 15  3 * 6 = 18  3 * 7 = 21  3 * 8 = 24  3 * 9 = 27  \n4 * 1 = 4   4 * 2 = 8   4 * 3 = 12  4 * 4 = 16  4 * 5 = 20  4 * 6 = 24  4 * 7 = 28  4 * 8 = 32  4 * 9 = 36  \n5 * 1 = 5   5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25  5 * 6 = 30  5 * 7 = 35  5 * 8 = 40  5 * 9 = 45  \n6 * 1 = 6   6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36  6 * 7 = 42  6 * 8 = 48  6 * 9 = 54  \n7 * 1 = 7   7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49  7 * 8 = 56  7 * 9 = 63  \n8 * 1 = 8   8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64  8 * 9 = 72  \n9 * 1 = 9   9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81  \n

i \u63a7\u5236\u5217(Row)\uff0cj \u63a7\u5236\u884c(Column)\uff0c\u6bcf\u4e00\u5217\u7684\u6578\u5b57\u90fd\u662f i\uff0c\u6bcf\u4e00\u884c\u7684\u6578\u5b57\u90fd\u662f j\uff0c\u6240\u4ee5 i * j \u5c31\u662f\u76f8\u61c9\u7684\u4e58\u7a4d\u3002

\u71b1\u8eab\u5b8c\u4e86\uff0c\u4f86\u770b\u4e00\u500b\u7a0d\u5fae\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57\uff0c\u5224\u65b7\u4ed6\u662f\u4e0d\u662f\u8cea\u6578\u3002

\u9019\u500b\u4f8b\u5b50\u6703\u662f\u7b49\u7b49\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\u7684\u57fa\u790e\uff0c\u6240\u4ee5\u8acb\u4ed4\u7d30\u770b\u3002

n = int(input())\nis_prime = True\n\nfor i in range(2, n):\n    if n % i == 0:\n        is_prime = False\n        break\n\nif is_prime:\n    print(\"Yes\")\nelse:\n    print(\"No\")\n
input
17\n4843\n
ouput
Yes\nNo\n

n \u5982\u679c\u53ef\u4ee5\u6574\u9664 1 \u8207\u81ea\u5df1\u4ee5\u5916\u7684\u6578\u5b57\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 break \u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8a08\u7b97\u3002

\u6211\u5011\u628a\u554f\u984c\u7528\u5f97\u66f4\u8907\u96dc\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u8f38\u51fa\u5728 \\([1, n]\\) \u4e4b\u9593\u6700\u5927\u7684\u8cea\u6578\u3002

\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u8b93 range \u5012\u8457\u6578\u56de\u4f86\u3002

n = int(input())\n\nfor i in range(n, 0, -1):\n    is_prime = True\n    for j in range(2, i):\n        if i % j == 0:\n            is_prime = False\n            break\n\n    if is_prime:\n        print(f\"{i} is the largest prime in [1, {n}]\")\n        break\n
input
100\n2\n
output
97 is the largest prime in [1, 97]\n2 is the largest prime in [1, 2]\n

i \u5217\u8209 \\([n, 1]\\) \u7684\u6578\u5b57\uff0c\u800c j \u5217\u8209 \\([2, i-1]\\) \u7684\u6578\u5b57\uff0c\u5982\u679c i \u53ef\u4ee5\u6574\u9664 j\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u99ac\u4e0a\u8df3\u51fa\u5167\u5c64 j \u8ff4\u5708\uff0c\u7e7c\u7e8c\u5217\u8209\u4e0b\u4e00\u500b\u6578\u5b57\u3002\u5982\u679c i \u662f\u8cea\u6578\uff0c\u90a3\u5c31\u5370\u51fa\u4f86\uff0c\u4e26\u4e14\u8df3\u51fa\u5916\u5c64 i \u8ff4\u5708\u3002

\u8acb\u4f60\u4ed4\u7d30\u7aef\u8a73\u9019\u5169\u500b\u4f8b\u5b50\u4e2d\u5f37\u8abf\u7684\u884c\u6578\u3002

@EditTime : 2024-01-30 16:33

"},{"location":"fundamental/python/repetition_structures/#while_loop","title":"While loop","text":"

\u63a5\u8457\u4f86\u4ecb\u7d39 while \u8ff4\u5708\uff0c\u4ed6\u7684\u4f7f\u7528\u5834\u666f\u662f\u7576\u4f60\u4e0d\u77e5\u9053\u8ff4\u5708\u8981\u57f7\u884c\u5e7e\u6b21\u7684\u6642\u5019\uff0c\u5c31\u53ef\u4ee5\u7528 while \u8ff4\u5708\uff0c\u4f46\u662f\u5225\u5beb\u51fa\u7121\u7aae\u8ff4\u5708(Infinite Loop)\u5594\u3002

\u4f46\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u5148\u4f86\u770b\u5982\u4f55\u7528 while \u8ff4\u5708\u4f86\u5370\u51fa \\(1\\) \u5230 \\(5\\)\u3002

i = 1\nwhile i < 6:\n    print(i, end=\" \")\n    i += 1\n
ouput
1 2 3 4 5\n

\u56de\u9867\u4e00\u4e0b for loop\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe\u908f\u8f2f\u5176\u5be6\u4e00\u6a23\u3002

\u7576\u689d\u4ef6\u6210\u7acb\u7684\u6642\u5019\uff0c\u5c31\u6703\u57f7\u884c\u8ff4\u5708\uff0c\u76f4\u5230\u689d\u4ef6\u4e0d\u6210\u7acb\u3002

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\(n!\\)

n = int(input())\ni = 1\nfact = 1\n\nwhile i <= n:\n    fact *= i\n    i += 1\n\nprint(fact)\n
input
5\n1\n
ouput
120\n1\n

\u4f46\u76ee\u524d\u9019\u5169\u500b\u4f8b\u5b50\u7121\u6cd5\u770b\u51fa while \u7684\u9b45\u529b\uff0c\u56e0\u70ba\u4f60\u90fd\u77e5\u9053\u8ff4\u5708\u4ec0\u9ebc\u6642\u5019\u6703\u7d50\u675f\uff0c\u6240\u4ee5\u8ddf\u4f60\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u554f\u984c:

\\[ \\text{The} \\ 3n+1 \\ \\text{Problem} \\ aka \\ \\text{Collatz Conjecture} \\]

\u8003\u62c9\u8332\u731c\u60f3 wikipedia

\u662f\u6307\u5c0d\u65bc\u6bcf\u4e00\u500b\u6b63\u6574\u6578\uff0c\u5982\u679c\u5b83\u662f\u5947\u6578\uff0c\u5247\u5c0d\u5b83\u4e583\u518d\u52a01\uff0c\u5982\u679c\u5b83\u662f\u5076\u6578\uff0c\u5247\u5c0d\u5b83\u9664\u4ee52\uff0c\u5982\u6b64\u5faa\u74b0\uff0c\u6700\u7d42\u90fd\u80fd\u5920\u5f97\u52301\u3002

\u90a3\u6211\u5011\u8981\u5beb\u7684\u7a0b\u5f0f\u662f\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa\u4ed6\u6703\u5728\u5e7e\u6b65\u5f8c\u8b8a\u6210 \\(1\\)\u3002

\\[ f(n)=\\begin{cases}\\frac{n}{2}, & \\text{if } n \\text{ is even} \\\\ 3n+1, & \\text{if } n \\text{ is odd}\\end{cases} \\]

\u4f9d\u7167\u5b9a\u7fa9\uff0c\u6211\u5011\u53ef\u4ee5\u5beb\u51fa\u4ee5\u4e0b\u7a0b\u5f0f\u78bc:

n = int(input())\nstep = 0\n\nwhile n != 1:\n    print(n, end=\" -> \")\n    if n % 2 == 0:\n        n = n // 2\n    else:\n        n = 3 * n + 1\n    step += 1\n\nprint(1)\nprint(f\"step = {step}\")\n
input
22\n1\n
ouput
22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1\nstep = 15\n1\nstep = 0\n

\u518d\u4f86\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\gt 1\\)\uff0c\u8f38\u51fa \\(n\\) \u7684\u8cea\u56e0\u6578\u5206\u89e3\u3002

\u8acb\u4f60\u5148\u81ea\u5df1\u60f3\u4e00\u4e0b\uff0c\u518d\u770b\u770b\u53c3\u8003\u7a0b\u5f0f\u78bc\u3002

Example
n = int(input())\nfactor = 2\n\nwhile n != 1:\n    while n % factor == 0:\n        print(factor, end=' ')\n        n //= factor\n    factor += 1\n
input
20\n4843\n
ouput
2 2 5\n29 167\n

\u5167\u5c64\u7684 while \u8ff4\u5708\u6703\u4e00\u76f4\u57f7\u884c\uff0c\u76f4\u5230 n \u4e0d\u662f factor \u7684\u500d\u6578\u70ba\u6b62\u3002\u5916\u5c64\u7684 while \u8ff4\u5708\u5247\u662f\u5217\u8209\u6240\u6709\u7684\u8cea\u56e0\u6578\uff0c\u76f4\u5230 n \u8b8a\u6210 \\(1\\) \u70ba\u6b62\u3002

\u984c\u5916\u8a71\uff0c\u7576\u521d\u9047\u5230\u9019\u500b\u984c\u76ee\u7684\u6642\u5019\uff0c\u60f3\u4e86\u4e00\u4e0b\u7d50\u679c\u4e00\u6b21\u5c31AC\uff0c\u662f\u5f88\u6709\u6210\u5c31\u611f\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5\u4e5f\u8acb\u4f60\u597d\u597d\u52aa\u529b\uff0c\u9010\u6b65\u57f9\u990a\u81ea\u4fe1\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7684\u3002

@EditTime : 2024-01-30 17:55

"},{"location":"fundamental/python/repetition_structures/#bonus_for_else_and_while_else","title":"Bonus: for ... else and while ... else","text":"

for \u8207 while \u8ff4\u5708\u90fd\u53ef\u4ee5\u642d\u914d else \u4f86\u4f7f\u7528\uff0c\u7576\u8ff4\u5708\u6b63\u5e38\u7d50\u675f\u7684\u6642\u5019(\u6c92\u6709 break )\uff0c\u5c31\u6703\u57f7\u884c else\u3002

\n

to be continued...

"},{"location":"fundamental/python/repetition_structures/#practice","title":"Practice","text":"

Itsa - [C_MM03-\u6613] \u5169\u6578\u7e3d\u548c

Reference code

while True:\n    a, b = map(int, input().split())\n    print(a + b)\n
\u9019\u984c\u6703\u51fa\u73fe\u5728\u9019\u88e1\uff0c\u55ae\u7d14\u662f\u56e0\u70ba\u8981\u91cd\u8907\u8f38\u5165\u3002

Itsa - [C_MM26-\u6613] \u8f38\u51fa 1x1\u30012x2\u3001...\u3001NxN\u4e4b\u7d50\u679c

Reference code
N = int(input())\n\nfor i in range(1, N + 1):\n    print(f\"{i}*{i}={i * i}\")\n

Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578

Reference code

N = int(input())\n\nfor i in range(6, N + 1, 2):\n    s = 0\n    for j in range(1, i):\n        if i % j == 0:\n            s += j\n    if s == i:\n        if i == 6:\n            print(i, end='')\n        else:\n            print(' ' + str(i), end='')\nprint()\n
\u9019\u662f\u66b4\u529b\u89e3\uff0c\u6703TLE\uff0c\u4e0b\u4e00\u7ae0\u6703\u4ecb\u7d39\u5982\u4f55\u6700\u4f73\u5316\uff0c \u4f46\u5982\u679c\u4f7f\u7528 C++ \u7684\u8a71\uff0c\u76f8\u540c\u908f\u8f2f\u7684\u7a0b\u5f0f\u78bc\u662f\u53ef\u4ee5AC\u7684\u3002

Itsa - [C_MM34-\u6613] \u56e0\u6578\u554f\u984c

Reference code
N = int(input())\n\nprint(1, end=\"\")\nfor i in range(2, N + 1):\n    if N % i == 0:\n        print(f\" {i}\", end=\"\")\nprint()\n

@EditTime : 2024-01-30 18:40

"},{"location":"fundamental/python/repetition_structures/#assignment","title":"Assignment","text":"

Itsa - [C_MM21-\u6613] \u7b97\u968e\u4e58

Itsa - [C_MM25-\u6613] \u8a08\u7b97\u6b63\u6574\u6578\u88ab3\u6574\u9664\u4e4b\u6578\u503c\u4e4b\u7e3d\u548c

Itsa - [C_MM27-\u6613] \u8a08\u7b97\u5169\u6574\u6578\u9593\u6240\u6709\u6574\u6578\u7684\u7e3d\u548c

Itsa - [C_MM28-\u6613] \u8a08\u7b971\u5230N\u4e4b\u9593\u5c6c\u65bc5\u548c7\u7684\u500d\u6578

Itsa - [C_MM29-\u6613] \u6700\u5927\u8cea\u6578\u554f\u984c

Itsa - [C_MM30-\u6613] \u8cea\u6578\u5224\u5225

Itsa - [C_MM31-\u6613] \u8a08\u7b971~N\u5167\u80fd\u88ab2\u8ddf3\u6574\u9664\uff0c\u4f46\u4e0d\u80fd\u88ab12\u6574\u9664\u7684\u6574\u6578\u7e3d\u548c

Itsa - [C_MM40-\u6613] 1~N\u4e4b\u9593\u7684\u7e3d\u548c

Itsa - [C_MM49-\u6613] \u9023\u7e8c1\u7684\u500d\u6578

Itsa - [C_ST09-\u6613] \u661f\u865f\u77e9\u5f62\u8f38\u51fa

Itsa - [C_ST11-\u6613] \u661f\u865f\u83f1\u5f62\u8f38\u51fa

Itsa - [C_ST14-\u6613] \u6578\u5b57\u76f4\u89d2\u4e09\u89d2\u5f62\u8f38\u51fa

@EditTime : 2024-01-30 21:51

"},{"location":"fundamental/python/say_hello/","title":"Say Hello to Python!","text":""},{"location":"fundamental/python/say_hello/#first_program","title":"First Program","text":"

\u55e8\uff0c\u9019\u662f\u4f60\u7b2c\u4e00\u500b Python \u7a0b\u5f0f\uff0c\u5728 Pycharm \u4e2d\u5efa\u7acb\u4e00\u500b main.py \u6a94\u6848\uff0c\u4e26\u5728\u6a94\u6848\u4e2d\u6253\u4e0a :

# print(\"Hello Cat!\")\nprint(\"Hello World!\")\nprint('Hello Python!')\nprint(\"cheung4843\")\n
output
Hello World!\nHello Python!\ncheung4843\n

\u6309\u4e0b\u57f7\u884c\u5f8c\uff0c\u4f60\u7684\u63a7\u5236\u53f0(Console)\u5c07\u6703\u5370\u51fa Hello World! \u63a5\u8457\u63db\u884c\uff0c\u518d\u5370\u51fa Hello Python!\uff0c\u63a5\u8457\u518d\u5370\u51fa\u6211 cheung4843\uff0c\u518d\u63db\u884c\u3002

\u90a3 # print(\"Hello Cat!\") \u662f\u4ec0\u9ebc?\u4ee5\u4e95\u5b57\u865f\u70ba\u958b\u982d\u7684\uff0c\u88ab\u7a31\u70ba\u8a3b\u89e3\uff0c\u4ed6\u4e0d\u6703\u5728\u7a0b\u5f0f\u4e2d\u88ab\u57f7\u884c\uff0c\u4ed6\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7406\u89e3\u7a0b\u5f0f\u78bc\uff0c\u6216\u8005\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002

\u4e0d\u66c9\u5f97\u4f60\u662f\u5426\u6709\u767c\u73fe \"Hello World!\" \u8207 'Hello Python!' \u7684\u5dee\u7570?\u7576\u7136\u9664\u4e86\u5b57\u6bcd\u4e0d\u4e00\u6a23\u4e4b\u5916\uff0c\u9084\u6709\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u7684\u5dee\u5225\uff0c\u4f46\u4ed6\u5011\u90fd\u88ab\u7a31\u70ba\u5b57\u4e32(String)\uff0c\u5728 Python \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6210\u5c0d\u7684\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u4f86\u8868\u9054\u4e00\u500b\u5b57\u4e32\uff0c\u4f46\u662f\u4e0d\u80fd\u6df7\u7528\uff0c\u4f8b\u5982 :

print(\"I love cats')\n
output
SyntaxError: unterminated string literal (detected at line 1)\n

\u57f7\u884c\u5f8c\uff0c\u4f60\u6703\u767c\u73fe\u63a7\u5236\u53f0\u8ddf\u4f60\u5831\u544a\u4e86\u932f\u8aa4\uff0c\u9019\u662f\u8a9e\u6cd5\u932f\u8aa4\uff0c\u800c\u5f8c\u7e8c\u5beb\u7a0b\u5f0f\u7684\u904e\u7a0b\u4e2d\uff0c\u4f60\u5c07\u906d\u9047\u8a31\u591a\u8a9e\u610f\u932f\u8aa4\u3002

\u90a3\u4f60\u77e5\u9053\u4ec0\u9ebc\u662f\u7a7a\u5b57\u4e32\u55ce?

print('')\nprint(\"\")\nprint(\"Above are nothing, right?\")\n
output
Above are nothing, right?\n

\u57f7\u884c\u5b8c\u5f8c\uff0c\u4f60\u5c31\u77e5\u9053\u7a7a\u5b57\u4e32\u662f\u4ec0\u9ebc\u4e86\u5427?

\u63a5\u8457\u6211\u5011\u4f86\u8b1b\u8b1b print \u9019\u500b\u6771\u897f\uff0c\u6211\u5011\u5728 Python \u4e2d\u7a31\u4ed6\u70ba\u51fd\u5f0f(Function)\uff0c\u6211\u5011\u8981\u5982\u4f55\u4f7f\u7528\u5462?\u4e5f\u5c31\u662f\u5728\u51fd\u5f0f\u7684\u540d\u7a31\u5f8c\u52a0\u4e0a\u4e00\u500b\u62ec\u865f()\u3002\u800c print \u662f Python \u5167\u5efa\u7d66\u6211\u5011\u7684\u300c\u5de5\u5177\u300d\uff0c\u5c31\u662f\u8aaa\uff0c\u6211\u5011\u4e0d\u77e5\u9053\u4ed6\u662f\u600e\u9ebc\u88ab\u9020\u51fa\u4f86\u7684\uff0c\u6211\u5011\u73fe\u968e\u6bb5\u53ea\u8981\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u6216\u8a31\u4f60\u53ef\u80fd\u807d\u904e\u4e00\u500b\u8001\u6389\u7259\u7684\u6bd4\u55bb\uff0c\u7a31\u51fd\u5f0f\u5c31\u50cf\u662f\u4e00\u500b\u9ed1\u76d2\u5b50\u3002

\u95dc\u65bc\u51fd\u5f0f(Function)\uff0c\u6211\u6703\u5728\u5f80\u5f8c\u7684\u7ae0\u7bc0\u8ddf\u4f60\u4ecb\u7d39\uff0c\u800c\u73fe\u5728\u4f60\u53ea\u8981\u77e5\u9053 print() \u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u5370\u51fa\u4f86\uff0c\u4e26\u63db\u884c\u3002

\u90a3\u5982\u679c\u6211\u4e0d\u60f3\u63db\u884c\u5462?

print(\"You\")\nprint(\"are\")\nprint(\"my\", end=' ')\nprint(\"special\", end='\\n')\nprint(\"1234\", end=\"5\")\nprint(\"---------\")\n
ouput
You\nare\nmy special\n12345---------\n

\u5728\u9019\u500b\u5947\u602a\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u4f60\u767c\u73fe\u6211\u5728 print \u7684\u62ec\u865f\u4e2d\u591a\u52a0\u4e86\u4e00\u500b end= \u7684\u6771\u897f\uff0c\u8b93 print \u5370\u5b8c\u524d\u9762\u7684\u6771\u897f\u5f8c\uff0c\u518d\u5370\u51fa\u7b49\u65bc\u5f8c\u7684\u6771\u897f\u3002

You are my special

\u90a3 \\n \u662f\u4ec0\u9ebc\u6771\u897f?\u4ed6\u662f\u8df3\u812b\u5b57\u5143(Escape Character)\u5bb6\u65cf\u4e2d\u7684\u4e00\u54e1\uff0c\u4f60\u73fe\u5728\u53ea\u8981\u77e5\u9053\u4ed6\u80fd\u5920\u63db\u884c\u3002

Note

  1. \u96d9\u5f15\u865f/\u55ae\u5f15\u865f\u570d\u4f4f\u7684\u5167\u5bb9\uff0c\u7a31\u70ba\u5b57\u4e32\uff0c\u4f8b\u5982 \"cheung4843\" \u8207 '114514 + 1919810' \uff0c\u4f46\u8a18\u4f4f\u55ae\u96d9\u5f15\u865f\u4e0d\u5f97\u6df7\u7528\u3002
  2. print(x, end=y) \u6703\u5370\u51fa x \uff0c\u518d\u5370\u51fa y\uff0c\u800c y \u9810\u8a2d\u70ba \\n \u4e5f\u5c31\u662f\u63db\u884c\u3002

Question

  1. \u90a3\u9ebc \"'\" \u8207 \"\"\" \u662f\u5408\u6cd5\u7684\u5b57\u4e32\u55ce?
  2. print() \u62ec\u865f\u5167\u6c92\u6709\u653e\u6771\u897f\u6703\u5370\u51fa\u4ec0\u9ebc?
"},{"location":"fundamental/python/selection_structures/","title":"Selection Structures","text":""},{"location":"fundamental/python/selection_structures/#introduction","title":"Introduction","text":"

\u5728\u6211\u5011\u4eba\u751f\u4e2d\uff0c\u6211\u5011\u6703\u9762\u81e8\u5f88\u591a\u9078\u64c7\uff0c\u6703\u6839\u64da\u7576\u4e0b\u7684\u60c5\u6cc1\uff0c\u505a\u51fa\u4e0d\u540c\u7684\u6c7a\u5b9a\uff0c\u800c\u7a0b\u5f0f\u4e5f\u662f\u4e00\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u60c5\u6cc1\uff0c\u57f7\u884c\u4e0d\u540c\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u7a31\u70ba\u9078\u64c7\u7d50\u69cb(Selection Structures)\uff0c\u662f\u6d41\u7a0b\u63a7\u5236\u7684\u4e00\u7a2e\u3002

\u4f8b\u5982\uff0c\u6211\u6839\u64da\u5b78\u6e2c\u6210\u7e3e\uff0c\u4f86\u6c7a\u5b9a\u8981\u586b\u54ea\u4e9b\u5fd7\u9858\uff0c\u6216\u662f\u6211\u6839\u64da\u5929\u6c23\uff0c\u4f86\u6c7a\u5b9a\u8981\u4e0d\u8981\u5e36\u5098\u3002

"},{"location":"fundamental/python/selection_structures/#if_elif_else","title":"if ... elif ... else","text":"

\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u56de\u60f3 Operators - Comparison Operators \u7684\u5167\u5bb9\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u6bd4\u8f03\u904b\u7b97\u5b50\u4f86\u5f97\u5230\u4e00\u500b\u5e03\u6797\u503c\uff0c\u800c if \u6703\u6839\u64da True \u6216 False \u4f86\u6c7a\u5b9a\u662f\u5426\u57f7\u884c\u67d0\u6bb5\u7a0b\u5f0f\u78bc\u3002

\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50:

\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u8f38\u51fa\u4ed6\u7684\u7d55\u5c0d\u503c\u3002

num = int(input(\"Enter a number: \"))\n\n# get the absolute value of the input\nif num < 0:\n    num = -num\n\nprint(num)\n
input
-1984\n1984\n
ouput
1984\n1984\n

Ivan Torrent - \"1984\" Lyrics Video

\u908f\u8f2f\u5f88\u7c21\u55ae\uff0c\u5982\u679c\u8f38\u5165\u7684\u6578\u5b57\u5c0f\u65bc\u96f6\uff0c\u90a3\u9ebc\u5c31\u5c07\u4ed6\u653e\u4e0a\u8ca0\u865f\u3002\u63d0\u9192\u4f60\u4e00\u4e0b\uff0c\u9019\u88e1\u7684 - \u8207 not \u4e00\u6a23\uff0c\u90fd\u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c

\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u53ea\u6709\u7528\u5230 if\uff0c\u63a5\u8457\u6211\u5011\u6709\u8acb else \u767b\u5834\u3002

\u8acb\u770b\u4e0b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u5224\u65b7\u4ed6\u662f\u5947\u6578\u9084\u662f\u5076\u6578\u3002

num = int(input(\"Enter a number: \"))\n\n# odd or even\nif num % 2 == 0:\n    print(\"Even\")\nelse:\n    print(\"Odd\")\n
input
1983\n-1982\n
ouput
Odd\nEven\n

Timecop1983 - On the Run

\u9019\u88e1\u6211\u5011\u7528\u5230\u4e86 else\uff0c\u7576 if \u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c else \u7684\u5167\u5bb9\u3002

\u6211\u5011\u518d\u4f86\u770b\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4f60\u7684\u5206\u6578\uff0c\u8f38\u51fa\u4f60\u7684\u8a55\u50f9\uff0c\u56e0\u70ba\u6709\u5f88\u591a\u7a2e\u8a55\u50f9\uff0c\u6240\u4ee5\u9700\u8981\u7528\u5230 elif\u3002

\u7576 if \u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u9762 elif \u7684\u689d\u4ef6\uff0c\u5982\u679c elif \u7684\u689d\u4ef6\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c elif \u7684\u5167\u5bb9\uff0c\u5982\u679c\u76ee\u524d\u7684 elif \u7684\u689d\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u4e00\u500b elif \u7684\u689d\u4ef6\uff0c\u5982\u679c\u6240\u6709\u7684 elif \u7684\u689d\u4ef6\u90fd\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c else \u7684\u5167\u5bb9\u3002

\u9019\u6a23\u8b1b\u6216\u8a31\u6709\u9ede\u7e5e\u53e3\uff0c\u4f60\u53ef\u4ee5\u770b\u770b\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u61c9\u8a72\u5c31\u80fd\u7406\u89e3\u4e86\u3002

score = int(input(\"Enter your score: \"))\n\nif score >= 90:\n    print(\"A\")\n    print(\"Excellent!\")\nelif score >= 80:\n    print(\"B\")\nelif score >= 70:\n    print(\"C\")\nelif score >= 60:\n    print(\"D\")\nelse:\n    print(\"F\")\n
input
60\n90\n49\n
ouput
D\nA\nExcellent!\nF\n

\u63a5\u4e0b\u4f86\u7d66\u4f60\u4e00\u500b\u53ef\u80fd\u6703\u7591\u60d1\u7684\u4f8b\u5b50:

score = int(input(\"Enter your score: \"))\nif score >= 60:\n    print(\"D\")\nelif score >= 70:\n    print(\"C\")\nelif score >= 80:\n    print(\"B\")\nelif score >= 90:\n    print(\"A\")\n    print(\"Excellent!\")\nelse:\n    print(\"F\")\n
input
90\n
ouput
D\n

\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u5947\u602a\uff0c\u70ba\u4ec0\u9ebc\u8f38\u5165 90 \u6703\u5370\u51fa D\uff0c\u800c\u4e0d\u662f A\uff0c\u9019\u662f\u56e0\u70ba if \u7684\u689d\u4ef6\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c if \u7684\u5167\u5bb9\uff0c\u5c31\u4e0d\u6703\u6aa2\u67e5 elif \u7684\u689d\u4ef6\u4e86\u3002

\u6240\u4ee5\uff0c\u4f60\u5728\u64b0\u5beb\u689d\u4ef6\u5f0f\u7684\u6642\u5019\uff0c\u8981\u6ce8\u610f\u9806\u5e8f\uff0c\u4ee5\u53ca\u78ba\u4fdd\u6bcf\u500b\u689d\u4ef6\u662f\u5426\u662f\u4e92\u65a5\u7684\u3002

score = int(input(\"Enter your score: \"))\n\nif 70 > score >= 60:\n    print(\"D\")\nelif 80 > score >= 70:\n    print(\"C\")\nelif 90 > score >= 80:\n    print(\"B\")\nelif 100 >= score >= 90:\n    print(\"A\")\n    print(\"Excellent!\")\nelse:\n    print(\"F\")\n
input
90\n4843\n55\n
ouput
A\nExcellent!\nF\nF\n

\u9019\u6a23\u5c31\u4e0d\u6703\u6709\u554f\u984c\u4e86\uff0c\u4f46\u662f\uff0c\u7576\u6211\u8f38\u5165\u8d85\u904e 100 \u7684\u5206\u6578\u6642\uff0c\u662f\u6703\u5370\u51fa F \u7684\uff0c\u4f60\u8a72\u600e\u9ebc\u89e3\u6c7a\u5462?

"},{"location":"fundamental/python/selection_structures/#nested_if","title":"Nested if","text":"

if \u7684\u5167\u5bb9\u53ef\u4ee5\u662f\u53e6\u4e00\u500b if\uff0c\u9019\u7a2e\u7d50\u69cb\u7a31\u70ba\u5de2\u72c0\u689d\u4ef6\u5f0f(Nested if)\u3002

\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6709\u4e09\u500b\u6574\u6578 a, b, c \uff0c\u8acb\u4f60\u8f38\u51fa\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002

a, b, c = map(int, input().split())\nmax_one = None\n\nif a > b:\n    max_one = a\n    if c > max_one:\n        max_one = c\nelse:\n    max_one = b\n    if c > max_one:\n        max_one = c\n\nprint(max_one)\n
input
10 20 30\n12 12 12\n-3 -4 -5\n
ouput
30\n12\n-3\n

\u60f3\u6cd5\u5f88\u7c21\u55ae\uff0c\u5148\u8b93 a, b \u9032\u884c\u6bd4\u8f03\uff0c\u5f97\u5230\u8f03\u5927\u7684\u90a3\u500b\u6578\u5b57\uff0c\u518d\u8b93 c \u8207 max_one \u6bd4\u8f03\uff0c\u5f97\u5230\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002

\u9019\u88e1\u5077\u5077\u544a\u8a34\u4f60\u4e00\u500b\u795e\u5947\u5999\u5999\u5de5\u5177\uff0cmax()\uff0c\u4ed6\u6703\u627e\u51fa\u62ec\u865f\u5167\u7684\u6771\u897f\u4e2d\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002

a, b, c = map(int, input().split())\nprint(max(a, b, c))\n
input
114514 1919 810\n
ouput
114514\n
"},{"location":"fundamental/python/selection_structures/#match_case","title":"match ... case","text":"

\u5728 Python 3.10 \u4e2d\uff0c\u65b0\u589e\u4e86 match ... case \uff0c\u4f46\u907a\u61be\u7684\u662f\u4e00\u4e9b\u53e4\u8001\u7684 Online Judge \u7684 Python \u7248\u672c\u592a\u820a\u4e0d\u80fd\u7528\uff0c\u4f46\u6211\u9084\u662f\u60f3\u4ecb\u7d39\u7d66\u4f60\u3002

\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5728 if ... elif ... else \u4e2d\u7684\u4f8b\u5b50:

month = int(input(\"Enter a month (1-12): \"))\n\n# which season?\nif 3 <= month <= 5:\n    print(\"Spring\")\nelif 6 <= month <= 8:\n    print(\"Summer\")\nelif 9 <= month <= 11:\n    print(\"Fall\")\nelif month == 12 or month == 1 or month == 2:\n    print(\"Winter\")\nelse:\n    print(\"Invalid month\")\n
input
4\n7\n10\n1\n13\n
ouput
Spring\nSummer\nFall\nWinter\nInvalid month\n

\u518d\u4f86\u770b\u770b match ... case \u7684\u7248\u672c:

month = int(input(\"Enter a month (1-12): \"))\n\nmatch month:\n    case 3 | 4 | 5:\n        print(\"Spring\")\n    case 6 | 7 | 8:\n        print(\"Summer\")\n    case 9 | 10 | 11:\n        print(\"Autumn\")\n    case 12 | 1 | 2:\n        print(\"Winter\")\n    case _:\n        print(\"Invalid month\")\n
input
270\n11\n3\n
ouput
Invalid month\nAutumn\nSpring\n

\u662f\u4e0d\u662f\u5f88\u7c21\u6f54\u5462?\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u50cf\u662f C, C++, Java \u7b49\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u770b\u904e switch .. case\uff0c\u4f46\u662f match ... case \u66f4\u5f37\u5927!

\u95dc\u65bc match ... case \u6211\u5c31\u4ecb\u7d39\u5230\u9019\u88e1\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u4f60\u8981\u77e5\u9053 if ... elif ... else \u7684\u7528\u6cd5\u3002

@EditTime : 2024-01-29 12:32

"},{"location":"fundamental/python/selection_structures/#practice","title":"Practice","text":"

Itsa - [C_MM09-\u6613] \u8a08\u7b97 i \u6b21\u65b9\u7684\u503c

Reference code
i = int(input())\nif i > 31:\n    print(\"Value of more than 31\")\nelse:\n    print(1 << i)\n

Itsa - [C_MM13-\u6613] \u505c\u8eca\u8cbb\u8a08\u7b97

Reference code
h1, m1 = map(int, input().split())\nh2, m2 = map(int, input().split())\n\npaid = 0\nminutes = (h2 - h1) * 60 + (m2 - m1)\nif minutes > 240:\n    paid += ((minutes - 240) // 30) * 60\n    minutes = 240\nif minutes > 120:\n    paid += ((minutes - 120) // 30) * 40\n    minutes = 120\npaid += (minutes // 30) * 30\n\nprint(paid)\n

Itsa - [C_MM15-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u6b63\u65b9\u5f62\u7684\u7bc4\u570d\u5167

Reference code
x, y = map(int, input().split())\n\nif 0 <= x <= 100 and 0 <= y <= 100:\n    print(\"inside\")\nelse:\n    print(\"outside\")\n
"},{"location":"fundamental/python/selection_structures/#assignment","title":"Assignment","text":"

Itsa - [C_MM16-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u5713\u5f62\u7684\u7bc4\u570d\u5167

Itsa - [C_MM19-\u6613] \u96fb\u8a71\u8cbb\u8a08\u7b97

Itsa - [C_MM24-\u6613] \u8a08\u7b97\u85aa\u6c34

Itsa - [C_MM32-\u6613] Armstrong\u6578

Itsa - [C_MM35-\u6613] \u5e73\u3001\u958f\u5e74\u5224\u5b9a

Itsa - [C_MM36-\u6613] \u5b63\u7bc0\u5224\u5b9a

Itsa - [C_MM37-\u6613] \u5224\u65b7\u5ea7\u6a19\u4f4d\u65bc\u4f55\u8655

Itsa - [C_MM38-\u6613] \u5224\u65b73\u6574\u6578\u662f\u5426\u80fd\u69cb\u6210\u4e09\u89d2\u5f62\u4e4b\u4e09\u908a\u9577

Itsa - [C_MM39-\u6613] \u5224\u65b7\u662f\u4f55\u7a2e\u4e09\u89d2\u5f62

Itsa - [C_MM46-\u6613] \u8907\u6578\u904b\u7b97

Itsa - [C_AR36-\u6613] \u661f\u5ea7\u67e5\u8a62

@EditTime : 2024-01-29 14:38

"},{"location":"fundamental/python/sets/","title":"Sets","text":""},{"location":"fundamental/python/sets/#introduction","title":"Introduction","text":"

\u5982\u679c\u7d66\u4f60\u4e00\u500b\u4e32\u5217\uff0c\u8acb\u4f60\u6aa2\u67e5\u88e1\u9762\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?\u4f60\u6703\u600e\u9ebc\u6a23\u78ba\u4fdd\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\u5462?

\u5728\u9019\u4e00\u7ae0\uff0c\u6211\u5011\u5c07\u6703\u5b78\u7fd2\u5230\u96c6\u5408(Set)\uff0c\u5b83\u6709\u4ee5\u4e0b\u7279\u6027:

  • \u7121\u5e8f: \u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u6c92\u6709\u9806\u5e8f\u7684
  • \u4e0d\u91cd\u8907: \u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684
  • \u53ef\u8b8a: \u53ef\u4ee5\u65b0\u589e\u3001\u522a\u9664\u5143\u7d20\uff0c\u4f46\u662f\u88e1\u9762\u7684\u5143\u7d20\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)
  • \u7528\u5927\u62ec\u865f{}\u4f86\u8868\u793a\uff0c\u88e1\u9762\u7684\u5143\u7d20\u7528\u9017\u865f,\u9694\u958b
"},{"location":"fundamental/python/sets/#create_a_set","title":"Create a Set","text":"

\u4f60\u53ef\u4ee5\u7528set()\u4f86\u5efa\u7acb\u4e00\u500b\u96c6\u5408\uff0c\u6216\u8005\u7528\u5927\u62ec\u865f{}\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:

a = set(\"FOREVER\")\nprint(a)\n\nb = [4, 8, 4, 3]\nc = set(b)\nprint(c)\n\nd = {(0, 1), (1, 2), (2, 3)}\nprint(d)\nprint(type(d))\n\ne = {}\nprint(type(e))\n
Output
{'R', 'F', 'O', 'E', 'V'}\n{8, 3, 4}\n{(2, 3), (1, 2), (0, 1)}\n<class 'set'>\n<class 'dict'>\n

SUHO(\uc218\ud638) _ FOREVER

\u4f60\u5f97\u6ce8\u610f\uff0c\u5efa\u7acb\u7a7a\u96c6\u5408\u7684\u6642\u5019\uff0c\u4f60\u5fc5\u9808\u7528set()\uff0c\u56e0\u70ba{}\u662f\u7528\u4f86\u5efa\u7acb\u7a7a\u5b57\u5178\u7684\u3002

\u4ee5\u53ca\u6240\u8b02\u7684\u7121\u5e8f\uff0c\u4f60\u8a66\u8457\u591a\u57f7\u884c\u5e7e\u6b21\u7a0b\u5f0f\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u6bcf\u6b21\u8f38\u51fa\u7684\u7d50\u679c\u90fd\u4e0d\u4e00\u6a23\u3002

\u4f60\u751a\u81f3\u53ef\u4ee5\u7528 Comprehension \u4f86\u5efa\u7acb\u96c6\u5408\u3002

a = {1, 2, 3}\nb = {3, 4, 5}\ndiff_b = {x for x in b if x not in a}\nprint(diff_b)\n\nc = {x ** 2 for x in range(-3, 4)}\nprint(c)\n
Output
{4, 5}\n{0, 9, 4, 1}\n
"},{"location":"fundamental/python/sets/#operations","title":"Operations","text":""},{"location":"fundamental/python/sets/#accessing_elements","title":"Accessing elements","text":"

\u56e0\u70ba\u96c6\u5408\u662f\u7121\u5e8f\u7684\uff0c\u6240\u4ee5\u4f60\u4e0d\u80fd\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5143\u7d20\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u7528in\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u53ca\u642d\u914dfor\u8ff4\u5708\u4f86\u904d\u6b77\u96c6\u5408\u3002

perfect_nums_set = {6, 28, 496, 8128, 33550336}\nprint(4843 in perfect_nums_set)\n\nfor num in perfect_nums_set:\n    print(num, end=' ')\n
Output
False\n33550336 8128 496 6 28 \n

Question

\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u96c6\u5408\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728?

\u7b54\u6848\u53ef\u80fd\u6703\u8b93\u4f60\u9a5a\u8a1d\uff0c\u4e0d\u9700\u8981 \\(n\\) \u500b\u6b65\u9a5f\uff0c\u53ea\u8981\u5e7e\u500b\u6b65\u9a5f\u5c31\u597d\uff0c\u53ef\u4ee5\u8aaa\u662f\u99ac\u4e0a\u3002

"},{"location":"fundamental/python/sets/#union","title":"Union","text":"

\u4f60\u53ef\u4ee5\u7528| \u6216\u8005 union() \u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u806f\u96c6\u3002

a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.union(b))\nc = a | b\nprint(c)\n\na |= b\nprint(a)\n
Output
{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n
"},{"location":"fundamental/python/sets/#intersection","title":"Intersection","text":"

\u4f60\u53ef\u4ee5\u7528& \u6216\u8005 intersection() \u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\u3002

a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.intersection(b))\nc = a & b\nprint(c)\n\na &= b\nprint(a)\n
Output
{5}\n{5}\n{5}\n
"},{"location":"fundamental/python/sets/#difference","title":"Difference","text":"

\u4f60\u53ef\u4ee5\u7528- \u6216\u8005 difference() \u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u5dee\u96c6\u3002

a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.difference(b))\nc = b - a\nprint(c)\n\na -= b\nprint(a)\n
Output
{1, 3}\n{2, 4, 6}\n{1, 3}\n

\u6211\u5c31\u8209\u9019\u4e09\u500b\u4f8b\u5b50\uff0c\u5176\u4ed6\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u3002

"},{"location":"fundamental/python/sets/#methods","title":"Methods","text":""},{"location":"fundamental/python/sets/#adding_elements","title":"Adding elements","text":"

\u4f60\u53ef\u4ee5\u7528 add() \u4f86\u65b0\u589e\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002

nums_set = {1, 2, 3, 4, 5}\nnums_set.add(6)\nnums_set.add(3)\nprint(nums_set)\n
Output
{1, 2, 3, 4, 5, 6}\n

\u4e5f\u53ef\u4ee5\u7528 update() \u4f86\u65b0\u589e\u591a\u500b\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002

\u5176\u5be6\u5c31\u8ddf |= \u4e00\u6a23\u3002

nums_set = {1, 2, 3, 4, 5}\nnums_set.update({6, 7})\nprint(nums_set)\n\nnums_set.update([1, 8, 9], {10, 11})\nprint(nums_set)\n
Output
{1, 2, 3, 4, 5, 6, 7}\n{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\n
"},{"location":"fundamental/python/sets/#removing_elements","title":"Removing elements","text":"

\u4f60\u53ef\u4ee5\u7528 remove() \u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u6703\u62cb\u51fa\u932f\u8aa4\u3002

a = {1, 2, 3}\na.remove(1)\nprint(a)\na.remove(1)\nprint(a)\n
Output
{2, 3}\nKeyError: 1\n

\u6216\u8005\u7528 discard() \u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4e0d\u6703\u62cb\u51fa\u932f\u8aa4\u3002

a = {1, 2, 3}\na.discard(2)\nprint(a)\na.discard(2)\nprint(a)\n
Output
{1, 3}\n{1, 3}\n

\u90a3\u600e\u9ebc\u6e05\u7a7a\u5462?\u4f60\u53ef\u4ee5\u7528 clear() \u4f86\u6e05\u7a7a\u96c6\u5408\u3002

a = {1, 2, 3}\na.clear()\nprint(len(a))\n
Output
0\n

\u9084\u6709\u66f4\u591a\u7684\u65b9\u6cd5\uff0c\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\uff0c\u4f46\u9019\u4e9b\u76ee\u524d\u61c9\u8a72\u5920\u4f60\u7528\u3002

"},{"location":"fundamental/python/sets/#practice","title":"Practice","text":"

Itsa - [C_AR20-\u6613] \u6aa2\u67e5\u6578\u503c\u662f\u5426\u6709\u91cd\u8907

Reference code

n = int(input())\nnum_set = set(input().split())\n\nif len(num_set) == n:\n    print(1)\nelse:\n    print(0)\n
\u6aa2\u67e5\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u9019\u500b\u554f\u984c\u5c31\u5f88\u9069\u5408\u7528\u96c6\u5408\u4f86\u89e3\u6c7a\uff0c\u56e0\u70ba\u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u53ea\u8981\u6aa2\u67e5\u96c6\u5408\u7684\u9577\u5ea6\u662f\u5426\u7b49\u65bc\u8f38\u5165\u4e32\u5217\u7684\u9577\u5ea6\u5c31\u597d\u3002

"},{"location":"fundamental/python/sets/#assignment","title":"Assignment","text":"

\u5728 Itsa \u4e0a\u4e0d\u592a\u597d\u627e\u984c\u76ee\uff0c\u6240\u4ee5\u4e7e\u8106\u9644\u4e0a\u89e3\u7b54\u3002

Itsa - [C_ST23-\u6613] \u76f8\u540c\u5b57\u96c6\u7684\u5b57\u4e32

Reference code

a, b = input().split(',')\na_set = set(str.lower(a))\na_set.discard(\" \")\nb_set = set(str.lower(b))\nb_set.discard(\" \")\n\nif a_set == b_set:\n    print(1)\nelse:\n    print(0)\n
\u9019\u88e1\u61c9\u8a72\u80fd\u611f\u53d7 discard() \u7684\u7528\u8655\uff0c\u984c\u76ee\u8981\u6c42\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\uff0c\u6240\u4ee5\u6211\u5011\u5148\u628a\u5b57\u4e32\u8f49\u6210\u5c0f\u5beb\uff0c\u7136\u5f8c\u518d\u628a\u7a7a\u767d\u5b57\u5143\u79fb\u9664(\u4e0d\u7ba1\u6709\u6c92\u6709)\uff0c\u6700\u5f8c\u518d\u6aa2\u67e5\u5169\u500b\u96c6\u5408\u662f\u5426\u76f8\u7b49\u3002

Itsa - [C_ST82-\u6613] \u4ea4\u96c6

Itsa - [C_ST83-\u6613] \u806f\u96c6

Itsa - [C_ST84-\u6613] \u5dee\u96c6

Itsa - [C_AR192-\u6613] \u5224\u65ad\u4efb\u610f\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\u662f\u5426\u6709\u91cd\u8907

@EditTime : 2024-02-06 23:16

"},{"location":"fundamental/python/tuples/","title":"Tuples","text":""},{"location":"fundamental/python/tuples/#introduction","title":"Introduction","text":"

\u6709\u4e86\u4e0a\u4e00\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u6211\u76f8\u4fe1\u5e8f\u5c0d(Tuple)\u5c0d\u4f60\u4f86\u8aaa\u4e0d\u6703\u56f0\u96e3\uff0c\u56e0\u70ba\u5b83\u8ddf\u4e32\u5217(List)\u5f88\u50cf\uff0c\u53ea\u662f\u5b83\u662f\u7528\u5c0f\u62ec\u865f()\u4f86\u8868\u793a\uff0c\u800c\u4e14\u5b83\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\u3002\u9019\u8868\u793a\u4f60\u4e0d\u80fd\u65b0\u589e\u3001\u522a\u9664\u6216\u4fee\u6539\u88e1\u9762\u7684\u5143\u7d20\u3002\u90a3\u65e2\u7136\u5982\u6b64\uff0c\u70ba\u4ec0\u9ebc\u9084\u8981\u7528\u5b83\u5462?

"},{"location":"fundamental/python/tuples/#create_a_tuple","title":"Create a Tuple","text":"

\u8981\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\u6709\u5f88\u591a\u7a2e\u65b9\u6cd5\uff0c\u6700\u7c21\u55ae\u7684\u5c31\u662f\u7528\u5c0f\u62ec\u865f()\u628a\u5143\u7d20\u5305\u8d77\u4f86\uff0c\u7528\u9017\u865f,\u9694\u958b\u5c31\u597d\u4e86\u3002

\u4f46\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u53ea\u6709\u4e00\u500b\u5143\u7d20\u6642\uff0c\u4f60\u5fc5\u9808\u5728\u5f8c\u9762\u52a0\u4e0a\u9017\u865f\u3002\u9019\u908a\u4f60\u81ea\u5df1\u8a66\u8a66\u770b\uff0c\u61c9\u8a72\u90fd\u80fd\u7406\u89e3\u3002

empty = ()\none = (1,)\ntwo = (1, 2)\nprint(len(empty), len(one), len(two))\nprint(type(empty))\n\nodd = 1, 3, 5\nprint(odd)\nprint(type(odd))\n
Output
0 1 2\n<class 'tuple'>\n(1, 3, 5)\n(1, 3, 5)\n<class 'tuple'>\n

\u4f60\u4e5f\u53ef\u4ee5\u7528tuple()\u4f86\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:

lst = [1, 2, 3, 4, 5]\ntup = tuple(lst)\nprint(tup)\n\ns = \"know me...\"\ns_tup = tuple(s)\nprint(s_tup)\n
Output
(1, 2, 3, 4, 5)\n('k', 'n', 'o', 'w', ' ', 'm', 'e', '.', '.', '.')\n

\u516b\u6728\u6d77\u8389 \u300eknow me...\u300f

\u9084\u8a18\u5f97 List Comprehension \u55ce?\u6211\u5011\u5c07\u4e2d\u62ec\u865f\u6539\u6210\u5c0f\u62ec\u865f\u8a66\u8a66\u770b:

gen = (x ** 2 for x in range(5))\nprint(type(gen))\ntup = tuple(gen)\nprint(tup)\n
Output
<class 'generator'>\n(0, 1, 4, 9, 16)\n

\u4f60\u6703\u767c\u73fe\uff0c\u7d50\u679c\u4e26\u975e\u662f\u4f60\u9810\u671f\u7684\u5e8f\u5c0d\uff0c\u800c\u662f\u7522\u751f\u5668(Generator)\uff0c\u4f60\u9084\u9700\u8981\u4f7f\u7528tuple()\u4f86\u8f49\u63db\u3002

\u81f3\u65bc\u70ba\u4ec0\u9ebc\u8981\u7528\u7522\u751f\u5668(Generator)\uff0c\u6211\u6703\u5728\u672a\u4f86\u7684\u7ae0\u7bc0\u8ddf\u4f60\u8aaa\u660e\uff0c\u53c8\u6316\u5751\u4e86\u3002

\u90a3\u9ebc\u52a0\u6cd5\u8ddf\u4e58\u6cd5\u5462?\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u52a0\u6cd5\u4f86\u5408\u4f75\u5169\u500b\u5e8f\u5c0d\uff0c\u7528\u4e58\u6cd5\u4f86\u8907\u88fd\u5e8f\u5c0d\u3002

a = (1, 2, 3)\nb = 4, 5, 6\nc = a + b\nprint(c)\n\nd = a * 2 + b\nprint(d)\n
Output
(1, 2, 3, 4, 5, 6)\n(1, 2, 3, 1, 2, 3, 4, 5, 6)\n
"},{"location":"fundamental/python/tuples/#operations","title":"Operations","text":""},{"location":"fundamental/python/tuples/#accessing_elements","title":"Accessing elements","text":"

\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u7528\u8ca0\u7d22\u5f15\u4f86\u5f9e\u5f8c\u5b58\u53d6\u3002

t = (1, 2, 3, 4, 5)\nprint(t[0], t[-1])\n
Output
1 5\n
"},{"location":"fundamental/python/tuples/#slicing","title":"Slicing","text":"

\u540c\u6a23\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u5207\u7247\u4f86\u53d6\u5f97\u5e8f\u5c0d\u4e2d\u7684\u5b50\u5e8f\u5c0d\u3002

t = (1, 2, 3, 4, 5)\nprint(t[1:3])\nprint(t[:3])\nprint(t[3:])\nprint(t[:])\n
Output
(2, 3)\n(1, 2, 3)\n(4, 5)\n(1, 2, 3, 4, 5)\n
"},{"location":"fundamental/python/tuples/#modifying_elements","title":"Modifying elements","text":"

\u4f46\u662f\u4f60\u4e0d\u80fd\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u9019\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u662f\u6709\u6298\u8877\u7684\u65b9\u6cd5\u5566\uff0c\u5c31\u662f\u628a\u5e8f\u5c0d\u8f49\u63db\u6210\u4e32\u5217\uff0c\u518d\u8f49\u63db\u56de\u4f86\u3002

cat_tup = (\"\ud83d\ude38\", \"\ud83d\ude3a\", \"\ud83d\ude3b\", [\"\ud83d\ude3f\", \"\ud83d\ude40\"])\n\ncat_lst = list(cat_tup)\ncat_lst[1] = \"\ud83d\ude3c\"\ncat_tup = tuple(cat_lst)\nprint(cat_tup)\n\ncat_tup[3][0] = \"\ud83d\ude3e\"\nprint(cat_tup)\n\ncat_tup[1] = \"\ud83d\ude3d\"\nprint(cat_tup)\n
Output
('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3f', '\ud83d\ude40'])\n('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3e', '\ud83d\ude40'])\nTypeError: 'tuple' object does not support item assignment\n

\u4f46\u4f60\u6709\u6c92\u6709\u89ba\u5f97\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u6211\u53ef\u4ee5\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u4e32\u5217\u5462?

"},{"location":"fundamental/python/tuples/#checking_elements","title":"Checking elements","text":"

\u4f60\u53ef\u4ee5\u7528 in \u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5728\u5e8f\u5c0d\u4e2d\uff0c\u9019\u8ddf\u4e32\u5217\u662f\u4e00\u6a23\u7684\u3002

fib = (0, 1, 1, 2, 3, 5, 8)\nprint(0 not in fib)\nprint(5 in fib)\n
Output
False\nTrue\n

Question

\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u5e8f\u5c0d\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u5e8f\u5c0d\u4e2d\uff0c\u6700\u597d\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?

\u53c8\u662f\u719f\u6089\u7684\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u7684\u3002

"},{"location":"fundamental/python/tuples/#methods","title":"Methods","text":"

\u56e0\u70ba\u5e8f\u5c0d\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u6240\u4ee5\u53ea\u6709\u5169\u500b\u65b9\u6cd5\uff0c\u4e00\u500b\u662fcount()\uff0c\u4e00\u500b\u662findex()\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u8a66\u8a66\u770b\u3002

"},{"location":"fundamental/python/tuples/#count","title":"count","text":"

count() \u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u7576\u7136 List \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\uff0c\u4f46\u524d\u9762\u6211\u4e26\u6c92\u6709\u63d0\uff0c\u56e0\u70ba\u53ef\u4ee5\u653e\u5728\u9019\u88e1\u6c34\u5167\u5bb9(X

t = ((1, 2), (3, 4), 6, 6, [7, 8])\nprint(t.count(1))\nprint(t.count([7, 8]))\nprint(t.count(6))\n
Output
0\n1\n2\n
"},{"location":"fundamental/python/tuples/#index","title":"index","text":"

index() \u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u7684\u7d22\u5f15\uff0c\u4e00\u6a23\u7684\uff0cList \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\u3002

t = (\"Love Me Again\", \"John Newman\", 2013)\nprint(t.index(\"John Newman\"))\nprint(t.index(2014))\n
Output
1\nValueError: tuple.index(x): x not in tuple\n

John Newman - Love Me Again

"},{"location":"fundamental/python/tuples/#tuple_vs_list","title":"Tuple vs List","text":"
  • List
    • \u7528\u4e2d\u62ec\u865f[]\u8868\u793a
    • \u53ef\u8b8a\u7684(mutable)
    • \u4e0d\u53ef\u96dc\u6e4a(unhashable)
    • \u6548\u80fd\u8f03\u5dee
    • \u9069\u7528\u65bc\u983b\u7e41\u7684\u589e\u522a\u6539
  • Tuple
    • \u7528\u5c0f\u62ec\u865f()\u8868\u793a
    • \u4e0d\u53ef\u8b8a\u7684(immutable)
    • \u53ef\u96dc\u6e4a(hashable)
    • \u6548\u80fd\u8f03\u597d
    • \u9069\u7528\u65bc\u4e0d\u9700\u8981\u8b8a\u52d5\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\u5e38\u6578\u3001\u5ea7\u6a19

\u95dc\u65bc\u300c\u53ef\u96dc\u6e4a\u7684(hashable)\u300d\uff0c\u6211\u6703\u5728\u5b57\u5178(Dict)\u9019\u7ae0\u4e2d\u8ddf\u4f60\u8aaa\u660e\u3002

"},{"location":"fundamental/python/tuples/#practice","title":"Practice","text":"

Itsa - [C_AR04-\u6613] \u908a\u7de3\u5075\u6e2c

Reference code

N = int(input())\n\ndirs = ((0, 1), (0, -1), (1, 0), (-1, 0))\n\nfor k in range(N):\n    n, m = map(int, input().split())\n    pic = []\n    for _ in range(n):\n        pic.append(input().split())\n\n    for i in range(n):\n        for j in range(m):\n            if pic[i][j] == '0':\n                print('_ ', end='')\n            else:\n                is_edge = False\n                for d in dirs:\n                    x, y = i + d[0], j + d[1]\n                    if 0 <= x < n and 0 <= y < m and pic[x][y] == '0':\n                        is_edge = True\n                        break\n                if is_edge:\n                    print('0 ', end='')\n                else:\n                    print('_ ', end='')\n        print()\n\n    if k != N - 1:\n        print()\n
\u65b9\u5411 dirs \u5c31\u5f88\u9069\u7528\u5e8f\u5c0d\u4f86\u8868\u793a\uff0c\u56e0\u70ba\u5b83\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u6703\u8b8a\u52d5\u3002

\u672a\u4f86\u5728\u5b78\u5716\u5f62\u8d70\u8a2a\u7684\u6642\u5019\uff0c\u4f60\u6703\u4e00\u76f4\u770b\u5230\u9019\u7a2e\u5beb\u6cd5\u3002

\u5c0d\u4e86\uff0c \u8ff4\u5708\u8b8a\u6578 _ \u88ab\u7a31\u70ba\u6368\u68c4\u8b8a\u6578\uff0c\u8868\u793a\u4e0d\u9700\u8981\u7528\u5230\u9019\u500b\u8b8a\u6578\uff0c\u53ea\u662f\u70ba\u4e86\u914d\u5408\u8ff4\u5708\u8a9e\u6cd5\u800c\u5df2\u3002

"},{"location":"fundamental/python/tuples/#assignment","title":"Assignment","text":"

Itsa - [C_AR119-\u6613] \u5730\u96f7\u5371\u96aa\u6307\u6578\u8868

Itsa - [C_AR139-\u6613] \u9ec3\u91d1\u63a2\u6e2c

Itsa - [C-AR140-\u6613] \u6c42\u6700\u5927\u7ce7\u98df\u7522\u91cf

@EditTime : 2024-02-04 21:15

"},{"location":"fundamental/python/variable_and_input/","title":"Variable and Input","text":""},{"location":"fundamental/python/variable_and_input/#variable","title":"Variable","text":"

\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f86\u770b\u600e\u9ebc\u4f7f\u7528\u8b8a\u6578\uff0c\u4ee5\u53ca\u8f38\u5165\u81ea\u5df1\u60f3\u8981\u7684\u6771\u897f\u3002

x = 4843\nprint(x)\n\nx = \"Memory Reboot\"\nprint(x)\n\nx = '4843'\nprint(x)\n

\u6216\u8a31\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u795e\u5947\uff0c\u70ba\u4ec0\u9ebc x \u53ef\u4ee5\u8b8a\u6210\u6578\u5b57\uff0c\u53c8\u53ef\u4ee5\u8b8a\u6210\u5b57\u4e32\uff0c\u9084\u6709\u70ba\u4ec0\u9ebc\u7b2c\u4e00\u500b\u8207\u7b2c\u4e09\u500b\u7684\u8f38\u51fa\u6703\u662f\u76f8\u540c\u7684\u3002

\u518d\u4f86\u7528\u4e00\u4e9b old-school \u7684\u6bd4\u55bb\uff0c\u4f60\u53ef\u4ee5\u5c07 x \u60f3\u50cf\u6210\u4e00\u500b\u7bb1\u5b50\uff0c\u800c\u7b49\u865f\u53f3\u908a\u7684\u300c\u503c\u300d\u5c31\u662f\u7bb1\u5b50\u88e1\u88dd\u7684\u6771\u897f\u3002\u65e2\u7136\u7bb1\u5b50\u88e1\u9762\u7684\u6771\u897f\u53ef\u4ee5\u8b8a\uff0c\u90a3\u6211\u662f\u4e0d\u662f\u53ef\u4ee5\u8aaa x \u662f\u4e00\u500b\u8b8a\u6578\u5462?

\u4f46\u8acb\u4f60\u5148\u5fd8\u6389\u9019\u500b\u6bd4\u55bb\uff0c\u56e0\u70ba\u9019\u500b\u6bd4\u55bb\u4e26\u4e0d\u5b8c\u7f8e\uff0c\u4f46\u6211\u60f3\u4f60\u61c9\u8a72\u80fd\u5920\u7406\u89e3\uff0cx \u662f\u4e00\u500b\u8b8a\u6578\uff0c\u800c = \u662f\u6307\u6d3e\u904b\u7b97\u5b50(Assignment Operator)\uff0c\u4ed6\u6703\u5c07\u7b49\u865f\u53f3\u908a\u7684\u6771\u897f\u6307\u6d3e\u7d66\u7b49\u865f\u5de6\u908a\u7684\u8b8a\u6578\uff0c\u6216\u8005\u8aaa x \u6703\u6307\u5411\u7b49\u865f\u53f3\u908a\u7684\u7269\u4ef6(Object)\u3002

Quote

\u5728Python\u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u7269\u4ef6\u3002

\u8acb\u89c0\u770b\u4e0b\u9762\u7684\u52d5\u756b\u3002

V\u00d8J, Narvent - Memory Reboot (4K Music Video)

\u518d\u4f86\u4ecb\u7d39\u5e7e\u7a2e\u4e0d\u540c\u7684\u8b8a\u6578\u985e\u578b\u3002

a = 114514\nprint(type(a))\nb = 1919810.0\nprint(type(b))\nc = 10e3\nprint(type(c))\nd = \"cheung4843\"\nprint(type(d))\ne = True\nprint(type(e))\nf = False\nprint(type(f))\nh = None\nprint(type(h))\n
type \u544a\u8a34\u4f60\u62ec\u865f\u4e2d\u7684\u6771\u897f\u662f\u4ec0\u9ebc\u985e\u5225(Class)\u3002

b \u8207 c \u90fd\u662f\u6d6e\u9ede\u6578(Float)\uff0c\u4e5f\u5c31\u662f\u5c0f\u6578\u9ede\u7684\u6578\u5b57\uff0c\u800c d \u5247\u662f\u5b57\u4e32\uff0ce \u8207 f \u5247\u662f\u5e03\u6797(Boolean)\uff0c\u800c h \u5247\u662f\u7a7a\u503c\u3002

Note

  1. type(x) \u56de\u50b3 x \u7684\u985e\u5225\u3002
  2. int \u6574\u6578\u3002
  3. float \u6d6e\u9ede\u6578\u3002
  4. str \u5b57\u4e32\u3002
  5. bool \u5e03\u6797\u3002
  6. None \u7a7a\u503c\u3002

Question

  1. print(type(3 + 4.0)) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. print(type(3 + True)) \u6703\u5370\u51fa\u4ec0\u9ebc?

@EditTime : 2024-01-27 16:44

"},{"location":"fundamental/python/variable_and_input/#input","title":"Input","text":"

\u4f46\u662f\u5982\u679c\u6bcf\u6b21\u60f3\u8981\u4fee\u6539 x \u88e1\u9762\u7684\u6771\u897f\uff0c\u96e3\u9053\u90fd\u8981\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4fee\u6539\u55ce?\u80fd\u4e0d\u80fd\u6211\u81ea\u5df1\u4f86\u8f38\u5165\u5462?

x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = int(x) + int(y)\nprint(\"The sum is: \", z)\n

\u5594\u5e79\uff0c\u600e\u9ebc\u4e00\u4e0b\u5b50\u591a\u51fa\u90a3\u9ebc\u591a\u6771\u897f\uff0c\u5225\u614c\uff0c\u6211\u4f86\u89e3\u91cb\uff0c\u4f46\u8acb\u4f60\u5148\u56de\u60f3\u5728\u524d\u4e00\u7bc0\u4e2d\u5b78\u904e\u7684\u6771\u897f\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe input, int, \u90fd\u662f\u51fd\u5f0f\u3002\u800c input \u62ec\u865f\u4e2d\u7684\u5b57\u4e32\u6703\u986f\u793a\u5728\u63a7\u5236\u53f0\u4e2d\u63d0\u793a\u4f60\u8981\u8f38\u5165\u4ec0\u9ebc\uff0cint \u5247\u662f\u628a\u62ec\u865f\u4e2d\u7684\u6771\u897f\u7684\u985e\u5225\u8f49\u63db\u6210\u6574\u6578\u3002

\u90a3\u70ba\u4ec0\u9ebc\u5370\u51fatype(x) \u5f97\u5230 <class 'str'> \u5462?\u4ee3\u8868 x \u662f\u4e00\u500b\u5b57\u4e32\uff0c\u9019\u662f\u56e0\u70ba input \u7e3d\u662f\u5c07\u4f60\u8f38\u5165\u9032\u4f86\u7684\u6771\u897f\u7576\u6210\u5b57\u4e32\uff0c\u4f46\u6211\u60f3\u8981\u8b93 z = x + y \u9019\u500b\u6578\u5b78\u5f0f\u5b50\u6210\u7acb\uff0c\u6240\u4ee5\u9700\u8981\u7528 int \u4f86\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\u518d\u9032\u884c\u904b\u7b97\u3002

\u90a3\u4e0b\u9762\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u662f\u4ec0\u9ebc\u5462?

x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = x + y\nprint(\"The sum is: \", z)\n

\u6c92\u932f\uff0c\u5b57\u4e32\u7684\u76f8\u52a0\uff0c\u5c31\u662f\u76f8\u9023\u3002\u90a3\u4f60\u8981\u4e0d\u8981\u8a66\u8a66\u770b\u76f8\u6e1b?

\u63a5\u4e0b\u4f86\u6211\u5011\u4f86\u505a\u500b\u6709\u8da3\u7684\u5be6\u9a57\uff0c\u9806\u4fbf\u8a8d\u8b58\u4e00\u4e0b f-string

a = 3.5\nb = int(a)\nprint(f'The value of b is {b}, and its type is {type(b)}')\nc = float(b)\nprint(f'The value of c is {c}, and its type is {type(c)}')\nprint(b == c)\n

\u90a3 f-string \u662f\u4ec0\u9ebc\u6771\u897f\u5462?\u4ed6\u662f\u4e00\u7a2e\u5b57\u4e32\u683c\u5f0f\u5316(String Formatting)\u7684\u65b9\u6cd5\uff0c\u4ed6\u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u5b57\u4e32\uff0c\u4e26\u5c07\u5b57\u4e32\u4e2d\u7684 {} \u66ff\u63db\u6210\u62ec\u865f\u5167\u7684\u6771\u897f\u3002

\u800c\u57f7\u884c\u5b8c\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u4f60\u6703\u767c\u73fe b \u8207 c \u7684\u985e\u5225\u4e0d\u540c\uff0c\u4f46\u4ed6\u5011\u7684\u503c\u537b\u76f8\u540c\uff0c\u9019\u662f\u56e0\u70ba int() \u8207 float() \u90fd\u662f\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u6574\u6578\u8207\u6d6e\u9ede\u6578\uff0c\u800c int() \u6703\u5c07\u6d6e\u9ede\u6578\u7684\u5c0f\u6578\u9ede\u6368\u53bb\uff0c\u800c float() \u5247\u6703\u5c07\u6574\u6578\u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002

\u90a3\u9ebc b == c \u662f\u4ec0\u9ebc\u610f\u601d\u5462?\u5176\u4e2d == \u662f\u6bd4\u8f03\u904b\u7b97\u5b50(Comparison Operator)\uff0c\u4ed6\u6703\u6bd4\u8f03\u7b49\u865f\u5de6\u53f3\u5169\u908a\u7684\u6771\u897f\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5247\u56de\u50b3 True\uff0c\u5426\u5247\u56de\u50b3 False\u3002

"},{"location":"fundamental/python/variable_and_input/#multiple_input","title":"Multiple Input","text":"

\u90a3\u5982\u679c\u6211\u4eca\u5929\u60f3\u8981\u4e00\u6b21\u8f38\u5165\u597d\u5e7e\u500b\u5b57\u4e32\uff0c\u6bcf\u4e00\u500b\u5b57\u4e32\u4ee5\u7a7a\u683c\u4f86\u9694\u958b\u5462? \u4f46\u5728\u9019\u4e4b\u524d\uff0c\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5c0f\u7a0b\u5f0f :

a, b = \"Hello World\".split()\nprint(a)\nprint(b)\n

\u6211\u77e5\u9053\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u5b57\u4e32\u5f8c\u9762\u63a5\u4e86\u4e00\u500b .split() \u5462?\u5728\u9019\u88e1\u4ed6\u5f88\u50cf\u662f\u51fd\u5f0f\uff0c\u4f46\u53c8\u4e0d\u662f\u51fd\u5f0f\uff0c\u90a3\u4ed6\u53eb\u4ec0\u9ebc\u5462?\u4ed6\u88ab\u7a31\u70ba \"Hello World\" \u9019\u500b\u5b57\u4e32\u7684\u65b9\u6cd5(Method)\uff0c\u4f46\u672a\u4f86\u4f60\u53ef\u80fd\u9084\u6703\u807d\u5230\u985e\u5225\u65b9\u6cd5(Class Method)\uff0c\u4ee5\u53ca\u975c\u614b\u65b9\u6cd5(Static Method) \u7b49\u540d\u8a5e\uff0c\u6211\u6015\u4f60\u6703\u641e\u6df7\uff0c\u6240\u4ee5\u4f60\u5c31\u5148\u8a8d\u9017\u9ede\u5f8c\u9762\u7684\u662f\u300c\u65b9\u6cd5\u300d\u5c31\u597d\u4e86\u3002

\u800c .split() \u6703\u628a\u5b57\u4e32\u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\u4e26\u56de\u50b3(\u90a3\u4f60\u60f3\u60f3\u62ec\u865f\u5167\u4ec0\u9ebc\u90fd\u6c92\u653e\uff0c\u9810\u8a2d\u6703\u662f\u4ec0\u9ebc?)\uff0c\u800c\u770b\u770b\u7b49\u865f\u5de6\u908a\uff0c\u6211\u7528 a, b \u53bb\u63a5\u8457\uff0c\u9019\u7a31\u70ba\u958b\u7bb1(Unpacking)

\u90a3\u73fe\u5728\u4f60\u61c9\u8a72\u80fd\u770b\u61c2\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u4e86\uff0c\u56e0\u70ba input() \u4e5f\u6703\u56de\u50b3\u4e00\u500b\u5b57\u4e32\uff0c\u56e0\u6b64\u4ed6\u4e5f\u80fd\u5920\u4f7f\u7528 .split()

a, b = input().split()\nprint(a)\nprint(b)\n

Note

  1. input() \u63a5\u53d7\u8f38\u5165\uff0c\u56de\u50b3\u4e00\u500b\u5b57\u4e32\u3002
  2. int(x) \u5c07 x \u8f49\u63db\u6210\u6574\u6578\u3002
  3. float(x) \u5c07 x \u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002
  4. str.split() \u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\uff0c\u4e26\u56de\u50b3\u3002

@EditTime : 2024-01-25 22:13

Question

  1. print(int(input()) + int(input())) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. 124.spilt(\"1\") \u662f\u5408\u6cd5\u7684\u55ce?
  3. bool(0) \u8207 bool(1) \u6703\u56de\u50b3\u4ec0\u9ebc?
  4. \u770b\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u70ba\u4ec0\u9ebc\u5b83\u5011\u7684\u529f\u80fd\u76f8\u540c\uff0ca \u8207 b \u6210\u529f\u4e92\u63db\u4e86\u5462?
a, b = 4, 5\nprint(a, b)\na, b = b, a\nprint(a, b)\n
a, b = 4, 5\nprint(a, b)\ntmp = a\na = b\nb = tmp\nprint(a, b)\n

@EditTime : 2024-01-25 22:17

"},{"location":"fundamental/python/variable_and_input/#practice","title":"Practice","text":"

\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:

Info

  1. \u5982\u4f55\u4f7f\u7528 print() \u8207 input()\u3002
  2. \u77e5\u9053\u8b8a\u6578\u7684\u6982\u5ff5\u3002
  3. \u77e5\u9053 type(x) \u8207 int(x), float(x) \u7684\u7528\u9014\u3002
  4. \u77e5\u9053 str.split() \u7684\u7528\u9014\u3002
  5. \u77e5\u9053\u5982\u4f55\u4f7f\u7528 f-string\u3002

\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002

ZeroJudge - a001. \u54c8\u56c9

Reference code
word = input()\nprint(f'hello, {word}')\n

@EditTime : 2024-01-27 17:02

"},{"location":"useful_packages/","title":"Useful Packages","text":"

\u5728\u9019\u88e1\uff0c\u5c07\u6703\u5411\u60a8\u4ecb\u7d39\u4e00\u4e9b\u975e\u5e38\u6709\u7528\u7684\u5957\u4ef6\u3002

"},{"location":"useful_packages/tkinter/","title":"Tkinter","text":""},{"location":"blog/archive/2024/","title":"2024","text":""},{"location":"blog/category/blog/","title":"Blog","text":""}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Hello ZestAlgo Book!","text":""},{"location":"#welcome_to_zestalgo","title":"Welcome to ZestAlgo","text":"

\u672c\u66f8\u7684\u6b63\u78ba\u6253\u958b\u65b9\u5f0f\u662f\u6697\u8272\u6a21\u5f0f\u3002

print(\"Hello ZestAlgo Book!\")\nprint(\"Welcome to LMcps!\")\n
"},{"location":"#why_does_it_start","title":"Why does it start?","text":"

\u767c\u73fe\u4e86\u76ee\u524d\u8cc7\u8a0a\u6559\u80b2\u7684\u7a98\u56f0\uff0c\u8af8\u5982\u7db2\u8def\u4e0a\u7684\u8cc7\u6e90\u96d6\u591a\uff0c\u4f46\u4e0d\u6613\u5165\u9580\uff0c\u65bc\u662f\u6c7a\u5b9a\u5beb\u4e00\u672c\u66f8\uff0c\u4ee5\u5927\u91cf\u7684\u5716\u7247\u53ca\u7c21\u55ae\u7684\u6587\u5b57\uff0c\u8b93\u65b0\u624b\u80fd\u5920\u5feb\u901f\u4e14\u53cb\u5584\u5730\u5165\u9580\uff0c\u4e26\u4e14\u80fd\u5920\u5728\u77ed\u6642\u9593\u5167\uff0c\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\u8207\u81ea\u4fe1\u3002

"},{"location":"#content","title":"Content","text":"

\u9019\u672c\u66f8\u8457\u91cd\u65bc\u61c9\u7528\u9762\uff0c\u4e26\u4e14\u5be6\u4f5c\u70ba\u4e3b\uff0c\u4e5f\u4e26\u975e\u5e36\u4f60\u6253\u7af6\u7a0b\uff0c\u800c\u662f\u5e36\u4f60\u4e86\u89e3\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u3002

"},{"location":"#wait_to_be_done","title":"Wait to be done","text":"
  1. \u5fc3\u5f97\u5206\u4eab\u5c08\u5340
  2. \u5c08\u696d\u7528\u8a9e
"},{"location":"#contributors","title":"Contributors","text":""},{"location":"algorithm/","title":"Algorithm","text":"

\u5728\u9019\u88e1\uff0c\u6211\u6703\u9010\u6b65\u5e36\u4f60\u8a8d\u8b58\u6f14\u7b97\u6cd5\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e00\u4e9b\u57fa\u672c\u7684\u6f14\u7b97\u6cd5\u7bc4\u4f8b\u3002

"},{"location":"algorithm/what_is_algo/","title":"What is an Algorithm?","text":""},{"location":"algorithm/what_is_algo/#introduction","title":"Introduction","text":"

\u606d\u559c\u4f60\uff0c\u4f60\u8e0f\u5165\u4e86\u6f14\u7b97\u6cd5\u7684\u5927\u9580\u3002

\u5982\u679c\u4f60\u6709\u770b\u904e\u4e00\u4e9b\u6f14\u7b97\u6cd5\u7684\u66f8\u6216\u8005\u662f\u4e00\u4e9b\u7db2\u7ad9\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u6f14\u7b97\u6cd5\u7684\u5b9a\u7fa9\u6709\u9ede\u62bd\u8c61\u3002\u4f46\u5c31\u6211\u7684\u7406\u89e3\uff0c\u5c31\u662f\u9019\u56db\u9ede:

  1. \u6f14\u7b97\u6cd5\u7531\u6709\u9650\u7684\u6307\u4ee4\u7d44\u6210
  2. \u6f14\u7b97\u6cd5\u662f\u70ba\u4e86\u89e3\u6c7a\u7279\u5b9a\u554f\u984c
  3. \u6f14\u7b97\u6cd5\u662f\u4e00\u6b65\u4e00\u6b65\u57f7\u884c\u7684
  4. \u6f14\u7b97\u6cd5\u542b\u6709\u8f38\u5165\u8207\u8f38\u51fa

\u9019\u6a23\u5c31\u597d\u4e86\uff0c\u4e26\u4e0d\u7528\u5c0d\u5b9a\u7fa9\u592a\u904e\u65bc\u56b4\u683c\uff0c\u6703\u5beb\u66f4\u91cd\u8981\uff0c\u6f14\u7b97\u6cd5\u5c31\u662f\u4e00\u500b\u89e3\u6c7a\u554f\u984c\u7684\u65b9\u6cd5\u3002

\u4f60\u53ef\u80fd\u6709\u807d\u904e\u6d41\u7a0b\u5716\uff0c\u9019\u662f\u4e00\u500b\u5f88\u597d\u7528\u4f86\u8996\u89ba\u5316\u6f14\u7b97\u6cd5\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u4e4b\u524d\u5728 Repetiton Structures - While loop \u6709\u63d0\u5230\u7684 Collatz Conjecture\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u6d41\u7a0b\u5716\u4f86\u8868\u793a:

\\[ f(n)=\\begin{cases}\\frac{n}{2}, & \\text{if } n \\text{ is even} \\\\ 3n+1, & \\text{if } n \\text{ is odd}\\end{cases} \\]
graph LR\nA[Input a number N] --> B{Is N == 1?};\nB -- Yes --> C[End];\nB -- No --> D{Is N odd?};\nD -- Yes --> E[N = 3N + 1];\nD -- No --> F[N = N // 2];\nE --> B;\nF --> B;
"},{"location":"algorithm/what_is_algo/#complexity","title":"Complexity","text":"

\u5728\u4e4b\u524d\u7684\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u9047\u5230\u4e86\u4e00\u4e9b\u6709\u591a\u7a2e\u89e3\u6cd5\u7684\u554f\u984c\uff0c\u90a3\u6211\u5011\u8981\u5982\u4f55\u8a55\u4f30\u9019\u4e9b\u6f14\u7b97\u6cd5\u7684\u597d\u58de\u5462?\u4ee5\u53ca\u8981\u4ee5\u4ec0\u9ebc\u6a19\u6e96(criteria)\u6216\u57fa\u6e96(bechmark)\u4f86\u8a55\u4f30\u5462?

\u5f88\u76f4\u89c0\u7684\uff0c\u6211\u5011\u6703\u60f3\u5230\u4ee5\u7a0b\u5f0f\u7684\u57f7\u884c\u6642\u9593\u4f86\u8a55\u4f30\uff0c\u4f46\u9019\u6a23\u7684\u65b9\u6cd5\u6709\u4e00\u500b\u554f\u984c\uff0c\u5c31\u662f\u7a0b\u5f0f\u7684\u57f7\u884c\u6642\u9593\u6703\u53d7\u5230\u5f88\u591a\u56e0\u7d20\u7684\u5f71\u97ff\uff0c\u4f8b\u5982: \u786c\u9ad4\u3001\u4f5c\u696d\u7cfb\u7d71\u3001\u7a0b\u5f0f\u8a9e\u8a00\u3001\u7de8\u8b6f\u5668\u3001\u8f38\u5165\u8cc7\u6599\u7b49\u7b49\u3002\u6240\u4ee5\u6211\u5011\u9700\u8981\u4e00\u500b\u66f4\u7a69\u5b9a\u7684\u65b9\u6cd5\u4f86\u8a55\u4f30\u6f14\u7b97\u6cd5\u7684\u597d\u58de\u3002

\u6216\u8a31\u6211\u5011\u5728 C++ \u4e0a\u5beb\u4e86\u4e00\u500b\u7cdf\u7cd5\u7684\u6f14\u7b97\u6cd5\uff0c\u800c\u5728 Python \u4e0a\u5beb\u4e86\u4e00\u500b\u300c\u597d\u7684\u300d\u6f14\u7b97\u6cd5\uff0c\u4f46\u5f8c\u8005\u7684\u57f7\u884c\u6642\u9593\u537b\u6bd4\u524d\u8005\u9577\uff0c\u96d6\u7136\u4f60 Python \u5beb\u7684\u901f\u5ea6\u6bd4 C++ \u5feb(XD)

\u6211\u5011\u4e0d\u59a8\u4ee5\u6f14\u7b97\u6cd5\u7684\u6b65\u9a5f\u6578\u4f86\u8a55\u4f30\uff0c\u4e26\u4e14\u89c0\u5bdf\u8f38\u5165\u8cc7\u6599\u8207\u6b65\u9a5f\u6578\u7684\u95dc\u4fc2\u3002

\u8209\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u505a\u70ba\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f print_hello\uff0c\u4e26\u63a5\u53d7\u4e00\u500b\u53c3\u6578 n\uff0c\u5370\u51fa hello, Algorithm! \u82e5\u5e72\u6b21\u3002

def print_hello(n):\n    for i in range(n):\n        for j in range(n):\n            print('hello, Algorithm!')\n\n    for i in range(2 * n):\n        print('hello, Algorithm!')\n\n    print('hello, Algorithm!')\n    print('hello, Algorithm!')\n    print('hello, Algorithm!')\n\n\nprint_hello(2)\n
Output
hello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\nhello, Algorithm!\n

\u5b9a\u7fa9 \\(f(n)=n^{2}+2n+3\\) \u4f86\u4ee3\u8868\u8f38\u5165 \\(n\\)\uff0c\u6703\u5370\u51fa hello, Algorithm! \u7684\u6b21\u6578\u4e4b\u9593\u7684\u95dc\u4fc2\u3002

\u800c \\(f(2)=11\\)\uff0c\u4e5f\u5c31\u662f\u8aaa\u7576 \\(n=2\\) \u6642\uff0c\u6211\u5011\u7684\u6f14\u7b97\u6cd5\u6703\u5370\u51fa hello, Algorithm! \\(11\\) \u6b21\u3002

\u7a31 \\(f(n)\\) \u70ba\u9019\u500b\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6(Time Complexity)\uff0c\u800c\u65e2\u7136\u6709\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u90a3\u7576\u7136\u4e5f\u6709\u7a7a\u9593\u8907\u96dc\u5ea6(Space Complexity)\uff0c\u9019\u662f\u6307\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u6240\u9700\u8981\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u4f8b\u5982: \u9663\u5217\u7684\u5927\u5c0f\u3001\u8b8a\u6578\u7684\u6578\u91cf\u7b49\u7b49\u3002

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u53cd\u8f49\u4e00\u500b\u4e32\u5217\uff0c\u63d0\u4f9b\u4e86\u5169\u7a2e\u89e3\u6cd5:

def reverse_list(lst):\n    reversed_lst = []\n    for i in range(len(lst) - 1, -1, -1):\n        reversed_lst.append(lst[i])\n\n    return reversed_lst\n\n\ndef reverse_list2(lst):\n    n = len(lst)\n    for i in range(n // 2):\n        tmp = lst[i]\n        lst[i] = lst[n - i - 1]\n        lst[n - i - 1] = tmp\n    return lst\n\n\nprint(reverse_list([1, 2, 3, 4, 5]))\nprint(reverse_list2([1, 2, 3, 4, 5]))\n
Output
[5, 4, 3, 2, 1]\n[5, 4, 3, 2, 1]\n

\u5b9a\u7fa9 \\(g(n)\\) \u4f86\u4ee3\u8868\u8f38\u5165 \\(n\\)\uff0c\u6f14\u7b97\u6cd5\u6240\u9700\u8981\u7684\u984d\u5916\u7a7a\u9593\uff0c\u4f8b\u5982 \\(g(n)=n\\)\uff0c\u4e5f\u5c31\u662f\u8aaa\u7576 \\(n=5\\) \u6642\uff0c\u6211\u5011\u7684\u6f14\u7b97\u6cd5\u6703\u9700\u8981 \\(5\\) \u500b\u7a7a\u9593\uff0c\u6216\u8005\u8aaa\u662f\u9700\u8981\u5927\u5c0f\u70ba \\(5\\) \u7684\u4e32\u5217\u3002

\u8a66\u8457\u6bd4\u8f03\u4e0a\u8ff0\u5169\u500b\u53cd\u8f49\u4e32\u5217\u7684\u6f14\u7b97\u6cd5:

Algorithm \\(f(n)\\) \\(g(n)\\) reverse_list \\(n\\) \\(n\\) reverse_list2 \\(n/2\\) \\(1\\)

reverse_list \u6703\u5efa\u7acb\u4e00\u500b\u5927\u5c0f\u70ba \\(n\\) \u7684\u4e32\u5217\uff0c\u5132\u5b58\u53cd\u8f49\u5f8c\u7684\u4e32\u5217\uff0c\u56e0\u6b64 \\(f(n)\\) \u8207 \\(g(n)\\) \u90fd\u662f \\(n\\)\uff0c

\u800c reverse_list2 \u5247\u662f\u76f4\u63a5\u5728\u539f\u5730(in-place)\u9032\u884c\u53cd\u8f49\uff0c\u53ea\u4f7f\u7528 tmp \u9019\u500b\u8b8a\u6578\uff0c\u6240\u4ee5 \\(g(n)\\) \u662f \\(1\\)\uff0c\u53c8\u56e0\u70ba\u53ea\u9700\u8981\u53cd\u8f49\u4e00\u534a\u7684\u4e32\u5217\uff0c\u6240\u4ee5 \\(f(n)\\) \u662f \\(\\frac{n}{2}\\)\u3002

\u9019\u6a23\u7684\u6bd4\u8f03\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u9078\u64c7\u9069\u5408\u7684\u6f14\u7b97\u6cd5\uff0c\u4e26\u4e14\u8a55\u4f30\u6f14\u7b97\u6cd5\u7684\u597d\u58de\uff0c\u4f46\u662f\u9019\u6a23\u9084\u7a0d\u5acc\u9ebb\u7169\uff0c\u6709\u6c92\u6709\u66f4\u6e05\u695a\u4e14\u7c21\u55ae\u7684\u65b9\u6cd5\u5462?

\u70ba\u63a5\u4e0b\u4f86\u7684\u5167\u5bb9\u5148\u6253\u500b\u9810\u9632\u91dd\uff0c\u56e0\u70ba\u6578\u5b78\u7b26\u865f\u6709\u9ede\u591a\uff0c\u5982\u679c\u771f\u7684\u770b\u4e0d\u61c2\uff0c\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230 Calculating Big O\uff0c\u4f46\u9084\u662f\u8acb\u4f60\u8a66\u8457\u591a\u770b\u5e7e\u6b21\uff0c\u6211\u76f8\u4fe1\u4f60\u53ef\u4ee5\u3002

"},{"location":"algorithm/what_is_algo/#big_o_notation","title":"Big O Notation","text":""},{"location":"algorithm/what_is_algo/#definition","title":"Definition","text":"

\u9019\u6642\u5019\u5c31\u8981\u4ecb\u7d39\u5927 O \u7b26\u865f(Big O Notation)\u4e86\uff0c\u5b83\u662f\u6f38\u8fd1\u7b26\u865f(Asymptotic Notation)\u7684\u4e00\u7a2e\uff0c\u7528\u4f86\u63cf\u8ff0\u4e00\u500b\u6f14\u7b97\u6cd5\u5728\u300c\u6700\u58de\u60c5\u6cc1\u300d\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u6240\u4ee5\u8aaa\u5b83\u662f\u4e00\u7a2e\u4e0a\u754c(Upper Bound)\uff0c\u4e5f\u901a\u5e38\u6307\u8f38\u5165\u8cc7\u6599\u7684\u898f\u6a21\u8da8\u8fd1\u7121\u7aae\u5927\u6642\u7684\u884c\u70ba\uff0c\u4e5f\u5c31\u662f \\(n\\) \u5f88\u5927\u7684\u6642\u5019\u3002

\u4f8b\u5982: \u7576 \\(n \\to \\infty\\)\uff0c\\(f(n)=n^{2}+2n+3\\) \u7684\u884c\u70ba\u5c31\u662f \\(\\mathcal{O}(n^{2})\\)\uff0c\u56e0\u70ba\u7576 \\(n\\) \u5f88\u5927\u7684\u6642\u5019\uff0c\\(n^{2}\\) \u6703\u9060\u9060\u5927\u65bc \\(2n+3\\)\uff0c\u6240\u4ee5\u53ef\u4ee5\u5ffd\u7565\u6389 \\(2n+3\\)\u3002

\u6211\u5011\u4f86\u770b\u8f03\u70ba\u6b63\u5f0f\u7684\u5b9a\u7fa9:

Big O

\\[f(n) \\text{ and } g(n) \\text{ are two functions.}\\] \\[\\text{The function }f(n)=\\mathcal{O}(g(n)) \\text{ iff } \\exists c,n_0 \\text{ s.t } 0\\le f(n) \\le c*g(n),\\forall n \\ge n_0\\]

\u5148\u5225\u6025\u8457\u95dc\u6389\u5566\uff0c\u5148\u7528\u4e2d\u6587\u89e3\u91cb\u4e00\u4e0b:

\\(f(n)\\) \u548c \\(g(n)\\) \u662f\u5169\u500b\u51fd\u6578\uff0c\u82e5\u4e14\u552f\u82e5\u5b58\u5728\u5e38\u6578 \\(c\\) \u548c \\(n_0\\)\uff0c\u5c0d\u65bc\u6240\u6709\u5927\u65bc\u7b49\u65bc \\(n_0\\) \u7684 \\(n\\)\uff0c\u4f7f\u5f97 \\(f(n)\\) \u5c0f\u65bc\u7b49\u65bc \\(c*g(n)\\)\uff0c\u90a3\u9ebc \\(f(n)\\) \u5c31\u5c6c\u65bc \\(\\mathcal{O}(g(n))\\)\u3002

\u5176\u4e2d \\(\\mathcal{O}(g(n))\\) \u662f\u4e00\u500b\u51fd\u6578\u96c6\u5408\uff0c\u5b83\u5305\u542b\u4e86\u6240\u6709\u4ee5 \\(c*g(n)\\) \u70ba\u4e0a\u754c\u7684\u51fd\u6578\uff0c\u800c \\(f(n)=\\mathcal{O}(g(n))\\) \u8207 \\(f(n) \\in \\mathcal{O}(g(n))\\) \u90fd\u662f\u4ee3\u8868 \\(f(n)\\) \u5c6c\u65bc \\(\\mathcal{O}(g(n))\\) \u7684\u4e00\u54e1\uff0c\u770b\u4f60\u7fd2\u6163\u7528\u54ea\u4e00\u7a2e\u7b26\u865f\u3002

\u9084\u662f\u4e0d\u592a\u61c2\uff0c\u90a3\u6211\u7528\u66f4\u767d\u8a71\u7684\u65b9\u5f0f\u9084\u6709\u6211\u5011\u719f\u6089\u7684 \\(f(x)\\) \u4f86\u89e3\u91cb:

\u6709\u5169\u500b\u51fd\u6578 \\(f(x)\\) \u548c \\(g(x)\\) \u5728\u4e8c\u7dad\u7684\u5ea7\u6a19\u5e73\u9762\u4e0a\uff0c\u6211\u5982\u679c\u627e\u7684\u5230\u4e00\u500b\u500d\u6578 \\(c\\) \u548c\u4e00\u689d\u925b\u76f4\u7dda \\(x=n_0\\)\uff0c\u4f7f\u5f97 \\(f(x)\\) \u5728\u925b\u76f4\u7dda\u53f3\u65b9(\\(x \\ge n_0\\))\uff0c\\(f(x)\\) \u7684\u5716\u5f62\u90fd\u5728 \\(c*g(x)\\) \u7684\u4e0b\u65b9\uff0c\u90a3\u9ebc \\(f(x)\\) \u5c31\u5c6c\u65bc \\(\\mathcal{O}(g(x))\\)\u3002

\u6ce8\u610f\u770b\u5f37\u8abf\u7684\u6587\u5b57\u5594\uff0c\u767d\u8a71\u7684\u89e3\u91cb\u61c9\u8a72\u80fd\u8b93\u4f60\u5728\u8166\u6d77\u4e2d\u6709\u756b\u9762\u3002

\u4f46\u89ba\u5f97\u9019\u6a23\u9084\u4e0d\u5920\u6e05\u695a\uff0c\u6211\u5011\u76f4\u63a5\u4f86\u770b\u4f8b\u5b50\uff0c\u6211\u76f8\u4fe1\u4f60\u80fd\u770b\u61c2!

\u5b9a\u7fa9 \\(f(n)=2n+1, \\ g(n)=n\\)\uff0c\u4ee5\u53ca \\(c=3, \\ h(n)=c*g(n)=3n\\)\uff0c\u4f86\u770b \\(f(n)\\) \u662f\u5426\u5c6c\u65bc \\(\\mathcal{O}(g(n))\\)\uff0c\u8acb\u770b\u4e0b\u5716:

\\(h(n)\\) \u8207 \\(f(n)\\) \u4ea4\u65bc \\((1,3)\\)\uff0c\u90a3\u9ebc \\(n_0=1\\)\uff0c\u4e14\u7576 \\(n \\ge n_0\\) \u6642\uff0c\\(f(n)\\) \u6703\u5c0f\u65bc\u7b49\u65bc \\(h(n)\\)\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u6211\u5011\u627e\u7684\u5230 \\(c\\) \u548c \\(n_0\\)\uff0c\u6240\u4ee5 \\(f(n)=\\mathcal{O}(g(n))=\\mathcal{O}(n)\\)\u3002

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9 \\(f(n)=n+2,\\ g(n)=n^{2},\\ c=1,g(n)=cg(n)\\)\uff0c\u76f4\u63a5\u770b\u5716:

\\(g(n)\\) \u8207 \\(f(n)\\) \u4ea4\u65bc \\((2,4)\\)\uff0c\u90a3\u9ebc \\(n_0=2\\)\uff0c\u4e14\u7576 \\(n \\ge n_0\\) \u6642\uff0c\\(f(n)\\) \u6703\u5c0f\u65bc\u7b49\u65bc \\(c*g(n)\\)\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u6211\u5011\u627e\u7684\u5230 \\(c\\) \u548c \\(n_0\\)\uff0c\u6240\u4ee5 \\(f(n)=\\mathcal{O}(g(n))=\\mathcal{O}(n^{2})\\)\u3002

\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\\(n+2=\\mathcal{O}(n^2)\\)\uff0c\u4f46\u4f60\u53ef\u4ee5\u5f9e\u7b2c\u4e00\u500b\u4f8b\u5b50\u4e2d\u985e\u63a8\u51fa \\(n+2=\\mathcal{O}(n)\\)\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u524d\u8005\u4e5f\u662f\u5c0d\u7684\uff0c\u4f46\u6211\u5011\u61c9\u8a72\u9078\u64c7\u66f4\u63a5\u8fd1\u7684\u4e0a\u754c \\(\\mathcal{O}(n)\\) \uff0c\u56e0\u70ba\u66f4\u80fd\u8868\u73fe \\(n+2\\) \u7684\u884c\u70ba\uff0c\u7576\u7136\uff0c\\(n+2=O(n\\log_{}n)=O(n^2)=O(n^3)=O(n!)\\cdots\\) \u4e5f\u90fd\u662f\u5c0d\u7684\u3002

"},{"location":"algorithm/what_is_algo/#calculating_big_o","title":"Calculating Big O","text":"

\u90a3\u9ebc\u5982\u4f55\u8a08\u7b97\u4e00\u500b\u51fd\u6578\u7684\u5927 \\(\\mathcal{O}\\) \u7b26\u865f\u5462?\u53ea\u8981\u8a18\u4f4f\u4ee5\u4e0b\u539f\u5247:

  1. \u5ffd\u7565\u5e38\u6578\u8207\u4fc2\u6578\uff0c\u4f8b\u5982: \\(f(n)=114514\\)\uff0c\u90a3\u9ebc \\(f(n)=\\mathcal{O}(1)\\)\uff0c\u6bd4\u8f03\u7279\u5225\u7684\u662f\u56e0\u70ba\u8f38\u5165\u8cc7\u6599\u7684\u898f\u6a21\u4e0d\u6703\u5f71\u97ff \\(f(n)\\) \u7684\u503c\uff0c\\(\\mathcal{O}(1)\\) \u53c8\u7a31\u70ba\u5e38\u6578\u6642\u9593\u3002
  2. \u53ea\u4fdd\u7559\u6700\u9ad8\u6b21\u9805\uff0c\u4f8b\u5982: \\(f(n)=n^2+2n+3\\)\uff0c\u90a3\u9ebc \\(f(n)=\\mathcal{O}(n^2)\\)\u3002
  3. \u4e0d\u5728\u4e4e\u5c0d\u6578\u7684\u5e95\u6578\uff0c\u4f8b\u5982: \\(f(n)=n\\log_{2}n\\)\uff0c\u90a3\u9ebc \\(f(n)=\\mathcal{O}(n\\log_{}n)\\)\u3002
"},{"location":"algorithm/what_is_algo/#common_big_o","title":"Common Big O","text":"

\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u5927 \\(\\mathcal{O}\\) \u7b26\u865f:

  1. \\(\\mathcal{O}(1)\\): \u5e38\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u5b58\u53d6\u9663\u5217\u7684\u5143\u7d20
  2. \\(\\mathcal{O}(\\log_{}n)\\): \u5c0d\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u4e8c\u5143\u641c\u5c0b\u6cd5(Binary Search)
  3. \\(\\mathcal{O}(n)\\): \u7dda\u6027\u6642\u9593\uff0c\u4f8b\u5982: \u627e\u51fa\u9663\u5217\u4e2d\u7684\u6700\u5927\u503c
  4. \\(\\mathcal{O}(n\\log_{}n)\\): \u7dda\u6027\u5c0d\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u5feb\u901f\u6392\u5e8f(Quick Sort)
  5. \\(\\mathcal{O}(n^2)\\): \u5e73\u65b9\u6642\u9593\uff0c\u4f8b\u5982: \u6ce1\u6cab\u6392\u5e8f(Bubble Sort)
  6. \\(\\mathcal{O}(n^3)\\): \u7acb\u65b9\u6642\u9593\uff0c\u4f8b\u5982: \u77e9\u9663\u4e58\u6cd5
  7. \\(\\mathcal{O}(2^n)\\): \u6307\u6578\u6642\u9593\uff0c\u4f8b\u5982: \u905e\u8ff4\u8cbb\u6c0f\u6578\u5217(Fibonacci Sequence)
  8. \\(\\mathcal{O}(n!)\\): \u968e\u4e58\u6642\u9593\uff0c\u4f8b\u5982: \u65c5\u884c\u63a8\u92b7\u54e1\u554f\u984c(Travelling Salesman Problem)

\u9019\u500b\u53c3\u8003\u5c31\u597d\u5594\uff0c\u770b\u904e\u53bb\u5c31\u597d\uff0c\u4e0d\u7528\u80cc\u3002

"},{"location":"algorithm/what_is_algo/#other_notations","title":"Other Notations","text":"

\u9664\u4e86\u5927 \\(\\mathcal{O}\\) \u7b26\u865f\u5916\uff0c\u9084\u6709\u5176\u4ed6\u7684\u6f38\u8fd1\u7b26\u865f:

  1. Big Omega: \\(\\Omega(g(n))\\)\uff0c\u7528\u4f86\u63cf\u8ff0\u4e00\u500b\u6f14\u7b97\u6cd5\u5728\u300c\u6700\u4f73\u60c5\u6cc1\u300d\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e0b\u754c(Lower Bound)\u3002
  2. Big Theta: \\(\\Theta(g(n))\\)\uff0c\u7528\u4f86\u63cf\u8ff0\u4e00\u500b\u6f14\u7b97\u6cd5\u5728\u300c\u5e73\u5747\u60c5\u6cc1\u300d\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e0a\u754c(Upper Bound)\u548c\u4e0b\u754c(Lower Bound)\u7684\u4ea4\u96c6\u3002

\u4f46\u5927 \\(\\mathcal{O}\\) \u7b26\u865f\u5c31\u5920\u7528\u4e86\uff0c\u9019\u5169\u500b\u7b26\u865f\u4e0d\u7528\u592a\u904e\u65bc\u6df1\u7a76\u3002

\u4f86\u500b\u7e3d\u7d50\u5427:

\\[\\text{if } f(n)=3n+2 \\text{ then:}\\] \\[\\underbrace{1 \\lt \\log_{}n \\lt \\sqrt{n} \\lt n^{\\frac{2}{3}}}_\\text{lower bound, including \u03a9(n)} \\lt \\overbrace{n}^\\text{Average bound, \u0398(n)} \\lt \\underbrace{n\\log_{}n \\lt n^2 \\lt n^3 \\lt \\cdots \\lt 2^n \\lt \\cdots \\lt n^n}_\\text{upper bound, including O(n)}\\]

\u4e00\u6a23\uff0c\u770b\u770b\u5c31\u597d\uff0c\u53ea\u662f\u70ba\u4e86\u8b93\u4f60\u77e5\u9053\u9084\u6709\u5176\u4ed6\u7684\u7b26\u865f\u3002

\u770b\u4e86\u90a3\u9ebc\u591a\uff0c\u4f11\u606f\u4e00\u4e0b\uff0c\u807d\u9996\u6b4c\u5427!

Coldplay - Viva La Vida

"},{"location":"algorithm/what_is_algo/#practice","title":"Practice","text":"

Practice 1

\u8acb\u554f\u4e0b\u65b9\u7a0b\u5f0f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

n = 5\ncnt = 0\n\nfor i in range(n):\n    for j in range(i):\n        cnt += 1\n\nprint(cnt)\n

Answer 1

\u5167\u5c64\u8ff4\u5708\u7684\u57f7\u884c\u6b21\u6578\u53d6\u6c7a\u65bc\u5916\u5c64\u8ff4\u5708\u7684 i \u503c\uff0c\u56e0\u6b64\uff0c\u7b2c\u4e00\u6b21\u57f7\u884c \\(0\\) \u6b21\uff0c\u7b2c\u4e8c\u6b21\u57f7\u884c \\(1\\) \u6b21\uff0c\u7b2c\u4e09\u6b21\u57f7\u884c \\(2\\) \u6b21\uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f:

\\(0+1+2+\\cdots+(n-1)=\\sum_{i=0}^{n-1}i=\\frac{(n-1)n}{2}=\\frac{n^2-n}{2}=\\mathcal{O}(n^2)\\)

\u9019\u500b\u8907\u96dc\u5ea6\u5728\u4ecb\u7d39\u6392\u5e8f\u6f14\u7b97\u6cd5\u6642\u6703\u518d\u6b21\u63d0\u5230\u3002

Practice 2

\u8acb\u554f\u4e0b\u65b9\u7a0b\u5f0f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

n = 64\ni = 0\n\nwhile i ** 2 < n:\n    i += 1\n\nprint(i)\n

Answer 2

\\(i^2=n\\)\uff0c\u6240\u4ee5 \\(i=\\sqrt{n}\\)\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(\\sqrt{n})\\)\u3002

Practice 3

\u8acb\u554f\u51fd\u5f0f search \u7684\u6700\u4f73\u3001\u6700\u58de\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

def search(lst, target):\n    for i in range(len(lst)):\n        if lst[i] == target:\n            return i\n    return -1\n\n\na = [1, 2, 3, 4, 5, 6]\nprint(search(a, 1))\nprint(search(a, 3))\nprint(search(a, 7))\n

Answer 3

\u6700\u4f73\u72c0\u6cc1\u5c31\u662f\u7b2c\u4e00\u500b\u5143\u7d20\u5c31\u662f\u76ee\u6a19\uff0c\u6700\u58de\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u4e0d\u5b58\u5728\u6216\u662f\u4ed6\u662f\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u5e73\u5747\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u5728\u4e32\u5217\u7684\u4e2d\u9593\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(1),\\mathcal{O}(n),\\mathcal{O}(n/2)\\)\u3002

\u9019\u500b\u51fd\u5f0f\u5c31\u662f\u7dda\u6027\u641c\u5c0b\u6cd5(Linear Search)\uff0c\u5728\u5f8c\u9762\u7684\u7ae0\u7bc0\u6703\u518d\u6b21\u63d0\u5230\u3002

Practice 4

\u8acb\u554f\u51fd\u5f0f factorial \u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

def factorial(n):\n    if n == 0:\n        return 1\n    return n * factorial(n - 1)\n\nprint(factorial(2))\n

Answer 4

\u5225\u5bb3\u6015\uff0c\u9019\u662f\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f\uff0c\u756b\u51fa\u6d41\u7a0b\u5716:

graph LR\n    A[\"factorial(2)\"] -->|\"2 != 0\"| B[\"2 * factorial(1)\"]\n    B -->|\"1 != 0\"| C[\"1 * factorial(0)\"]\n    C -->|\"0 == 0\"| D[\"1\"]\n    D -->|return| C\n    C -->|return| B\n    B -->|return| A

\u76f4\u5230 \\(n=0\\) \u6642\uff0c\u624d\u6703\u7d50\u675f\u905e\u8ff4\uff0c\u63a5\u8457\u5411\u4e0a\u8fd4\u56de\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(n)\\)\u3002

"},{"location":"algorithm/basic_algo/","title":"Basic Algorithm","text":"

\u5148\u4f86\u71b1\u500b\u8eab\uff0c\u6211\u5011\u5f9e\u4e00\u4e9b\u57fa\u672c\u7684\u6f14\u7b97\u6cd5\u958b\u59cb\u3002

"},{"location":"algorithm/basic_algo/binary_search/","title":"Binary Search","text":""},{"location":"algorithm/basic_algo/binary_search/#introduction","title":"Introduction","text":"

Binary Search(\u4e8c\u5143\u641c\u5c0b\u6cd5) \u662f\u5206\u6cbb\u6cd5(Divide and Conquer)\u7684\u4e00\u7a2e\u61c9\u7528\uff0c\u975e\u5e38\u6709\u6548\u7387\uff0c\u4f46\u53ea\u9069\u7528\u5728\u55ae\u8abf(Monotonic)\u7684\u5e8f\u5217\u6216\u51fd\u6578\u4e0a\uff0c\u4e5f\u5c31\u662f\u8aaa\uff0c\u5fc5\u9808\u662f\u905e\u589e\u6216\u905e\u6e1b\u7684\uff0c\u6216\u662f\u4f60\u7c21\u55ae\u7406\u89e3\u6210\u6392\u5e8f\u904e(sorted)\u7684\uff0c\u8209\u5e7e\u500b\u4f8b\u5b50:

  1. [1, 2, 3, 4, 5]
  2. ['a', 'b', 'c', 'd', 'e']
  3. [32, 16, 8, 4, 2, 1]
  4. \\(f(x)=x^2, \\ x \\ge 0,\\ [f(1), f(2), f(3), f(4), f(5)]\\)

\u51fd\u6578 \\(f(x)=x^2\\) \u5728 \\(x \\ge 0\\) \u6642\u662f\u905e\u589e\u7684:

def f(x):\n    return x ** 2\n\n\nxs = [1, 2, 3, 4, 5]\nprint(xs)\n# [f(1), f(2), f(3), f(4), f(5)]\nys = [f(x) for x in xs]\nprint(ys)\n
Output
[1, 2, 3, 4, 5]\n[1, 4, 9, 16, 25]\n

\u6700\u5f8c\u4e00\u500b\u4f8b\u5b50\u662f\u60f3\u8ddf\u4f60\u5f37\u8abf\uff0c\u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u4e8c\u5143\u641c\u5c0b\u6cd5\uff0c\u4f8b\u5982\u60f3\u8981\u627e\u7b2c\u4e00\u500b \\(f(x) \\ge 10\\) \u7684\u6574\u6578 \\(x\\)\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u4e8c\u5143\u641c\u5c0b\u6cd5\u627e\u5230 \\(x=4\\)\u3002

"},{"location":"algorithm/basic_algo/binary_search/#complexity","title":"Complexity","text":"

\u73a9\u904e\u731c\u6578\u5b57\u55ce?\u4f60\u6bcf\u731c\u4e00\u6b21\uff0c\u6211\u6703\u544a\u8a34\u4f60\u731c\u7684\u6578\u5b57\u662f\u592a\u5927\u9084\u662f\u592a\u5c0f\uff0c\u8b93\u4f60\u5f9e \\([1, 100]\\) \u9593\u731c\u4e00\u500b\u6578\u5b57\uff0c\u600e\u9ebc\u731c\u6703\u6700\u6709\u6548\u7387?\u5982\u679c\u4f60\u7b2c\u4e00\u6b21\u5c31\u731c \\(50\\)\uff0c\u6211\u544a\u8a34\u4f60\u731c\u7684\u6578\u5b57\u592a\u5927\uff0c\u4f60\u5c31\u53ef\u4ee5\u77e5\u9053\u7b54\u6848\u4e00\u5b9a\u5728 \\([1, 49]\\) \u4e4b\u9593\uff0c\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u7701\u4e0b\u4e00\u534a\u7684\u6642\u9593\uff0c\u4f60\u53ef\u4ee5\u518d\u731c \\(25\\)\uff0c\u800c\u6211\u544a\u8a34\u4f60\u731c\u7684\u6578\u5b57\u592a\u5c0f\uff0c\u90a3\u9ebc\u7b54\u6848\u4e00\u5b9a\u5728 \\([26, 49]\\) \u4e4b\u9593\uff0c\u4f60\u53c8\u7701\u4e0b\u4e00\u534a\u7684\u6642\u9593\uff0c\u9019\u5c31\u662f\u4e8c\u5143\u641c\u5c0b\u6cd5\u7684\u7cbe\u795e\u3002

\u90a3\u6700\u591a\u8981\u731c\u5e7e\u6b21\u5462?\u5047\u8a2d\u4f60\u771f\u7684\u5f88\u8870\uff0c\u731c\u5230\u5269\u4e0b \\([47, 48]\\) \u9019\u5169\u500b\u6578\u5b57\u5728\u9078\u7684\u6642\u5019\u53c8\u731c\u932f\uff0c\u4f60\u731c\u4e86 \\(47\\) \u800c\u6211\u544a\u8a34\u4f60\u731c\u592a\u5c0f\uff0c\u90a3\u5c31\u53ea\u5269\u4e0b \\(48\\) \u4e86\uff0c\u4e5f\u5c31\u662f\u5340\u9593 \\([48, 48]\\) \u5269\u4e0b\u4e00\u500b\u5143\u7d20\uff0c\u9019\u662f\u6211\u5011\u7684\u7d42\u6b62\u689d\u4ef6\u3002

\u8003\u616e\u4e00\u822c\u7684\u60c5\u6cc1\uff0c\u6709 \\(n\\) \u500b\u6578\u5b57\uff0c\u6bcf\u6b21\u731c\u90fd\u53ef\u4ee5\u628a\u5340\u9593\u7e2e\u5c0f\u4e00\u534a\uff0c\u8a2d\u6211\u5011\u8981\u731c \\(k\\) \u6b21\u624d\u80fd\u628a\u5340\u9593\u7e2e\u5c0f\u5230\u5269\u4e0b\u4e00\u500b\u5143\u7d20:

\\[ \\frac{n}{2^k} = 1 \\] \\[ \\Rightarrow n = 2^k \\] \\[ \\Rightarrow \\log_2 n = k \\]

\u90a3\u9ebc\u6642\u9593\u8907\u96dc\u5ea6\u5c31\u662f \\(\\mathcal{O}(k) = \\mathcal{O}(\\log n)\\)\u3002

"},{"location":"algorithm/basic_algo/binary_search/#implementation","title":"Implementation","text":"

\u5728\u5beb\u7a0b\u5f0f\u4e4b\u524d\uff0c\u5148\u4f86\u770b\u52d5\u756b\uff0c\u6709\u4e00\u500b\u5df2\u6392\u5e8f\u904e\u7684\u4e32\u5217 [1, 1, 4, 5, 7, 8, 9, 10, 11, 12]\uff0c\u6211\u5011\u8981\u627e 4:

\u518d\u4f86\u770b\u7a0b\u5f0f\u78bc:

binary_search.py
def binary_search(lst, target):\n    left, right = 0, len(lst) - 1\n\n    while left <= right:\n        mid = (left + right) // 2\n        if lst[mid] == target:\n            return mid\n        elif lst[mid] < target:\n            left = mid + 1\n        else:\n            right = mid - 1\n\n    return -1\n\nnums = [1, 1, 4, 5, 7, 8, 9, 10, 11, 12]\nprint(binary_search(nums, 4))\n
Output
2\n
"},{"location":"algorithm/basic_algo/linear_search/","title":"Linear Search","text":""},{"location":"algorithm/basic_algo/linear_search/#introduction","title":"Introduction","text":"

\u7dda\u6027\u641c\u5c0b(Linear Search)\u6216\u7a31\u5faa\u5e8f\u641c\u5c0b(Squential Search)\uff0c\u662f\u4e00\u7a2e\u7c21\u55ae\u7684\u641c\u5c0b\u6f14\u7b97\u6cd5\uff0c\u5b83\u662f\u4e00\u7a2e\u904d\u6b77\u6574\u500b\u4e32\u5217\u7684\u65b9\u5f0f\uff0c\u9010\u4e00\u6aa2\u67e5\u6bcf\u500b\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6a19\u3002

\u9019\u500b\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(n)\\)\uff0c\u56e0\u70ba\u6700\u58de\u7684\u60c5\u6cc1\u5c31\u662f\u76ee\u6a19\u503c\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u6aa2\u67e5\u6574\u500b\u4e32\u5217\u3002

\u9019\u61c9\u8a72\u662f\u6700\u7c21\u55ae\u7684\u6f14\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4e0d\u904e\u6211\u5011\u53ef\u4ee5\u4f86\u8907\u7fd2\u4e0a\u4e00\u7ae0\u6240\u5b78\u7684\u6642\u9593\u8907\u96dc\u5ea6\u3002

"},{"location":"algorithm/basic_algo/linear_search/#implementation","title":"Implementation","text":"

\u76f4\u63a5\u4f86\u770b\u7a0b\u5f0f\u78bc\uff0c\u5b9a\u7fa9 linear_search(lst, target) \u51fd\u5f0f\uff0c\u9019\u500b\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u4e32\u5217 lst \u548c\u4e00\u500b\u76ee\u6a19\u503c target\uff0c\u56de\u50b3\u76ee\u6a19\u503c\u5728\u4e32\u5217\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u76ee\u6a19\u503c\u4e0d\u5b58\u5728\u5c31\u56de\u50b3 -1\u3002

linear_search.py
def linear_search(lst, target):\n    for i in range(len(lst)):\n        if lst[i] == target:\n            return i\n\n    return -1\n\n\na = [4, 8, 4, 3, 1, 2, 3, 0]\nprint(linear_search(a, 4))\nprint(linear_search(a, 0))\nprint(linear_search(a, 10))\n
Output
0\n7\n-1\n

\u9084\u8a18\u5f97\u4e0a\u4e00\u7ae0\u7684\u7df4\u7fd2\u55ce? \u9019\u500b\u51fd\u5f0f\u7684\u6700\u4f73\u3001\u6700\u58de\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u662f\u591a\u5c11?

Answer

\u6700\u4f73\u72c0\u6cc1\u5c31\u662f\u7b2c\u4e00\u500b\u5143\u7d20\u5c31\u662f\u76ee\u6a19\uff0c\u6700\u58de\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u4e0d\u5b58\u5728\u6216\u662f\u4ed6\u662f\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u5e73\u5747\u72c0\u6cc1\u5c31\u662f\u76ee\u6a19\u5728\u4e32\u5217\u7684\u4e2d\u9593\uff0c\u6240\u4ee5\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(\\mathcal{O}(1),\\mathcal{O}(n),\\mathcal{O}(n/2)\\)\u3002

\u800c\u4e32\u5217\u539f\u672c\u5c31\u6709\u4e00\u500b index \u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u9019\u4ef6\u4e8b\uff0c\u4ee5\u53ca\u4f60\u53ef\u4ee5\u7528 in \u95dc\u9375\u5b57\u4f86\u6aa2\u67e5\u76ee\u6a19\u503c\u662f\u5426\u5728\u4e32\u5217\u4e2d\u3002

a = [4, 8, 4, 3, 1, 2, 3, 0]\nprint(a.index(4))\nprint(4 in a)\n
Output
0\nTrue\n

\u6240\u4ee5\u9019\u500b\u6f14\u7b97\u6cd5\u5728\u5be6\u52d9\u4e0a\u4e26\u4e0d\u5e38\u7528\uff0c\u5c31\u7576\u4f5c\u71b1\u71b1\u8eab\u3002

"},{"location":"algorithm/basic_algo/linear_search/#practice","title":"Practice","text":""},{"location":"algorithm/basic_algo/linear_search/#leetcode_540_single_element_in_a_sorted_array","title":"LeetCode 540. Single Element in a Sorted Array","text":"

LeetCode 540. Single Element in a Sorted Array

Reference Code
class Solution:\n    def singleNonDuplicate(self, nums: list[int]) -> int:\n        if len(nums) == 1 or nums[0] != nums[1]:\n            return nums[0]\n        elif nums[-1] != nums[-2]:\n            return nums[-1]\n\n        for i in range(1, len(nums) - 1):\n            if nums[i] != nums[i - 1] and nums[i] != nums[i + 1]:\n                return nums[i]\n

\u5f88\u76f4\u89c0\uff0c\u53ea\u8981\u6aa2\u67e5\u76f8\u9130\u7684\u5169\u500b\u5143\u7d20\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u5c31\u662f\u7b54\u6848\u3002\u4f46\u9019\u6a23\u6211\u5011\u4e26\u6c92\u6709\u7528\u5230\u9019\u500b\u4e32\u5217\u662f\u6392\u5e8f\u904e\u7684\u9019\u500b\u7279\u6027\uff0c\u4e0b\u4e00\u7ae0\u4ecb\u7d39\u7684\u4e8c\u5143\u641c\u5c0b\u6cd5(Binary Search)\u53ef\u4ee5\u66f4\u5feb\u7684\u89e3\u6c7a\u9019\u500b\u554f\u984c\u3002

"},{"location":"blog/","title":"Blog","text":""},{"location":"blog/2024/02/06/hello-blog-/","title":"Hello Blog !","text":"

Hi, this is a test post.

For the better experience of the development, I think recording the process of the development is a good idea.

If you are interested in the project, you can follow the blog to get the latest information about the project.

Also, plaease contact me if you wanna join the project. I am looking forward to working with you.

"},{"location":"blog/2024/02/19/%E7%B6%B2%E7%AB%99%E7%9A%84%E6%96%B0%E5%90%8D%E5%AD%97/","title":"\u7db2\u7ad9\u7684\u65b0\u540d\u5b57","text":"

\u7db2\u7ad9\u7684\u65b0\u540d\u5b57\u662f ZestAlgo\uff0c\u53c3\u898b\u95dc\u65bc\u7db2\u7ad9\u540d\u7a31 #17

\u4f46\u9084\u662f\u53ef\u4ee5\u7e7c\u7e8c\u8a0e\u8ad6\uff0c\u800c\u539f\u672c\u7684 LMcps \u5c31\u53ef\u4ee5\u6b78\u985e\u70ba\u53c3\u8207\u6b64\u5c08\u6848\u7684\u7dad\u8b77\u793e\u7fa4\u4e4b\u4e00\u3002

"},{"location":"blog/2024/02/19/%E7%B6%B2%E7%AB%99%E7%9A%84%E6%96%B0%E5%90%8D%E5%AD%97/#logo","title":"\u95dc\u65bcLogo","text":"

\u56e0\u70ba Zest \u6709\u679c\u76ae\u7684\u610f\u601d\uff0c\u6240\u4ee5\u53ef\u4ee5\u671d\u5411\u6a58\u5b50\u3001\u6ab8\u6aac\u3001\u67f3\u6a59\u7b49\u7b49\u7684\u65b9\u5411\u53bb\u8a2d\u8a08\uff0c\u7b49\u6211\u6709\u6642\u9593\u7684\u6642\u5019\uff0c\u6211\u6703\u8a2d\u8a08\u4e00\u500b Logo \u51fa\u4f86\u3002

"},{"location":"data_structure/","title":"Data Structure","text":""},{"location":"fundamental/","title":"Fundamental","text":"

\u9019\u88e1\u5c07\u6703\u6559\u4f60\u57fa\u790e\u7684 Python \u8207 C++ \u7684\u57fa\u790e\u8a9e\u6cd5\u3002

"},{"location":"fundamental/cpp/","title":"C++","text":"

\u6211\u662fc++\u7cfb\u5217\u7684\u7b46\u8005bloodnighttw\uff0c\u6211\u9810\u8a08\u5728\u6b64\u6587\u7ae0\u5206\u4e0b\u5217\u5e7e\u500b\u7ae0\u7bc0\uff0c\u4e26\u76e1\u91cf\u4ee5\u521d\u5b78\u8005\u7684\u89d2\u5ea6\u5beb\u51fa\u5167\u5bb9\u3002

  1. \u74b0\u5883\u67b6\u8a2d
  2. Hello world (\u57fa\u672c\u8f38\u51fa\u5165)
  3. \u578b\u614b\u3001\u8b8a\u6578\u8207\u76f8\u95dc\u904b\u7b97
  4. \u6d41\u7a0b\u63a7\u5236
  5. \u8ff4\u5708
  6. \u51fd\u5f0f
  7. struct & class
  8. \u6307\u6a19\u8207\u53c3\u7167
  9. standard template libary

\u5982\u679c\u5728\u64b0\u5beb\u4e0a\u6709\u4efb\u4f55\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u5bc4\u96fb\u5b50\u90f5\u4ef6\u5230 bbeenn1227@gmail.com \u6216\u8005 emails@bntw.dev \u4f86\u505a\u8a62\u554f(\u4e0d\u904e\u8acb\u8868\u660e\u4f60\u7684\u8eab\u4efd\uff0c\u4e0d\u7136\u6211\u53ef\u80fd\u6703\u5ffd\u7565)\uff0c\u6216\u8005\u53ef\u4ee5\u5728\u6211\u66f4\u5e38\u51fa\u73fe\u7684discord\u8a62\u554f\u6211\u3002

\u6b64\u9023\u7d50\u70ba\u9ece\u660e\u8cc7\u8a0a\u793e\u7684discord\u7fa4\u7d44\u9023\u7d50\uff0c\u52a0\u5165\u4e4b\u5f8c\u53ef\u4ee5tag @bloodnighttw\uff0c\u53ea\u8981\u6211\u5728\u7dda\u4e0a\u4e14\u6709\u6642\u9593\u6211\u90fd\u6703\u76e1\u91cf\u5e6b\u4f60\u56de\u7b54\u3002

\u53e6\u5916\u6709\u4efb\u4f55\u932f\u5b57\u6216\u53ef\u4ee5\u6539\u5584\u7684\u5730\u65b9\u8acb\u767cissues\uff0c\u6211\u5011\u6703\u518d\u8a0e\u8ad6\u904e\u5f8c\u9069\u6642\u5730\u505a\u51fa\u4fee\u6b63\u3002

"},{"location":"fundamental/cpp/0/create_env/","title":"0. \u74b0\u5883\u67b6\u8a2d","text":"

\u6b63\u6240\u8b02\u5de5\u6b32\u5584\u5176\u4e8b\uff0c\u5fc5\u5148\u5229\u5176\u5668\uff0c\u6b64\u7ae0\u6211\u5011\u5206\u5225\u6703\u5728\u5206\u5225\u8b1b\u8ff0\u5982\u4f55\u5728windows\u3001macos\u4e0a\u5b89\u88dd\u76f8\u95dc\u5957\u4ef6\u3002

\u6211\u5011\u9810\u8a08\u8981\u4f7f\u7528GNU C++ Compiler\u9032\u884c\u7de8\u8b6f\uff0c\u4ee5\u53ca\u4f7f\u7528Visual studio code + clangd\u8207c++11\u7684\u6a19\u6e96\u64b0\u5beb\u7a0b\u5f0f\uff0c\u4e0b\u9762\u6703\u8a73\u8ff0\u5b89\u88dd\u6d41\u7a0b\u3002

\u7576\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u50cf\u662fdev c++\u3001codeblock\u9019\u985e\u7684\u8edf\u9ad4\u9032\u884c\u64b0\u5beb\uff0c\u53ea\u4e0d\u904e\u6709\u4e00\u9ede\u9700\u8981\u6ce8\u610f\uff0c\u820a\u7248\u7684dev c++\u9810\u8a2d\u6c92\u6709\u555f\u7528c++11\u6a19\u6e96\u7684\u652f\u63f4\uff0c\u4f60\u5fc5\u9808\u8981\u5728\u8a2d\u5b9a\u88e1\u9762\u5c0b\u627e\u958b\u555f\u7684\u65b9\u5f0f\u3002

"},{"location":"fundamental/cpp/0/create_env/#windows","title":"Windows","text":"

GNU C++ Compiler (G++)\u672c\u8eab\u662f\u6c92\u6709\u652f\u63f4Windows\u7684\uff0c\u4f46\u6211\u5011\u53ef\u4ee5\u900f\u904e\u4f7f\u7528wsl2\u6216\u8005\u5225\u4eba\u7528\u597d\u7684\u79fb\u690d\u7248\u672c\u9032\u884c\u4f7f\u7528\uff0c\u9019\u908a\u6211\u5011\u6703\u5b89\u88dd\u5225\u4eba\u5beb\u597d\u7684\u79fb\u690d\u7248\u3002

"},{"location":"fundamental/cpp/0/create_env/#1mingw","title":"1.\u4e0b\u8f09\u4e26\u5b89\u88ddmingw","text":"
  1. \u524d\u5f80\u9019\u500b\u9023\u7d50\u5b89\u88ddmingw
  2. \u52fe\u9078 mingw32-gcc-g++
  3. installation > apply change
  4. \u6309\u4e0bapply\u5b89\u88dd

"},{"location":"fundamental/cpp/0/create_env/#2","title":"2. \u52a0\u5165\u74b0\u5883\u8b8a\u6578","text":"
  1. \u5c0b\u627e\u5b89\u88dd\u4f4d\u7f6e\uff0c\u4ee5\u6211\u70ba\u4f8b\uff0c\u662f\u5b89\u88dd\u5728C:\\MinGW\u88e1\u9762\uff0c\u627e\u5230\u88e1\u9762\u7684bin\u8cc7\u6599\u593e\uff0c\u9ede\u9032\u53bb\u4e26\u8907\u88fd\u8def\u5f91\u3002
  2. \u641c\u5c0b\u74b0\u5883\u8b8a\u6578\uff0c\u4e26\u9032\u53bb\u7de8\u8f2f\u74b0\u5883\u8b8a\u6578\u3002
  3. \u9ede\u5165 \u9032\u968e >\u74b0\u5883\u8b8a\u6578\uff0c\u4e26\u5728Path\u4e2d\u8cbc\u4e0a\u525b\u624d\u8907\u88fd\u7684\u8b8a\u6578\uff0c\u7136\u5f8c\u4fdd\u5b58\u9000\u51fa\u3002

"},{"location":"fundamental/cpp/0/create_env/#3","title":"3. \u6e2c\u8a66\u6307\u4ee4","text":"

\u958b\u555f\u63d0\u793a\u547d\u4ee4\u5b57\u5143\u5f8c\uff0c\u6253\u5165g++ -v\uff0c\u5982\u679c\u6709\u7248\u672c\u8a0a\u606f\u4ee3\u8868\u5b89\u88dd\u6210\u529f\u3002

"},{"location":"fundamental/cpp/0/create_env/#macos","title":"macOS","text":"

macos\u672c\u8eab\u5c31\u6709\u63d0\u4f9bclangd\u4f5c\u70ba\u7de8\u8b6f\u5668\u4f7f\u7528\uff0c\u5728\u5b89\u88ddxcode\u5f8c\u61c9\u8a72\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982\u679c\u9084\u662f\u9700\u8981GNU C++ Compiler\uff0c\u8acb\u53c3\u8003\u4e0b\u9762\u6b65\u9a5f\u3002

"},{"location":"fundamental/cpp/0/create_env/#1homwbrew","title":"1.\u5b89\u88ddhomwbrew\u5957\u4ef6\u7ba1\u7406\u5668","text":"

\u6839\u64da\u5b98\u7db2\u7684\u6307\u793a\u5b89\u88ddhomebrew\u3002

"},{"location":"fundamental/cpp/0/create_env/#2g","title":"2.\u4f7f\u7528\u7ba1\u7406\u5668\u5b89\u88ddg++\u5957\u4ef6","text":"
  1. \u5148\u4f7f\u7528brew search gcc\u641c\u5c0b\u76f8\u95dc\u5957\u4ef6
  2. \u4f7f\u7528 brew install gcc\u5b89\u88ddCompiler
  3. \u4f7f\u7528gcc-13 -v\u6216g++-13\u78ba\u8a8d\u662f\u5426\u6210\u529f\u5b89\u88dd

Warning

\u8acb\u6ce8\u610f\uff0c\u5728macos\u4e0a\u9810\u8a2d\u4f7f\u7528\u7684g++\u6307\u4ee4\u70baclang\u800c\u4e0d\u662fGNU C++ Compiler\uff0c\u8acb\u6539\u4f7f\u7528g++-XX \u4f86\u64cd\u4f5cg++\u7de8\u8b6f\u5668\u3002

"},{"location":"fundamental/cpp/0/create_env/#visual_studio_code","title":"Visual Studio Code","text":""},{"location":"fundamental/cpp/0/create_env/#1_visual_studio_code","title":"1. \u5b89\u88ddVisual Studio Code","text":"

\u524d\u5f80\u9019\u500b\u7db2\u7ad9\u4e0b\u8f09\u5c6c\u65bc\u4f60\u7cfb\u7d71\u7684visual studio code\u5b89\u88dd\u6a94\u3002

"},{"location":"fundamental/cpp/0/create_env/#2_extension","title":"2. \u5b89\u88dd extension","text":"
  1. \u9ede\u5165extension\u9801\u9762\u5206\u5225\u5b89\u88ddC/C++ clangd \u9019\u5169\u500bextension\uff0c\u4e26\u91cd\u555fvscode
  2. \u4f60\u53ef\u80fd\u9047\u5230\u9019\u6a23\u7684\u8b66\u544a\u8a0a\u606f\uff0c\u8acb\u6309\u4e0b Disable Intellisence
"},{"location":"fundamental/cpp/0/create_env/#_1","title":"\u5176\u4ed6","text":"

\u5982\u679c\u9047\u5230\u5b57\u9ad4\u975e\u7b49\u5bec\uff0c\u8acb\u5c0b\u627e\u4e00\u500b\u7b49\u5bec\u5b57\u9ad4\u4e26\u4f7f\u7528\uff0c\u7b49\u5bec\u5b57\u9ad4\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002

"},{"location":"fundamental/cpp/1/hello_world/","title":"1. Hello World","text":""},{"location":"fundamental/cpp/1/hello_world/#hello_world","title":"Hello World (\u8f38\u51fa)","text":""},{"location":"fundamental/cpp/1/hello_world/#_1","title":"\u521d\u5b78\u8005\u7684\u7b2c\u4e00\u6b65","text":"

\u9996\u5148\uff0c\u8acb\u5728vscode (Visual studio code)\u4e2d\u65b0\u589e\u4e00\u500b\u540d\u70bahello_world.cpp\uff0c\u4e26\u6253\u5165\u4e0b\u5217\u7a0b\u5f0f\u78bc\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\" << endl;\n    return 0;\n}\n
\u78ba\u8a8d\u597d\u5b58\u6a94\u904e\u5f8c\uff0c\u6309\u4e0b\u53f3\u4e0a\u89d2\u7684\u57f7\u884c\u6309\u9375\uff0c\u4f60\u5c31\u6703\u6210\u529f\u770b\u5230\u4f60\u5beb\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\u3002

\u63a5\u8457\uff0c\u6211\u5011\u5c07\u66f4\u6df1\u5165\u8a0e\u8ad6\u9019\u6bb5\u7a0b\u5f0f\uff0c\u5230\u5e95\u767c\u751f\u4e86\u4ec0\u9ebc\u4e8b\u60c5\u3002

"},{"location":"fundamental/cpp/1/hello_world/#hello_world_1","title":"Hello World\u7a0b\u5f0f\u89e3\u91cb","text":"

\u5728\u9019\u6bb5\u7a0b\u5f0f\u4e2d\uff0c\u5df2\u7d93\u6709\u8a31\u591a\u7a0d\u5fae\u8907\u96dc\u7684\u6982\u5ff5\uff0c\u5148\u5225\u6015\uff0c\u6211\u5011\u5c31\u53ea\u8981\u4e86\u89e3\u5230:

  1. \u7a0b\u5f0f\u4e00\u5b9a\u662f\u5f9eint main(){......}\u88e1\u9762\u958b\u59cb\u57f7\u884c\u7684
  2. \u6700\u5f8c\u4e00\u5b9a\u6703\u6709\u500breturn 0; \u4ee3\u8868\u7a0b\u5f0f\u6210\u529f\u7d50\u675f\u3002

\u8b93\u6211\u5011\u7126\u9ede\u653e\u5728\u7b2c\u516d\u884c:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\" << endl;\n    return 0;\n}\n

\u9019\u908a\u6709\u4e86\u6211\u5011\u7684\u7b2c\u4e00\u652f\u7a0b\u5f0f\uff0c\u4f60\u53ef\u4ee5\u628acout\u7576\u6210\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\uff0c\u628a<<\u7576\u6210\u50b3\u905e\u7684\u7bad\u982d\uff0c\u800c\u9019\u6bb5\u7a0b\u5f0f\u5c31\u662f\u8981\u628a\u9019\u6bb5\u5b57\u50b3\u7d66\u96fb\u8166\u5370\u51fa\u3002

\u9019\u908a\u7684endl\u4ee3\u8868\u8457\u63db\u884c\u7684\u610f\u601d\uff0c\u900f\u904e<<\u50b3\u7d66\u96fb\u8166\u87a2\u5e55\u7684\u6587\u5b57\u5370\u8868\u6a5f\u3002

\u53e6\u5916\u4e00\u500b\u5beb\u6cd5

\u9019\u908a\u63d0\u4f9b\u53e6\u5916\u4e00\u500b\u63db\u884c\u7684\u5beb\u6cd5\uff0c\u5177\u9ad4\u5dee\u7570\u5728\u719f\u6089\u8a9e\u6cd5\u5f8c\uff0c\u6703\u5728\u53e6\u5916\u4e00\u7bc7\u6587\u7ae0\u505a\u8aaa\u660e\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\\n\";\n    return 0;\n}\n
\\n\u70ba\u63db\u884c\u5b57\u5143\uff0c\u5728\u5b57\u5143\u4e2d\u6709\u8a31\u591a\u5b57\u5143\u6703\u6709\u7279\u5b9a\u529f\u7528\uff0c\u6703\u4ee5\\\u958b\u982d\uff0c\u9023\u7d50\u4e00\u500b\u5b57\u6bcd\u6216\u6578\u5b57\uff0c\u4ee3\u8868\u8457\u8df3\u812b\u5b57\u5143\uff0c\u4f5c\u70ba\u7279\u6b8a\u7528\u9014\u4f7f\u7528\u3002

\u7d30\u5fc3\u5982\u4f60\uff0c\u9019\u662f\u53c8\u8981\u63d0\u554f\u4e86\uff0c\u5206\u865f;\u53c8\u4ee3\u8868\u4ec0\u9ebc\u610f\u601d\uff1f

\u5206\u865f;\u662f\u544a\u8a34\u96fb\u8166\u8aaa\uff0c\u9019\u4e9b\u6307\u4ee4\u8ddf\u4e0b\u4e00\u500b\u5b57\u5143\u6216\u4e0b\u4e00\u884c\u662f\u5206\u958b\u7684\uff0c\u4e0d\u8981\u628a\u4ed6\u9023\u5728\u4e00\u8d77\uff0c\u4e5f\u56e0\u70ba\u6709\u9019\u6771\u897f\uff0c\u6211\u5011\u7a0b\u5f0f\u53ef\u4ee5\u9019\u6a23\u5beb\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    cout << \"hello world!\\n\"; cout << endl;\n    return 0;\n}\n

Danger

\u5341\u5206\u4e0d\u5efa\u8b70\u4f60\u9019\u6a23\u5beb\uff0c\u5c0d\u65bc\u7a0b\u5f0f\u719f\u7df4\u7684\u4eba\u4f86\u8aaa\uff0c\u9019\u6a23\u7684\u6392\u7248\uff0c\u7a0b\u5f0f\u78bc\u8b80\u8d77\u4f86\u6703\u975e\u5e38\u75db\u82e6\uff0c\u5c0d\u65bc\u521d\u5b78\u8005\uff0c\u5247\u5bb9\u6613\u8aa4\u89e3\u7a0b\u5f0f\u78bc\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_2","title":"\u52a0\u5165\u8b8a\u6578 (\u8f38\u5165)","text":""},{"location":"fundamental/cpp/1/hello_world/#_3","title":"\u8b80\u53d6\u5b57\u4e32","text":"
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    string str;\n    cin >> str;\n    cout << str << endl;\n    return 0;\n}\n

\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u4e32\u6587\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u6587\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_4","title":"\u8b80\u53d6\u6578\u5b57","text":"

\u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp;\n    cin >> temp;\n    cout << temp << endl;\n    return 0;\n}\n

\u6309\u4e0b\u57f7\u884c\uff0c\u6253\u5165\u96a8\u4fbf\u4e00\u500b\u6578\u5b57\uff0c\u6309\u4e0benter\uff0c\u4f60\u6703\u767c\u73fe\u4f60\u6253\u7684\u4e00\u4e32\u6578\u5b57\u88ab\u5370\u5728\u87a2\u5e55\u4e0a\u3002

Info

\u5982\u679c\u4eca\u5929\u6253\u5165\u7684\u6578\u5b57\u975e\u5e38\u975e\u5e38\u7684\u5927\uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u5370\u51fa\u4f86\u7684\u6578\u5b57\u8ddf\u6253\u5165\u7684\u6578\u5b57\u4e0d\u4e00\u6a23\uff0c\u5176\u539f\u56e0\u662f\u6ea2\u4f4d\uff0c\u9019\u9ede\u6211\u5011\u6703\u5728\u4e0b\u4e00\u7ae0\u8ac7\u5230\u5176\u539f\u56e0\u8207\u89e3\u6cd5\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_5","title":"\u8f38\u5165\u7a0b\u5f0f\u89e3\u91cb","text":"

\u6b63\u5982cout\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u628acin\u7576\u6210\u4f60\u7684\u9375\u76e4\uff0c\u900f\u904e>>\u544a\u8a34\u96fb\u8166\u5f9e\u9375\u76e4\u8f38\u5165\u81f3\u53f3\u908a\u7684\u8b8a\u6578\u3002

\u5e38\u898b\u7684\u8aa4\u5340

\u5c0d\u65bc\u521d\u5b78\u8005\u4f86\u8aaa\uff0c\u53ef\u80fd\u6703\u4e0d\u5c0f\u5fc3\u5beb\u51fa\u9019\u6a23\u7684\u7a0b\u5f0f\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp1,temp2;\n    cin << temp1 << temp2;\n    cout << temp1 << \"!=\" << temp2 << endl;\n    return 0;\n}\n

\u6b64\u6642\u4f60\u6703\u767c\u73fe\u7121\u6cd5\u7de8\u8b6f\uff0c\u5176\u539f\u56e0\u662f\u4f60\u7684cin\u7684<<\u61c9\u8a72\u8981\u662f>>\u624d\u5c0d \u3002

\u5e38\u898b\u7684\u932f\u8aa4\u4e5f\u5305\u62ec\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a

  1. cout\u7684<< \u65b9\u5411\u932f\u8aa4
    #include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp1,temp2;\n    cin >> temp1 >> temp2;\n    cout >> temp1 >> temp2;\n    return 0;\n}\n
  2. cout\u7684\u4f4d\u7f6e\u932f\u8aa4

    #include <iostream>\n\nusing namespace std;\n\nint main(){\n    \"error\" >> cout;\n    return 0;\n}\n
    cout \u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002

  3. cin\u7684\u4f4d\u7f6e\u932f\u8aa4

    #include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a;\n    a << cin;\n    return 0;\n}\n
    \u539f\u56e0\u540c\u4e0a\uff0ccin\u53ea\u80fd\u653e\u5728\u5de6\u908a\u554a\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_6","title":"\u9023\u7e8c\u8b80\u53d6\u6578\u5b57/\u5b57\u4e32","text":"

\u5047\u5982\u4eca\u5929\u6211\u5011\u8f38\u5165\u7684\u4e00\u9023\u6578\u5b57\uff0c\u5247\u7a0b\u5f0f\u78bc\u6539\u6210\u9019\u6a23\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int temp1;\n    string temp2;\n    cin >> temp1 >> temp2;\n    cout << temp1 << \"!=\" << temp2 << endl;\n    return 0;\n}\n
input
1234 4321\n
output
1234!=4321\n

\u4f60\u6703\u767c\u73fe\u4e00\u4ef6\u4e8b\u60c5\uff0ccin\u662f\u900f\u904e\u7a7a\u683c\u6216\u8005\u63db\u884c\u4f86\u5206\u958b\u7684\uff0c\u4eca\u5929\u5982\u679c\u60f3\u8981\u8b80\u53d6\u4e00\u884c\uff0c\u8acb\u53c3\u8003\u4e0b\u65b9\u7a0b\u5f0f\u3002

"},{"location":"fundamental/cpp/1/hello_world/#_7","title":"\u8b80\u53d6\u4e00\u884c","text":"

\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff0c\u5176\u4e2dtemp2\u8acb\u6539\u6210\u8b8a\u6578\u7684\u540d\u7a31\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    string temp2;\n    getline(cin,temp2);\n    cout << temp2 << endl;\n    return 0;\n}\n
"},{"location":"fundamental/cpp/1/hello_world/#practice","title":"Practice","text":"

\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:

Info

  1. \u5982\u4f55\u4f7f\u7528 cout\u8207cin
  2. \u63a5\u89f8\u8b8a\u6578\u7684\u6982\u5ff5\u3002

\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002

ZeroJudge - a001. \u54c8\u56c9

Reference code
#include <iostream>\n\nint main(){\n    string word;\n    cin >> word;\n    cout << \"hello, \" << word << endl;\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/","title":"\u578b\u614b\u3001\u8b8a\u6578\u8207\u76f8\u95dc\u904b\u7b97","text":"

\u5728\u524d\u4e00\u7ae0\u4e2d\uff0c\u4f60\u61c9\u8a72\u6703\u5c0d\u8b8a\u6578\u6709\u76f8\u95dc\u7684\u6982\u5ff5\uff0c\u9019\u4e00\u7ae0\u6211\u5011\u5c07\u66f4\u6df1\u5165\u8a0e\u8ad6\u9019\u4e9b\u6771\u897f\u3002

"},{"location":"fundamental/cpp/2/#_2","title":"\u578b\u614b\u5ba3\u544a","text":"

\u6b63\u5982\u524d\u4e00\u7bc7\u6240\u8ff0\uff0c\u7576\u6211\u5011\u9700\u8981\u4f7f\u7528\u4e00\u500b\u8b8a\u6578\uff0c\u8981\u5148\u505a\u5ba3\u544a\uff0c\u505a\u5ba3\u544a\u7684\u65b9\u5f0f\u70ba:

<\u578b\u614b\u540d\u7a31> <\u8b8a\u6578\u540d\u7a31>;\n
\u4f8b\u5982\u4eca\u5929\u6211\u5011\u8981\u5ba3\u544a\u4e00\u500b\u6574\u6578\u578b\u614b\u7684\u8b8a\u6578\uff0c\u5c31\u8981\u9019\u6a23\u5beb:
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int number;\n    return 0;\n}\n

\u5982\u679c\u4eca\u5929\u8981\u6307\u5b9a\u4e00\u500b\u521d\u59cb\u503c\uff0c\u5247\u8981\u9019\u6a23\u5beb:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int number = 0;\n    return 0;\n}\n

\u5370\u51fa\u4e00\u500b\u6c92\u6709\u7d66\u503c\u7684\u8b8a\u6578

\u8b93\u6211\u5011\u8a66\u8457\u5370\u51fa\u5b83:

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int number;\n    cout << nunber << endl;\n    return 0;\n}\n
\u8acb\u591a\u57f7\u884c\u5e7e\u6b21\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u6bcf\u6b21\u57f7\u884c\u7684\u7d50\u679c\u597d\u50cf\u90fd\u6c92\u6709\u898f\u5247\u3002

\u9019\u662f\u56e0\u70ba\u5728\u5728c/c++\u4e2d\uff0c\u4e00\u500b\u8b8a\u6578\u5982\u679c\u6c92\u6709\u7d66\u521d\u59cb\u503c\uff0c\u90a3\u9ebc\u4ed6\u7684\u503c\u5c31\u662f\u672a\u5b9a\u7684\uff0c\u800c\u4e0d\u6703\u81ea\u52d5\u7d660\uff0c\u4ee5\u4e0a\u9762\u7684\u7a0b\u5f0f\u70ba\u4f8b\uff0c\u5982\u679c\u6c92\u6709\u7d66\u5b9a\u521d\u59cb\u503c\uff0c\u90a3\u4ed6\u6709\u53ef\u80fd\u662f1 \uff0c\u4e5f\u53ef\u80fd\u6703\u662f-10235\uff0c\u6240\u4ee5\u9664\u975e\u4f60\u975e\u5e38\u78ba\u5b9a\u9019\u500b\u8b8a\u6578\u5728\u5f8c\u9762\u6703\u99ac\u4e0a\u6539\u8b8a\u6210\u4e00\u500b\u53ef\u77e5\u7684\u72c0\u614b(\u50cf\u662fcin\u8f38\u5165\u9032\u53bb\u8b8a\u6578)\uff0c\u4e0d\u7136\u8acb\u4e00\u5f8b\u53c3\u8003\u6709\u7d66\u521d\u59cb\u503c\u7684\u5beb\u6cd5\uff0c\u4ee5\u907f\u514d\u51fa\u932f\u3002

"},{"location":"fundamental/cpp/2/#_3","title":"\u578b\u614b\u985e\u5225","text":"

\u4e00\u822c\u800c\u8a00\uff0c\u5e38\u4f7f\u7528\u7684\u578b\u614b\u6709\u53ef\u5206\u70ba\u4e94\u985e\uff0c\u5206\u5225\u70ba\u6574\u6578\u578b\u614b\u3001\u5c0f\u6578\u578b\u614b\u3001\u5b57\u5143\u578b\u614b\u3001\u9663\u5217\u578b\u614b\u3001\u548c\u5b57\u4e32\u578b\u614b\uff0c\u518d\u63a5\u4e0b\u4f86\u7684\u5e7e\u7bc7\u6587\u7ae0\uff0c\u6211\u5011\u5c07\u6703\u8457\u58a8\u65bc\u4ed6\u5011\u662f\u5982\u4f55\u5b58\u5728\u96fb\u8166\u7684\uff0c\u4ee5\u53ca\u4ed6\u5011\u7684\u76f8\u95dc\u7279\u6027\u3002

"},{"location":"fundamental/cpp/2/array/","title":"\u9663\u5217","text":"

\u5728\u524d\u9762\u7684\u7ae0\u7bc0\uff0c\u6211\u5011\u4e86\u89e3\u5982\u4f55\u5ba3\u544a\u8b8a\u6578\uff0c\u90a3\u8b93\u6211\u5011\u8a2d\u60f3\u4e00\u500b\u72c0\u6cc1\uff0c\u6211\u5011\u4eca\u5929\u8981\u5ba3\u544a100000\u500b\u8b8a\u6578\uff0c\u8a72\u4e0d\u6703\u53ea\u80fd\u9019\u6a23\u505a\u5427\uff1f

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a1,a2,a3,a4,......,a99999,a100000;\n\n    return 0;\n}\n

\u60f3\u4e5f\u77e5\u9053\u6211\u5011\u4e0d\u53ef\u80fd\u9019\u6a23\u4f5c\u5c0d\u5427\uff01 \u800c\u4e14\u9019\u6a23\u9084\u6709\u5ef6\u4f38\u4e00\u500b\u554f\u984c\uff0c\u8981\u5982\u4f55\u53d6\u5f97\u7b2cn\u500b\u8b8a\u6578\u7684\u503c\uff1f

"},{"location":"fundamental/cpp/2/array/#_2","title":"\u9663\u5217\u7684\u6982\u5ff5","text":"

\u5728\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5ba3\u544a\u4e00\u9023\u4e32\u7684\u8a18\u61b6\u9ad4\uff0c\u4f86\u7d66\u7a0b\u5f0f\u4f7f\u7528\uff0c\u6211\u5011\u7a31\u5176\u70ba\u9663\u5217\u3002

"},{"location":"fundamental/cpp/2/array/#_3","title":"\u4e00\u7dad\u9663\u5217","text":""},{"location":"fundamental/cpp/2/array/#_4","title":"\u5ba3\u544a","text":"

\u90a3\u8b93\u6211\u5011\u56de\u5230\u4e00\u958b\u59cb\u7684\u554f\u984c\uff0c\u5ba3\u544a100000\u500b\u8b8a\u6578\uff0c\u6211\u5011\u53ea\u8981\u9019\u6a23\u505a\uff0c\u4e26\u5b58\u53d6\u5b83\u3002

\u7b2c\u4e00\u7a2e\u5ba3\u544a\u65b9\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[0] = 1; //write into memory\n\n    cout << a[0] << endl;\n\n    return 0;\n}\n
\u7b2c\u4e8c\u7a2e\u5ba3\u544a\u65b9\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[] = {1,2,3,4,5};\n\n    cout << a[0] << endl;\n\n    return 0;\n}\n
\u8a3b\u89e3

\u9019\u908a//\u70ba\u8a3b\u89e3\uff0c\u5b83\u6703\u7701\u7565//\u5f8c\u9762\u7684\u5167\u5bb9\uff0c\u76f4\u5230\u4e0b\u884c\uff0c\u4e0d\u505a\u57f7\u884c\u3002\u540c\u6a23\u7684\u6771\u897f\u6709\u7528/* */\u5305\u7236\u7684\u7a0b\u5f0f\uff0c\u7528\u9019\u5169\u500b\u6771\u897f\u5305\u8986\u7684\u7247\u6bb5\u4e5f\u4e0d\u6703\u88ab\u57f7\u884c\uff0c\u4e0b\u65b9\u70ba\u7bc4\u4f8b\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[0] = 1; //write into memory\n\n    /*cout << a[0] << endl;*/\n\n    return 0;\n}\n
\u5e38\u898b\u7684\u932f\u8aa4

\u5982\u679c\u4f60\u6c92\u6709\u7d66\u5b9a\u503c\u5c31\u505a\u5b58\u53d6\uff0c\u90a3\u4f60\u4e5f\u6703\u9047\u5230\u672a\u7d66\u503c\u5f97\u76f8\u95dc\u932f\u8aa4\uff0c\u5982\u540c\u524d\u9762\u5c0d\u8b8a\u6578\u7684\u63cf\u8ff0\u4e00\u6a23\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[0] = 1; //write into memory\n\n    cout << a[1] << endl;\n\n    return 0;\n}\n

\u9019\u908a\u7684\u8f38\u51fa\u6703\u4e0d\u56fa\u5b9a\uff01

\u9019\u908a\u6709\u500b\u9ede\u9700\u8981\u6ce8\u610f\uff0c\u5b58\u53d6array\u90fd\u662f\u5f9e0\u958b\u59cb\u7684\uff0c\u4ee5\u4e0a\u65b9\u7684\u7a0b\u5f0f\u70ba\u4f8b\uff0c\u8981\u5b58\u53d6\u6700\u5f8c\u4e00\u500b\u9663\u5217\u5143\u7d20\u7684\u65b9\u6cd5\u70ba\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[99999] = 1; //write into memory\n\n    cout << a[99999] << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/array/#_5","title":"\u8d85\u51fa\u9663\u5217\u7bc4\u570d","text":"

\u4e0b\u65b9\u7684\u7a0b\u5f0f\u70ba\u8d85\u51fa\u9663\u5217\u61c9\u8a72\u5b58\u53d6\u7684\u7bc4\u570d \uff080~99999\uff09\uff0c\u6703\u6839\u64da\u4e0d\u540c\u7684\u4f5c\u696d\u7cfb\u7d71\uff08\u53ca\u7248\u672c\uff09\uff0c\u51fa\u73fe\u4e0d\u540c\u7684\u8655\u7406\u65b9\u5f0f\uff0c\u6709\u5831\u932f\u7684\uff0c\u6709\u4e7e\u8106\u4e0d\u8655\u7406\u7684\u3002

\u932f\u8aa4\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100000];\n\n    a[100000] = 1; //error\n\n    cout << a[100000] << endl; //error\n\n    return 0;\n}\n

\u8acb\u6ce8\u610f\uff0c\u9019\u500b\u932f\u8aa4\u5e38\u662f\u6211\u5011\u5beb\u7a0b\u5f0f\u7684\u8aa4\u5340\uff0c\u5728\u64b0\u5beb\u7a0b\u5f0f\u6642\uff0c\u8acb\u52d9\u5fc5\u78ba\u4fdd\u6703\u5b58\u53d6\u5230\u6709\u6548\u7684\u7bc4\u570d\u3002

"},{"location":"fundamental/cpp/2/array/#_6","title":"\u4e8c\u7dad\u9663\u5217","text":""},{"location":"fundamental/cpp/2/array/#_7","title":"\u5ba3\u544a","text":"

\u4e8b\u5be6\u4e0a\uff0c\u9663\u5217\u4e0d\u4fb7\u9650\u65bc\u4e00\u7dad\uff0c\u5b83\u4e5f\u53ef\u4ee5\u662f\u4e8c\u7dad\u7684\u72c0\u614b\uff0c\u4e0b\u65b9\u70ba\u7bc4\u4f8b\u7a0b\u5f0f\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100][100];\n\n    a[0][0] = 1; //write into memory\n\n    cout << a[0][0] << endl; \n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/array/#_8","title":"\u8d85\u51fa\u9663\u5217\u7bc4\u570d","text":"

\u932f\u8aa4\u7a0b\u5f0f

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    char a[100][100];\n\n    a[0][100] = 'a'; //write into memory\n\n    cout << a[1][0] << endl; // output not random, print 'a' here \n\n    return 0;\n}\n
\u6b64\u7a0b\u5f0f\u6703\u5370\u51fa a\uff0c\u800c\u4e0d\u662f\u6709\u76f8\u95dc\u932f\u8aa4\uff0c\u9019\u662f\u70ba\u4f55\uff1f

\u8a18\u4e0d\u8a18\u5f97\u524d\u9762\u6240\u8aaa\uff0c\u9663\u5217\u70ba\u4e00\u9023\u4e32\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u6240\u4ee5\u5b83\u5206\u914d\u4e86\u4e00\u9023\u4e32\u7a0b\u5f0f\u80fd\u7528\u7684\u7a7a\u9593\uff0c[0][100]\u6240\u5728\u7684\u5be6\u969b\u4f4d\u7f6e\u4f4d\u65bc[1][0]\uff0c\u6240\u4ee5\u624d\u4e0d\u6703\u6709\u56b4\u91cd\u7684\u932f\u8aa4\uff0c\u4e0d\u904e\u8acb\u6ce8\u610f\uff0c\u5e73\u6642\u64b0\u5beb\u6642\uff0c\u4e5f\u76e1\u91cf\u4e0d\u8981\u51fa\u73fe\u9019\u6a23\u7684\u932f\u8aa4\uff0c\u7562\u7adf\u6703\u51fa\u73fe\u9019\u6a23\u7684\u5beb\u6cd5\uff0c\u516b\u6210\u662f\u4f60\u5beb\u932f\u4e86\uff01

\u800c\u4e0b\u65b9\u7a0b\u5f0f\u5c31\u6703\u51fa\u73fe\u8d85\u51fa\u9663\u5217\u7684\u932f\u8aa4\uff0c\u56e0\u70ba\u5df2\u7d93\u8d85\u51fa\u80fd\u4f7f\u7528\u7684\u7bc4\u570d\uff1a

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[10][10];\n\n    a[10][10] = 1; //error\n\n    cout << a[10][10] << endl; //error\n\n    return 0;\n}\n

"},{"location":"fundamental/cpp/2/array/#_9","title":"\u591a\u7dad\u9663\u5217","text":""},{"location":"fundamental/cpp/2/array/#_10","title":"\u5ba3\u544a","text":"
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int a[100][100][10];\n\n    a[0][99][9] = 1; //write into memory\n\n    cout << a[0][99][9] << endl; \n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/array/#_11","title":"\u5e38\u898b\u932f\u8aa4","text":"

\u4f60\u6216\u8a31\u5728\u5176\u4ed6\u5730\u65b9\u770b\u904e\u9019\u6a23\u7684\u7a0b\u5f0f\uff0c\u7528\u65bc\u5ba3\u544a\u4e00\u500b\u4e0d\u56fa\u5b9a\u5927\u5c0f\u7684\u9663\u5217\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    int n;\n    cin >> n;\n    int arr[n];\n\n    return 0;\n}\n

\u7136\u800c\uff0c\u9019\u500b\u5beb\u6cd5\u5b58\u5728\u4e00\u500b\u554f\u984c\uff0c\u9019\u7a2e\u7a0b\u5f0f\u7de8\u8b6f\u5668\u901a\u5e38\u6703\u81ea\u52d5\u628aarr\u5206\u914d\u7684\u5927\u4e00\u9ede\uff0c\u56e0\u70ba\u7de8\u8b6f\u5668\u4e0d\u77e5\u9053n\u9577\u600e\u6a23\uff0c\u6240\u4ee5\u53ea\u597d\u914d\u4e00\u500b\u6709\u9ede\u5927\u7684\u5927\u5c0f\u7d66\u9019\u500b\u9663\u5217\uff0c\u6211\u5011\u6703\u5728\u672a\u4f86\u7684\u8a18\u61b6\u9ad4\u7bc7\u7ae0\u4f86\u8a73\u7d30\u6558\u8ff0\u70ba\u5565\u6703\u51fa\u73fe\u9019\u500b\u60c5\u6cc1\u3002

\u800c\u4f60\u5728\u9019\u908a\u8981\u77e5\u9053\uff0c\u9019\u500b\u5beb\u6cd5\uff0c\u4e0d\u4e00\u5b9a\u6703\u6b63\u78ba\u3002

"},{"location":"fundamental/cpp/2/char/","title":"\u5b57\u5143\u578b\u614b","text":"

\u5728\u524d\u9762\u7684\u7ae0\u7bc0\uff0c\u6211\u5011\u6709\u63d0\u5230char\u662f\u5b57\u5143\u578b\u614b\uff0c\u5b57\u5143\uff0c\u60f3\u5fc5\u662f\u8868\u793a\u5b57\u7684\uff0c\u90a3\u9019\u578b\u614b\u5230\u5e95\u662f\u600e\u6a23\u8868\u793a\u5b57\uff1f

"},{"location":"fundamental/cpp/2/char/#ascii","title":"ASCII","text":"

\u4ee5\u524d\u7f8e\u570b\u5b9a\u7fa9\u4e86\u4e00\u500b\u6578\u503c\u8f49\u63db\u6210\u5b57\u7684\u4e00\u5f35\u8868\uff0c\u4f5c\u70ba\u6a19\u6e96\uff0c\u6211\u5011\u7a31\u5176\u70baASCII\uff0c\u4e0b\u9762\u662f\u8a73\u7d30\u5167\u5bb9\u3002

\u7531\u65bc\u7528\u7684\u7bc4\u570d\u8f03\u5c0f\uff0cascii\u7de8\u78bc\u53ea\u9700\u89811byte\uff0c\u6240\u4ee5char\u9019\u578b\u614b\u70ba1byte\u578b\u614b\u3002

"},{"location":"fundamental/cpp/2/char/#_2","title":"\u975e\u82f1\u6587\u5b57\u5143","text":"

\u9019\u908a\u4f60\u80af\u5b9a\u767c\u73fe\u5230\uff0c\u9019\u908a\u53ea\u6709\u5b9a\u7fa9\u82f1\u6587\u5b57\u6bcd\uff0c\u4e26\u6c92\u6709\u4e2d\u6587\u5b57\uff0c\u5c0d\u7684\uff0c\u4e2d\u6587\u81ea\u6709\u53e6\u5916\u4e00\u5957\u7de8\u78bc\u6a19\u6e96\uff0c\u53ef\u80fd\u4f7f\u7528\u8d85\u904e1\u500bbyte\u5132\u5b58\u5b57\u5143\uff0c\u50cf\u662fBig-5\u963f\uff0c\u4ee5\u53ca\u73fe\u5728\u5927\u5bb6\u5e38\u7528\u7684Unicode\u842c\u570b\u78bc\uff0c\u8a73\u7d30\u5167\u5bb9\u9019\u908a\u4e0d\u505a\u904e\u591a\u8d05\u8ff0\u3002

"},{"location":"fundamental/cpp/2/char/#_3","title":"\u5ba3\u544a\u65b9\u5f0f","text":"
#include <iostream>\n\nint main(){\n\n    char A_test = 41;\n    char a_test = 'a';\n\n    cout << A << a << endl;\n\n    return 0;\n}\n
output
Aa\n

\u7b2c\u4e00\u500b\u65b9\u5f0f\u70ba\u76f4\u63a5\u7d66\u4e88\u5ba3\u544aascii\u7684\u503c\uff0ccout\u5728\u8f38\u51fa\u6642\uff0c\u6703\u628a\u5b83\u8f49\u6210ascii\u5c0d\u61c9\u7684\u5b57\u5143\uff0c\u4e5f\u5c31\u662fA\u3002

\u7b2c\u4e8c\u7a2e\u65b9\u5f0f\u70ba\u544a\u8a34\u7de8\u8b6f\u5668\u6211\u9019\u908a\u8981\u7d66\u7684\u503c\u70ba\u5b57\u5143\uff0c\u6211\u5011\u628a\u8981\u8f38\u5165\u7684\u5b57\u5143\u7528\u55ae\u5f15\u865f\\'\u5305\u8d77\u4f86\uff0c\u5c31\u544a\u8a34\u7de8\u8b6f\u5668\u9019\u662f\u5b57\u5143\uff0c\u800c\u8f38\u51fa\u6642\u6703\u628a\u9019\u500b\u5b57\u5143\u5370\u51fa\u4f86\u3002

\u6240\u4ee5\u7d50\u679c\u5c31\u5982\u4e0b\u65b9output\u6240\u8ff0\u3002

"},{"location":"fundamental/cpp/2/char/#_4","title":"\u8df3\u812b\u5b57\u5143","text":"

\u5728\u5b57\u4e32\u7576\u4e2d\uff0c\u6211\u5011\u7528\u4e86\"\"\u8868\u793a\u5b57\u4e32\u7684\u5167\u5bb9\u7bc4\u570d\uff0c\u4f46\u662f\u5982\u679c\u4eca\u5929\u6211\u5011\u60f3\u5370\"\uff0c\u8a72\u600e\u6a23\u505a\uff1f

\u4f7f\u7528\u8df3\u812b\u5b57\u5143\u963f\uff01

\u8df3\u812b\u5b57\u5143\u7bc4\u4f8b
#include <iostream>\n\nint main(){\n\n\n    cout << '\\\"' << endl;\n\n    return 0;\n}\n

\u9664\u4e86\u9019\u500b\uff0c\u9084\u6709\u4e0b\u9762\u9019\u4e9b\u4f8b\u5b50\uff1a

\u63db\u884c\u7bc4\u4f8b
#include <iostream>\n\nint main(){\n\n\n    cout << '\\n' << endl;\n\n    return 0;\n}\n
\u55ae\u5f15\u865f\u7bc4\u4f8b
#include <iostream>\n\nint main(){\n\n\n    cout << '\\'' << endl;\n\n    return 0;\n}\n

reference

  1. https://www.commfront.com/pages/ascii-chart
"},{"location":"fundamental/cpp/2/floating/","title":"\u6d6e\u9ede\u6578\u578b\u614b","text":"

\u5728\u524d\u9762\u7684\u7bc7\u7ae0\uff0c\u76f8\u4fe1\u4f60\u5c0d\u5c0f\u6578\u9ede\u7684\u5132\u5b58\u4e0d\u964c\u751f\u4e86\uff0c\u90a3\u5982\u679c\u4eca\u5929\u8981\u5b58\u7684\u662f\u6709\u5c0f\u6578\u9ede\u7684\u6578\u5b57\uff1f

\u5728\u5f88\u4e45\u5f88\u4e45\u5f88\u4e45\u4ee5\u524d\uff0c\u5c0f\u6578\u9ede\u7684\u5b58\u6cd5\u662f\u6c92\u6709\u6a19\u6e96\u7684\uff0c\u4f46\u7d93\u904e\u6642\u9593\u7684\u6f14\u8b8a\u904e\u5f8c\uff0c\u4eba\u5011\u767c\u73fe\u904e\u65bc\u6df7\u4e82\uff0c\u65bc\u662f\u5171\u540c\u5236\u5b9a\u4e86\u4e00\u500b\u5171\u540c\u7684\u5b58\u6cd5\uff0c\u800c\u9019\u500b\u6a19\u6e96\u5c31\u662f IEEE 754\u3002

"},{"location":"fundamental/cpp/2/floating/#_2","title":"\u79d1\u5b78\u8a18\u865f","text":"

\u5728\u8ac7\u5230IEEE 754\u7684\u6a19\u6e96\u524d\uff0c\u6211\u5011\u5148\u4f86\u8ac7\u4e00\u4e0b\u79d1\u5b78\u8a18\u865f\uff0c\u56e0\u70ba\u79d1\u5b78\u8a18\u865f\u7684\u8a31\u591a\u7cbe\u9ad3\u8207IEEE 754\u76f8\u8fd1\u3002

\u5982\u679c\u6211\u5011\u8981\u7528\u5fc5\u9808\u7528\u79d1\u5b78\u8a18\u865f\u8868\u793a\u4e00\u500b\u8b8a\u6578\uff0c\u5fc5\u7136\u6703\u662f\u9019\u7a2e\u5f62\u5f0f\u3002

\\[ -1001 = -1.001 * 10^3 \\] \\[ 0.001 = 1*10^{-3} \\]

\u9019\u662f\u5c0d\u65bc\u5341\u9032\u4f4d\u6642\u7684\u72c0\u6cc1\uff0c\u5982\u679c\u4eca\u5929\u662f\u4e8c\u9032\u4f4d\u7684\u8a71\uff0c\u4e8c\u9032\u4f4d\u7684\u79d1\u5b78\u8a18\u865f\u8981\u9577\u9019\u6a23\uff1a

\\[ 0.5_{10} = {1 * 2^{-1}} = 0.1_2\\]

\u800cIEEE 754\u5c31\u662f\u4f9d\u7167\u9019\u500b\u5f62\u5f0f\u5b9a\u7fa9\u7684\u3002

"},{"location":"fundamental/cpp/2/floating/#ieee_754","title":"IEEE 754","text":""},{"location":"fundamental/cpp/2/floating/#_3","title":"\u55ae\u7cbe\u5ea6","text":"

\u55ae\u7cbe\u5ea6\u4f7f\u75284byte\u505a\u5b58\u5132\uff0c\u7b2c\u4e00\u500bbit\u5132\u5b58\u6b63\u8ca0\u7684\u8cc7\u8a0a\uff0c\u63a5\u4e0b\u4f86\u76847\u500bbit\u5132\u5b58\u6307\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u7684^{-1}\\)\uff09\uff0c\u5269\u4e0b\u7684\u90e8\u4efd\u5132\u5b58\u5be6\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u76841\\)\uff09\u3002 \u4e0b\u65b9\u70ba\\(1.0*2^{-1}\\)\u5728\u8a18\u61b6\u9ad4\u4e2d\u7684\u72c0\u614b\u3002

0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

\u311f\uff0c\u9ec3\u8272\u7684\u90e8\u4efd\u61c9\u8a72\u70ba-1\u55ce\uff1f\u70ba\u4f55\u8f49\u63db\u621010\u9032\u4f4d\u70ba62?

\u9019\u662f\u56e0\u70ba\u898f\u7bc4\u898f\u5b9a\u4e86\uff0c\u9ec3\u8272\u5167\u7684\u503c\u9084\u8981\u518d\u526a\u4e0a\\(2^{7-1}-1=63\\)\u624d\u6703\u662f\u6211\u5011\u8981\u7684\u6307\u6578\uff0c\u9019\u4e03\u683c\u4e26\u4e0d\u7167\u8457\u524d\u4e00\u7ae0\u8ca0\u6578\u898f\u5247\u8d70(\u4e5f\u6c92\u6709\u5fc5\u8981)\u3002

\u63a5\u8457\u4f60\u61c9\u8a72\u6703\u6709\u7b2c\u4e8c\u500b\u7591\u554f\uff1f\u70ba\u4f55\u5269\u9918\u7684\u5be6\u6578\u90e8\u4efd\u7686\u70ba0\u3002

\u8b93\u6211\u5011\u56de\u60f3\u770b\u770b\u79d1\u5b78\u8a18\u865f\uff0c\u5176\u5be6\u5b83\u9084\u53ef\u4ee5\u7c21\u5316\u6210\\((1+a)*10^b\\space_{0 \\le a < 1}\\)\u9019\u500b\u5f62\u5f0f\uff0c\u800c\u6211\u5011\u5176\u5be6\u53ea\u8981\u5132\u5b58a\u7684\u90e8\u4efd\u5c31\u597d\u4e86\uff0c\u9019\u908a\u4e5f\u662f\u9019\u500b\u9053\u7406\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c0.0\u5728IEEE\u4e2d\u5b9a\u7fa9\u70ba\u5168\u90e8\u7684bit\u90fd\u70ba0\uff0c\u5982\u8868\u683c\u6240\u793a\uff1a

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

\u6d6e\u9ede\u6578\u7576\u4e2d\u9084\u6709\u4e00\u500b\u7279\u5225\u7684\u6578\uff0c\u6211\u5011\u7a31\u70ba\u5b83\u70baNot a Number\uff0c\u6216\u8005\u53c8\u53eb\u505aNaN\uff0c\u6839\u64da\u5b9a\u7fa9\uff0c\u70ba\u6240\u6709\u7684bit\u90fd\u70ba1\uff0c\u5982\u8868\u683c\u6240\u793a:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ..."},{"location":"fundamental/cpp/2/floating/#_4","title":"\u8aa4\u5dee","text":"

\u7d30\u5fc3\u7684\u4f60\u5728\u9019\u908a\u61c9\u8a72\u6703\u6ce8\u610f\u5230\uff0c\u6709\u4e9b\u6578\u5176\u5be6\u662f\u4e0d\u80fd\u88ab\u6709\u9650\u7684\u4e8c\u9032\u4f4d\u5c0f\u6578\u8868\u793a\u7684\uff0c\u50cf\u662f0.3\uff0c\u8f49\u6210\u4e8c\u9032\u4f4d\u7684\u8a71\u6703\u662f\u7121\u9650\u5c0f\u6578,\u800c\u9019\u7a2e\u5b58\u6cd5\u662f\u6c92\u8fa6\u6cd5\u5b58\u7121\u9650\u591a\u500b\u5c0f\u6578\u7684\uff0c\u6240\u4ee5\u67d0\u4e9b\u4f4d\u5143\u5f8c\u52e2\u5fc5\u6703\u88ab\u6368\u68c4\uff0c\u800c\u9020\u6210\u8207\u8f38\u5165\u4e0d\u4e00\u6a23\uff0c\u9020\u6210\u8aa4\u5dee\u7684\u767c\u751f\uff0c\u4e0b\u65b9\u70ba\u9019\u985e\u8aa4\u5dee\u7684\u6700\u7d93\u5178\u554f\u984c\u76f8\u95dc\u9023\u7d50\uff1a

(0.1+0.2=0.30000000000000004)

\u7e3d\u800c\u8a00\u4e4b\uff0c\u7121\u8ad6\u5982\u4f55\u8acb\u8a18\u4f4f\u9019\u53e5\u8a71:

\u7b97\u9322\u7528\u6d6e\u9ede\uff0c\u9072\u65e9\u88ab\u4eba\u6241\uff01

\u90a3\u6709\u54ea\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u5176\u7b97\u7684\u66f4\u6e96\u4e00\u4e9b\uff1f

\u52a0\u5927\u7cbe\u6e96\u5ea6\uff0c\u800c\u4e8b\u5be6\u4e0a\uff0c\u6211\u5011\u53ef\u4ee5\u52a0\u5927\u4ed6\u7684\u7cbe\u6e96\u5ea6\u3002

"},{"location":"fundamental/cpp/2/floating/#_5","title":"\u96d9\u7cbe\u5ea6","text":"

\u55ae\u7cbe\u5ea6\u4f7f\u75284byte\u505a\u5132\u5b58\uff0c\u7b2c\u4e00\u500bbit\u5132\u5b58\u6b63\u8ca0\u7684\u8cc7\u8a0a\uff0c\u63a5\u4e0b\u4f86\u76847+4=11\u500bbit\u5132\u5b58\u6307\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u7684^{-1}\\)\uff09\uff0c\u5269\u4e0b\u7684\u90e8\u4efd\u5132\u5b58\u5be6\u6578\u7684\u8cc7\u8a0a\uff08\u4f8b\u5982\uff1a\\(1.0*2^{-1}\u76841.0\\)\uff09\u3002 \u4e0b\u65b9\u70ba\\(1*2^{-1}\\)\u5728\u8a18\u61b6\u9ad4\u4e2d\u7684\u72c0\u614b\u3002

0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 ...

\u9ec3\u8272\u7684\u90e8\u4efd\u8f49\u621010\u9032\u4f4d\u70ba1022\uff0c\u518d\u526a\u4e0a\u526a\u4e0a\\(2^{11-1}-1=1023\\)\uff0c\u6703\u7b49\u65bc-1\u3002

"},{"location":"fundamental/cpp/2/floating/#_6","title":"\u7e3d\u7d50","text":"

\u4f9d\u64daIEEE 754\u7684\u898f\u7bc4\uff0c\u6d6e\u9ede\u6578\u7684\u503c\u6703\u7b49\u65bc

\\[ \u6b63\u8ca0\uff08\u7d05\u8272\u7684\u90e8\u4efd\uff09* (1.\u85cd\u8272\u5f97\u90e8\u4efd)*2^(\u9ec3\u8272\u7684\u90e8\u4efd\u526a\u4e0a\u7279\u5b9a\u503c)\\]"},{"location":"fundamental/cpp/2/floating/#cc","title":"C/C++\u4e2d\u7684\u6d6e\u9ede\u6578","text":"

C/C++\u9664\u4e86\u63d0\u4f9b\u4e86\u5169\u7a2e\u7cbe\u5ea6\u7684\u6d6e\u9ede\u6578\uff0c\u5206\u5225\u70bafloat\u548cdouble\uff0c\u5206\u5225\u5c0d\u61c9\u55ae\u7cbe\u5ea6\u8207\u96d9\u7cbe\u5ea6\uff0c\u9084\u6709\u63d0\u4f9b\u66f4\u5927\u7bc4\u570d\u7684\u7cbe\u5ea6\uff0c\u4e00\u822c\u800c\u8a00\uff0cdouble\u5c31\u5df2\u7d93\u5f88\u5920\u7528\u4e86\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u66f4\u5927\u7684\u7cbe\u6e96\u5ea6\uff0c\u6216\u8005\u66f4\u5927\u7684\u7bc4\u570d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528long double\u3002

reference

  1. https://en.cppreference.com/w/cpp/language/types#Range_of_values
"},{"location":"fundamental/cpp/2/interger/","title":"\u6574\u6578","text":"

\u5728\u96fb\u8166\u7684\u4e16\u754c\u4e2d\uff0c\u6211\u5011\u4f7f\u75280\u548c1\u4f86\u5132\u5b58\u8cc7\u6599\uff0c\u6211\u5011\u628a\u6b64\u7a31\u70ba1\u500bbit\uff0c\u6709\u4e86\u4e00\u9023\u4e32\u7684bit\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u4e8c\u9032\u4f4d\u4f86\u5132\u5b58\u4e00\u500b\u6578\u5b57\u3002

\u800c\u96fb\u8166\u4e2d\u7684\u57fa\u672c\u55ae\u4f4d\u70ba 8bits \uff0c\u6211\u5011\u7a31\u5176\u70ba1\u500bbyte\u3002

\u4e86\u89e3\u5230\u4e86\u9019\u4e9b\u57fa\u672c\u77e5\u8b58\uff0c\u8b93\u6211\u5011\u770b\u770b\u6574\u6578\u662f\u5982\u4f55\u5b58\u7684\u3002

"},{"location":"fundamental/cpp/2/interger/#_2","title":"\u6b63\u6574\u6578","text":"

\u6b63\u5982\u524d\u9762\u6240\u8aaa\uff0c\u6211\u5011\u628a\u4e00\u500b\u6578\u5b57\u5f9e\u5341\u9032\u4f4d\u8f49\u6210\u4e8c\u9032\u4f4d\u5f8c\uff0c\u5c31\u53ef\u4ee5\u653e\u9032\u56fa\u5b9a\u5927\u5c0f\u7684\u8a18\u61b6\u9ad4\u5b58\u4e86\uff0c\u4e0b\u9762\u70ba\u5177\u9ad4\u904e\u7a0b\u3002

"},{"location":"fundamental/cpp/2/interger/#_3","title":"\u6ea2\u4f4d","text":"

\u5047\u5982\u4eca\u5929\u8981\u5b58\u7684\u6578\u653e\u9032\u56fa\u5b9a\u5927\u5c0f\u7684\u8a18\u61b6\u9ad4\u592a\u5927\u4e86\uff0c\u5b58\u4e0d\u4e0b\u600e\u8fa6\uff1f

\u4e1f\u6389\u4e0d\u7ba1\u5c31\u597d\u4e86\u963f\uff01

\u8b8a\u6578\u7684\u503c\u5982\u679c\u8d85\u904e\u8a18\u61b6\u9ad4\u80fd\u5b58\u7684\u4e0a\u9650\uff0c\u5247\u7a31\u70ba\u6ea2\u4f4d\uff0c\u6ea2\u4f4d\u5728\u6578\u503c\u904b\u7b97\u4e0a\u662f\u4e00\u500b\u7cbe\u5999\u7684\u8a2d\u8a08\uff0c\u5728\u7b49\u7b49\u7684\u7bc4\u4f8b\u6703\u5728\u8aaa\u660e\u3002

"},{"location":"fundamental/cpp/2/interger/#_4","title":"\u8ca0\u6578","text":"

\u7d93\u904e\u524d\u9762\u7684\u5f71\u7247\u7bc4\u4f8b\uff0c\u60f3\u5fc5\u4f60\u5c0d\u6b63\u6574\u6578\u7684\u5132\u5b58\u6709\u4e00\u5b9a\u7684\u6982\u5ff5\uff0c\u90a3\u5982\u679c\u4eca\u5929\u8981\u5b58\u7684\u662f\u8ca0\u6578\uff0c\u8981\u600e\u6a23\u505a\uff1f

\u5728\u8ac7\u53ca\u600e\u6a23\u505a\u4e4b\u524d\uff0c\u6211\u5011\u5148\u8ac7\u4e00\u4e0b\u88dc\u6578\u7684\u6982\u5ff5\u3002

"},{"location":"fundamental/cpp/2/interger/#_5","title":"\u4e00\u88dc\u6578","text":"

\u5728\u96fb\u8166\u7576\u4e2d\uff0c\u6211\u5011\u8b93\u6700\u5de6\u908a\u7684bit\u4ee3\u8868\u8457\u6b63\u8ca0\u3002\u5982\u679c\u662f0\u4ee3\u8868\u6b64\u6578\u70ba\u6b63\uff0c\u5982\u679c\u70ba\u8ca0\u5247\u5176\u9918bit 0 -> 1 \u3001 1 -> 0\uff0c\u4f8b\u5982\uff1a

\\[121_{10} = 01111110_{2}\\] \\[-121_{10} = 100000001_{2}\\]"},{"location":"fundamental/cpp/2/interger/#_6","title":"\u4e8c\u88dc\u6578","text":"

\u4e8c\u88dc\u6578\u7684\u7279\u6027\u8207\u4e00\u88dc\u6578\u76f8\u8fd1\uff0c\u552f\u4e00\u7684\u5340\u5225\u662f\u7576\u4ed6\u662f\u8ca0\u6578\u6642\uff0c\u9700\u8981\u5728\u8f49\u6210\u4e00\u88dc\u6578\u5f8c+1\uff0c\u4f8b\u5982\uff1a

\\[121_{10} = 01111110_{2''}\\] \\[-121_{10} = 10000010_{2''}\\] \\[\uff08\u5047\u8a2d\u6211\u5011\u53ea\u67091byte=8bits\u4f86\u5b58\u8cc7\u6599\uff09\\]

\u63a5\u4e0b\u4f86\u4f60\u61c9\u8a72\u6703\u6709\u4e00\u500b\u7591\u554f\uff0c\u70ba\u4f55\u6211\u5011\u9700\u8981\u4e8c\u88dc\u6578\uff1f\u9019\u4e0d\u662f\u591a\u4e00\u500b\u6b65\u9a5f\u55ce\uff1f \u8b93\u6211\u5011\u8a66\u8457\u628a 121 \u548c -121 \u7684\u4e8c\u88dc\u6578\u76f8\u52a0\uff0c\u4e26\u5c0e\u5165\u6ea2\u4f4d\u7684\u6982\u5ff5\uff1a

\\[ 01111110_{2''} + 10000010_{2''} = _{1}00000000_{2''}\\]

\u6700\u5de6\u908a\u76841\u767c\u751f\u6ea2\u4f4d\uff0c\u6703\u6d88\u5931\uff08\u6545\u4f7f\u5176\u5b57\u9ad4\u7e2e\u5c0f\uff09\uff0c\u7d50\u679c\u70ba\u6b63\u78ba\u7b54\u6848\uff0c\u795e\u5947\u5c0d\u5427\uff01

\u4e8b\u5be6\u4e0a\uff0c\u96fb\u8166\u4e2d\u6211\u5011\u53ea\u6709\u8a2d\u8a08\u52a0\u6cd5\u5668\uff0c\u4e26\u6c92\u6709\u8a2d\u8a08\u6e1b\u6cd5\u5668\uff0c\u5c31\u662f\u56e0\u70ba\u6211\u5011\u53ef\u4ee5\u900f\u904e\u4e8c\u88dc\u6578+\u6ea2\u4f4d\u9019\u5169\u500b\u5de7\u5999\u7684\u8a2d\u8a08\uff0c \u4f86\u9054\u6210\u6574\u6578\u7684\u5feb\u901f\u904b\u7b97\u3002

"},{"location":"fundamental/cpp/2/interger/#c","title":"C++\u4e2d\u7684\u6574\u6578\u578b\u614b","text":"

\u5728\u4e86\u89e3\u5230\u9019\u4e9b\u80cc\u5f8c\u77e5\u8b58\u5f8c\uff0c\u6211\u5011\u4f86\u8a0e\u8ad6C++\u6240\u6709\u7684\u6574\u6578\u578b\u614b\u3002 \u7531\u65bc\u6bcf\u500b\u8b8a\u6578\u6240\u9700\u8981\u7684\u7bc4\u570d\u4e0d\u540c\uff0cC++\u7279\u5225\u8a2d\u8a08\u4e86\u597d\u5e7e\u7a2e\u7684\u578b\u614b\uff0c\u4f86\u8b93\u4f60\u4f7f\u7528\u3002

\u800c\u6bcf\u500b\u578b\u614b\u7684\u7bc4\u570d\uff0c\u7c21\u5316\u904e\u5f8c\u5927\u81f4\u4e0a\u53ef\u4ee5\u5206\u6210\u4e0b\u9762\u5e7e\u500b\u72c0\u6cc1\uff08\u8a73\u7d30\u8acb\u53c3\u8003reference\u7684\u9023\u7d501\uff09:

type 32 bits compiler 64 bits compiler unsigned char \\(-2^7\\sim2^7-1\\) \\(-2^7\\sim2^7-1\\) unsigned short \\(-2^{15}\\sim2^{15}-1\\) \\(-2^{15}\\sim2^{15}-1\\) unsigned int \\(-2^{31}\\sim2^{31}-1\\) \\(-2^{31}\\sim2^{31}-1\\) unsigned long \\(-2^{31}\\sim2^{31}-1\\) \\(-2^{63}\\sim2^{63}-1\\) unsigned long long \\(-2^{63}\\sim2^{63}-1\\) \\(-2^{63}\\sim2^{63}-1\\)

\u4e00\u822c\u800c\u8a00\uff0c\u5728\u4f7f\u7528\u6574\u6578\u578b\u614b\uff0c\u6211\u5011\u6703\u5ba3\u544aint\u505a\u4f7f\u7528\u3002 \u90a3C++\u540c\u6642\u4e5f\u63d0\u4f9b\u6c92\u6709\u8ca0\u6578\u7684\u578b\u614b\uff0c\u53ea\u8981\u5728\u9019\u4e9b\u578b\u614b\u524d\u9762\u52a0\u4e0aunsigned\u5373\u53ef\u3002

type 32 bits compiler 64 bits compiler char \\(0\\sim2^8-1\\) \\(0\\sim2^{8}-1\\) short \\(0\\sim2^{16}-1\\) \\(0\\sim2^{16}-1\\) int \\(0\\sim2^{32}-1\\) \\(0\\sim2^{32}-1\\) long \\(0\\sim2^{32}-1\\) \\({0}\\sim2^{64}-1\\) long long \\({0}\\sim2^{64}-1\\) \\({0}\\sim2^{64}-1\\)

\u9019\u908a\u6709\u500b\u9ede\u8981\u6ce8\u610f\u4e00\u4e0b\uff0c\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u6cc1\u4e0b\uff0c\u4f60\u7684\u7de8\u8b6f\u5668\u53ef\u80fd\u6703\u662f32\u4f4d\u5143\u7684\uff0clong\u7684\u5927\u5c0f\u6703\u6bd4\u8f03\u5c0f\uff0c\u6240\u4ee5\u5728\u5ba3\u544a8byte(64bits)\u5927\u5c0f\u7684\u6574\u6578\u578b\u614b\u6642\uff0c\u8acb\u76e1\u91cf\u4f7f\u7528long long\uff0c\u4ee5\u907f\u514d\u51fa\u932f\u3002

* \u8acb\u6ce8\u610f\uff0cchar\u540c\u6642\u70ba\u5b57\u5143\u578b\u614b\uff0c\u8a73\u7d30\u6703\u5728\u5f8c\u9762\u8aaa\u660e\u3002

"},{"location":"fundamental/cpp/2/interger/#_7","title":"\u6574\u6578\u578b\u614b\u7684\u76f8\u95dc\u904b\u7b97","text":"

C++ \u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u56db\u5247\u904b\u7b97\u3002

+ \u4ee3\u8868\u52a0\u865f\u3002

-\u4ee3\u8868\u6e1b\u6cd5\u904b\u7b97\u3002

*\u4ee3\u8868\u4e58\u6cd5\u904b\u7b97\u3002

/\u4ee3\u8868\u9664\u6cd5\u904b\u7b97\u3002 \u8acb\u6ce8\u610f\uff0c\u6574\u6578\u7684\u9664\u6cd5\u4e0d\u6703\u6709\u5c0f\u6578\u9ede\u3002

%\u4ee3\u8868\u53d6\u9918\u6578\u3002

\u8acb\u8a66\u8457\u57f7\u884c\u4e0b\u65b9\u7bc4\u4f8b\u7a0b\u5f0f\uff0c\u4e26\u5617\u8a66\u53bb\u7406\u89e3\u5b83\u3002

#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a = 126, b = -126;\n    int ans1 = a + b;\n    int ans2 = a - b ;\n    int ans3 = a * b * a * b * a * b ;\n    int ans4 = a / b;\n    int ans5 = (a*b)%2;\n\n    cout << ans1 << \" \" << ans2 << \" \"  << ans3 << \" \"  << ans4 << \" \"  << ans5 << endl;\n\n\n    return 0;\n}\n
reference
  1. https://en.cppreference.com/w/cpp/language/types
"},{"location":"fundamental/cpp/2/practice/","title":"\u7df4\u7fd2","text":"

\u5728\u7d93\u904e\u524d\u9762\u7684\u7bc7\u7ae0\uff0c\u76f8\u4fe1\u4f60\u5c0d\u8b8a\u6578\u578b\u614b\u5df2\u7d93\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\uff01\u63a5\u4e0b\u4f86\u6211\u5011\u5c07\u7df4\u7fd2\u76f8\u95dc\u984c\u76ee\uff0c\u4ee5\u78ba\u4fdd\u4f60\u771f\u7684\u6709\u5b78\u9032\u53bb\uff01

\u5148\u8a66\u8457\u5beb\u5beb\u770b\uff0c\u4e0d\u6703\u7684\u5148\u8a66\u8457\u900f\u904e\u7db2\u8def\u67e5\u8a62\u4e26\u7406\u89e3\uff0c\u771f\u7684\u4e0d\u884c\u6216\u8005\u5beb\u5b8c\u518d\u4f86\u770b\u8a73\u89e3\uff01

"},{"location":"fundamental/cpp/2/practice/#_2","title":"\u7c21\u55ae\u52a0\u6cd5","text":"\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a, b;\n    cin >> a >> b;\n    cout << a + b << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/practice/#_3","title":"\u7cdf\u7cd5\uff0c\u6211\u767c\u71d2\u4e86\uff01","text":"

\u9019\u908a\u7684fixed\u662f\u5f37\u5236cout\u4f7f\u7528\u5c0f\u6578\u9ede\u8f38\u51fa\u6d6e\u9ede\u6578\uff0csetprecision(3) \u5247\u662f\u544a\u8a34cout\uff0c\u5370\u5230\u5c0f\u6578\u9ede\u4e0b\u7b2c\u4e09\u4f4d\uff01

\u800c\u7b2c11\u884c\u7684 (double)(n-32)\uff0c\u4ee3\u8868\u5f37\u5236\u8f49\u578b\uff0c\u628an-32\u63db\u6210\u6d6e\u9ede\u6578\u7684\u578b\u614b\uff0c\u8b93\u5f8c\u9762\u7684\u8a08\u7b97\u6703\u6709\u5c0f\u6578\u9ede\uff0c\u9019\u908a\u8acb\u8a18\u4f4f\uff0c\u6574\u6578\u7684\u56db\u5247\u904b\u7b97\u53ea\u6703\u6709\u6574\u6578\uff0c\u800c\u6d6e\u9ede\u6578\u7684\u56db\u5247\u904b\u7b97\u4e00\u6a23\u4e5f\u53ea\u6709\u6d6e\u9ede\u6578\uff01\uff0c\u6574\u6578\u9664\u4e0a\u4efb\u4f55\u4e00\u500b\u6574\u6578\uff0c\u662f\u4e0d\u53ef\u80fd\u51fa\u73fe\u6d6e\u9ede\u6578\u7684\uff01

\u7bc4\u4f8b\u7a0b\u5f0f
// https://zerojudge.tw/ShowProblem?problemid=d051\n\n#include<iostream>\n#include<iomanip>\n\nusing namespace std;\n\nint main(){\n    int n;\n    cin >> n;\n    double ans = (double)(n-32) * 5 / 9;\n    cout<< fixed\n        << setprecision(3) \n        << ans\n        << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/practive/","title":"\u7df4\u7fd2","text":"

\u5728\u7d93\u904e\u524d\u9762\u7684\u7bc7\u7ae0\uff0c\u76f8\u4fe1\u4f60\u5c0d\u8b8a\u6578\u578b\u614b\u5df2\u7d93\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\uff01\u63a5\u4e0b\u4f86\u6211\u5011\u5c07\u7df4\u7fd2\u76f8\u95dc\u984c\u76ee\uff0c\u4ee5\u78ba\u4fdd\u4f60\u771f\u7684\u6709\u5b78\u9032\u53bb\uff01

\u5148\u8a66\u8457\u5beb\u5beb\u770b\uff0c\u4e0d\u6703\u7684\u5148\u8a66\u8457\u900f\u904e\u7db2\u8def\u67e5\u8a62\u4e26\u7406\u89e3\uff0c\u771f\u7684\u4e0d\u884c\u6216\u8005\u5beb\u5b8c\u518d\u4f86\u770b\u8a73\u89e3\uff01

"},{"location":"fundamental/cpp/2/practive/#_2","title":"\u7c21\u55ae\u52a0\u6cd5","text":"\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n    int a, b;\n    cin >> a >> b;\n    cout << a + b << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/practive/#_3","title":"\u7cdf\u7cd5\uff0c\u6211\u767c\u71d2\u4e86\uff01","text":"

\u9019\u908a\u7684fixed\u662f\u5f37\u5236cout\u4f7f\u7528\u5c0f\u6578\u9ede\u8f38\u51fa\u6d6e\u9ede\u6578\uff0csetprecision(3) \u5247\u662f\u544a\u8a34cout\uff0c\u5370\u5230\u5c0f\u6578\u9ede\u4e0b\u7b2c\u4e09\u4f4d\uff01

\u800c\u7b2c11\u884c\u7684 (double)(n-32)\uff0c\u4ee3\u8868\u5f37\u5236\u8f49\u578b\uff0c\u628an-32\u63db\u6210\u6d6e\u9ede\u6578\u7684\u578b\u614b\uff0c\u8b93\u5f8c\u9762\u7684\u8a08\u7b97\u6703\u6709\u5c0f\u6578\u9ede\uff0c\u9019\u908a\u8acb\u8a18\u4f4f\uff0c\u6574\u6578\u7684\u56db\u5247\u904b\u7b97\u53ea\u6703\u6709\u6574\u6578\uff0c\u800c\u6d6e\u9ede\u6578\u7684\u56db\u5247\u904b\u7b97\u4e00\u6a23\u4e5f\u53ea\u6709\u6d6e\u9ede\u6578\uff01\uff0c\u6574\u6578\u9664\u4e0a\u4efb\u4f55\u4e00\u500b\u6574\u6578\uff0c\u662f\u4e0d\u53ef\u80fd\u51fa\u73fe\u6d6e\u9ede\u6578\u7684\uff01

\u7bc4\u4f8b\u7a0b\u5f0f
// https://zerojudge.tw/ShowProblem?problemid=d051\n\n#include<iostream>\n#include<iomanip>\n\nusing namespace std;\n\nint main(){\n    int n;\n    cin >> n;\n    double ans = (double)(n-32) * 5 / 9;\n    cout<< fixed\n        << setprecision(3) \n        << ans\n        << endl;\n\n    return 0;\n}\n
"},{"location":"fundamental/cpp/2/string/","title":"\u5b57\u4e32","text":""},{"location":"fundamental/cpp/2/string/#_2","title":"\u5b57\u5143\u9663\u5217","text":"

\u4e00\u6bb5\u5b57\uff0c\u662f\u8907\u6578\u500b\u5b57\u6bcd\uff08\u6216\u7a31\uff1a\u5b57\u5143\uff09\u6240\u7d44\u6210\uff0c\u6240\u4ee5\u6211\u5011\u8981\u986f\u793a\u4e00\u6bb5\u5b57\uff0c\u53ef\u4ee5\u5ba3\u544a\u5b57\u5143\u9663\u5217\u4f86\u5132\u5b58\u3002

\u5b57\u5143\u9663\u5217\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    char arr[] = {'g','o','o','d'};\n\n    cout << arr << endl;\n\n    return 0;\n}\n
Output
good\n
"},{"location":"fundamental/cpp/2/string/#_3","title":"\u5b57\u4e32","text":"

\u5728C++\u4e2d\uff0c\u5b57\u4e32\u4f86\u7684\u6bd4\u5b57\u5143\u9663\u5217\u597d\u7528\uff0c\u4ed6\u8207\u5b57\u5143\u9663\u5217\u76f8\u4f3c\uff0c\u4f46\u6bd4\u5b57\u5143\u9663\u5217\u597d\u7528\u8a31\u591a\uff01

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u4e32\u7684\u7d50\u5c3e\uff0c\u5fc5\u5b9a\u662f\u7528\u8df3\u812b\u5b57\u5143\\0\u505a\u7d50\u5c3e\uff0c\u7528\u4ee5\u8868\u793a\u9019\u908a\u662f\u5b57\u4e32\u7684\u6700\u5f8c\u9762\u3002

H e l l o W o r l d \\0

\u4e0b\u65b9\u70ba\u76f8\u95dc\u7a0b\u5f0f\u7684\u7bc4\u4f8b\uff1a

\u5b57\u4e32\u7bc4\u4f8b\u7a0b\u5f0f
#include <iostream>\n\nusing namespace std;\n\nint main(){\n\n    string str = \"Hello World\";\n\n    a[0] = 1; //write into memory\n\n    cout << \"Size: \" << str.size() << endl; // \u8b80\u53d6\u5b57\u4e32\u6709\u591a\u5c11\u5b57\n    cout << \"str[11]: \" << str[11] << endl; // \u5b58\u53d6\u5b57\u4e32\u7684\u7b2c11\u500b\u5b57\u5143\uff0c\u4e5f\u5c31\u662f\u8df3\u812b\u5b57\u5143'\\0'\n    cout << \"str[0]: \" << str[0] << endl; // \u5b58\u53d6\u5b57\u4e32\u7684\u7b2c0\u500b\u5b57\u5143\n\n    return 0;\n}\n
Output
11\n\nH\n
"},{"location":"fundamental/python/","title":"Python","text":"

\u55e8\uff0c\u6211\u662f cheung4843\uff0c\u662f Python \u7cfb\u5217\u7684\u4f5c\u8005\u3002\u5728\u4e00\u958b\u59cb\u57fa\u790e\u8a9e\u6cd5\u7684\u90e8\u5206\u6703\u6559\u5f97\u7279\u5225\u8a73\u7d30\uff0c\u6587\u7ae0\u4e2d\u7684\u5c08\u6709\u540d\u8a5e\u5c07\u6703\u4ee5\u9ec3\u8272\u8207\u7da0\u8272\u6a19\u793a\uff0c\u8acb\u4f60\u7a0d\u5fae\u6ce8\u610f\u4e00\u4e0b\uff0c\u82e5\u4e0d\u61c2\u4e5f\u6c92\u95dc\u4fc2\uff0c\u6709\u500b\u5927\u6982\u7684\u6982\u5ff5\u5c31\u597d\u3002

\u5728\u6bcf\u4e00\u7ae0\u7684\u6700\u5f8c\u90fd\u6703\u653e\u4e0a\u4e00\u4e9b\u7df4\u7fd2\u984c\uff0c\u8acb\u5617\u8a66\u5b8c\u6210\u4ed6\u5011!

\u4f60\u6703\u5728\u6587\u7ae0\u4e2d\u770b\u5230\u5f88\u591a\u6b4c\uff0c\u9019\u4e9b\u591a\u534a\u662f\u6211\u5728\u64b0\u5beb\u6587\u7ae0\u7684\u6642\u5019\u525b\u597d\u5728\u807d\u7684\u6b4c\u3002

Tears For Fears - Everybody Wants To Rule The World

\u56e0\u70ba\u9019\u500b\u5c08\u6848\u6b63\u5728\u65e9\u671f\u958b\u767c\u968e\u6bb5\uff0c\u6240\u4ee5\u52d5\u756b\u8207\u5716\u7247\u5c07\u6703\u5728\u672a\u4f86\u52a0\u5165\u3002

\u6700\u5f8c\uff0c\u795d\u4f60\u5b78\u7fd2\u6109\u5feb!

"},{"location":"fundamental/python/classes/","title":"Classes","text":""},{"location":"fundamental/python/classes/#introduction","title":"Introduction","text":"

\u56de\u60f3 Variable and Input - Variable \u4e2d\u6240\u8b1b\u7684:

Quote

\u5728 Python \u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u7269\u4ef6\u3002

\u5728 Python \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u5df1\u5b9a\u7fa9\u4e00\u500b\u65b0\u7684\u7269\u4ef6\uff0c\u9019\u500b\u7269\u4ef6\u7684\u5b9a\u7fa9\u65b9\u5f0f\u5c31\u662f\u985e\u5225(Class)\uff0c\u5b83\u5c31\u50cf\u662f\u4e00\u500b\u85cd\u5716\uff0c\u53ef\u4ee5\u7528\u4f86\u5275\u5efa\u7269\u4ef6\u3002\u800c\u4f9d\u6b64\u5275\u9020\u51fa\u7684\u7269\u4ef6\u5c31\u662f\u5be6\u9ad4(Instance)\u3002

"},{"location":"fundamental/python/classes/#define_a_class","title":"Define a Class","text":"

\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u60f3\u8981\u5b9a\u7fa9\u4e00\u500b\u985e\u5225 Ship\uff0c\u64c1\u6709\u5c6c\u6027(Attribute) hp, name, speed\uff0c\u4ee5\u53ca\u65b9\u6cd5(Method) move \u548c stop\u3002

class Ship:\n    # Class attribute\n    max_no_flags = 3\n\n    def __init__(self, hp, name, speed):\n        # Instance attribute\n        self.hp = hp\n        self.name = name\n        self.speed = speed\n\n    # Method\n    def move(self):\n        print(f\"{self.name} is moving at speed {self.speed}.\")\n\n    def stop(self):\n        self.speed = 0\n        print(f\"{self.name} has stopped.\")\n\n\n# Create instances\nTitanic = Ship(120, \"Titanic\", 21)\nCarpathia = Ship(100, \"RMS Carpathia\", 14)\n# Access class attributes\nprint(Ship.max_no_flags)\nprint(Titanic.max_no_flags)\nShip.max_no_flags = 4\nprint(Carpathia.max_no_flags)\n# Access instance attributes\nprint(Titanic.name)\nprint(Carpathia.name)\n# Call methods\nTitanic.move()\nCarpathia.stop()\n
Output
3\n3\n4\nTitanic\nRMS Carpathia\nTitanic is moving at speed 21.\nRMS Carpathia has stopped.\n

C\u00e9line Dion - My Heart Will Go On

max_no_flags \u662f\u4e00\u500b\u985e\u5225\u5c6c\u6027(Class attribute)\uff0c\u5b83\u662f\u5c6c\u65bc\u985e\u5225 Ship \u7684\uff0c\u53ea\u8981\u662f Ship \u7684\u5be6\u9ad4\u90fd\u53ef\u4ee5\u5b58\u53d6\uff0c\u7136\u800c\u53ea\u8981 Ship \u4fee\u6539\u4e86\u5b83\uff0c\u6240\u6709\u7684\u5be6\u9ad4\u90fd\u6703\u53d7\u5230\u5f71\u97ff\u3002

hp, name, speed \u662f\u5be6\u9ad4\u5c6c\u6027(Instance attribute)\uff0c\u5b83\u5011\u662f\u5c6c\u65bc Ship \u7684\u5be6\u9ad4\uff0c\u6bcf\u500b\u5be6\u9ad4\u90fd\u6709\u81ea\u5df1\u7684 name \u548c speed\u3002

move \u548c stop \u662f\u5be6\u9ad4\u65b9\u6cd5(Instance method)\uff0c\u5b83\u5011\u662f\u5c6c\u65bc Ship \u7684\u5be6\u9ad4\uff0c\u53ef\u4ee5\u88ab Ship \u7684\u5be6\u9ad4\u547c\u53eb\u3002

self \u662f\u4e00\u500b\u7279\u6b8a\u7684\u53c3\u6578\uff0c\u5b83\u4ee3\u8868\u4e86\u5be6\u9ad4\u672c\u8eab\u3002

__init__ \u662f\u4e00\u500b\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u7576\u6211\u5011\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5be6\u9ad4\u6642\uff0c\u5b83\u6703\u5728\u5275\u5efa\u5be6\u9ad4\u6642\u81ea\u52d5\u88ab\u547c\u53eb\u4f86\u521d\u59cb\u5316\u5be6\u9ad4\u3002\u4f46\u5b83\u4e26\u975e\u662f\u5fc5\u8981\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4e0d\u5beb\u3002

\u4f60\u53ef\u80fd\u6709\u5b78\u904e\u5176\u4ed6\u8a9e\u8a00\uff0c\u807d\u904e\u5efa\u69cb\u5b50(Constructor)\u9019\u500b\u540d\u8a5e\uff0c\u6982\u5ff5\u985e\u4f3c\u3002

"},{"location":"fundamental/python/classes/#inheritance","title":"Inheritance","text":"

\u5982\u679c\u6211\u9084\u60f3\u8981\u518d\u5b9a\u7fa9\u4e00\u500b\u985e\u5225 BattleShip\uff0c\u5b83\u64c1\u6709\u6240\u6709 Ship \u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u662f\u9084\u6709\u4e00\u4e9b\u984d\u5916\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u6211\u53c8\u4e0d\u60f3\u6574\u500b\u91cd\u5beb\u4e00\u6b21\uff0c\u9019\u6642\u5019\u5c31\u53ef\u4ee5\u7528\u5230\u7e7c\u627f(Inheritance)\u3002

from random import random\n\n\nclass Ship:\n    max_no_flags = 3\n\n    def __init__(self, hp, name, speed):\n        # Instance attribute\n        self.hp = hp\n        self.name = name\n        self.speed = speed\n\n    def move(self):\n        print(f\"{self.name} is moving at speed {self.speed}.\")\n\n    def stop(self):\n        self.speed = 0\n        print(f\"{self.name} has stopped.\")\n\n\nclass BattleShip(Ship):\n    def __init__(self, hp, name, speed, max_speed, damage, defense, acc):\n        super().__init__(hp, name, speed)\n        self.max_speed = max_speed\n        self.damage = damage\n        self.defense = defense\n        self.acc = acc\n\n    # Overriding the move method\n    def move(self, acceleration=1):\n        if self.speed < self.max_speed:\n            self.speed += acceleration\n        self.speed = min(self.speed, self.max_speed)\n        print(f\"{self.name} is moving at speed {self.speed}.\")\n\n    def stop(self):\n        self.speed = 0\n        print(f\"{self.name} has stopped.\")\n\n    # New method\n    def fire(self, target):\n        if random() <= self.acc:\n            damage = self.damage - target.defense\n            print(f\"{self.name} hit {target.name} for {damage} damage.\")\n            target.hp -= damage\n            if target.hp <= 0:\n                print(f\"{target.name} has been destroyed.\")\n        else:\n            print(f\"{self.name} missed {target.name}.\")\n\n\nBismarck = BattleShip(500, \"Bismarck\", 0, 30, 30, 10, 0.8)\nYamato = BattleShip(650, \"Yamato\", 0, 27, 41, 12, 0.6)\n\nBismarck.move(5)\nBismarck.fire(Yamato)\nYamato.fire(Bismarck)\nBismarck.stop()\n\nprint(type(Bismarck))\n
Possible Output
Bismarck is moving at speed 5.\nBismarck hit Yamato for 18 damage.\nYamato missed Bismarck.\nBismarck has stopped.\n<class '__main__.BattleShip'>\n

SABATON - Bismarck

\u53ea\u8981\u5728\u5b9a\u7fa9 BattleShip \u6642\uff0c\u628a Ship \u653e\u5728\u62ec\u865f\u88e1\uff0c\u5c31\u53ef\u4ee5\u8b93 BattleShip \u7e7c\u627f Ship \u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\u3002

\u9019\u88e1\u6211\u5f9e random \u6a21\u7d44\u4e2d\u5f15\u5165\u4e86 random \u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u96a8\u6a5f\u7522\u751f\u4e00\u500b \\([0, 1)\\) \u4e4b\u9593\u7684\u6d6e\u9ede\u6578\uff0c\u4f86\u6a21\u64ec\u5c04\u64ca\u7684\u6e96\u78ba\u5ea6\u3002

BattleShip \u7e7c\u627f\u4e86 Ship\uff0c\u6240\u4ee5\u5b83\u64c1\u6709 Ship \u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u9084\u591a\u4e86\u4e00\u4e9b\u81ea\u5df1\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u5176\u4e2d move \u65b9\u6cd5\u88ab\u8986\u5beb(Overriding)\u4e86\uff0c\u9019\u6a23 BattleShip \u7684\u5be6\u9ad4\u547c\u53eb move \u65b9\u6cd5\u6642\uff0c\u6703\u547c\u53eb BattleShip \u7684 move \u65b9\u6cd5\uff0c\u800c\u4e0d\u662f Ship \u7684\u3002\u800c\u65b0\u65b9\u6cd5 fire \u53ea\u6709 BattleShip \u6709\u3002

super() \u662f\u4e00\u500b\u7279\u6b8a\u7684\u5de5\u5177\uff0c\u5b83\u53d6\u5f97\u7236\u985e\u5225\uff0c\u56e0\u6b64\u6211\u5011\u5728 BattleShip \u7684 __init__ \u65b9\u6cd5\u4e2d\u53ef\u4ee5\u547c\u53eb Ship \u7684 __init__ \u65b9\u6cd5\u3002

"},{"location":"fundamental/python/classes/#encapsulation","title":"Encapsulation","text":"

\u5728 Python \u4e2d\uff0c\u6211\u5011\u7121\u6cd5\u50cf\u5176\u4ed6\u8a9e\u8a00\u4e00\u6a23\uff0c\u5c0d\u5c6c\u6027\u8a2d\u5b9a\u5b58\u53d6\u6b0a\u9650\uff0c\u4f46\u662f\u6211\u5011\u53ef\u4ee5\u7528\u547d\u540d\u898f\u5247(Naming Convention)\u4f86\u544a\u8a34\u4f7f\u7528\u8005\uff0c\u54ea\u4e9b\u5c6c\u6027\u662f\u4e0d\u61c9\u8a72\u76f4\u63a5\u5b58\u53d6\u7684\u3002

\u5728 Python \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u5c6c\u6027\u540d\u7a31\u91cd\u6574(Name Mangling)\u4f86\u9054\u5230\u9019\u500b\u76ee\u7684\uff0c\u53ea\u8981\u5728\u5c6c\u6027\u540d\u7a31\u524d\u9762\u52a0\u4e0a\u5169\u500b\u5e95\u7dda __\uff0cPython \u5c31\u6703\u628a\u5b83\u6539\u540d\u6210 _\u985e\u5225\u540d\u7a31__\u5c6c\u6027\u540d\u7a31\uff0c\u9019\u6a23\u5c31\u4e0d\u5bb9\u6613\u88ab\u5b58\u53d6\u5230\u3002

class Ship:\n    def __init__(self, hp, name, speed):\n        self.__hp = hp\n        self.__name = name\n        self.__speed = speed\n\n    def get_hp(self):\n        return self.__hp\n\n    def set_hp(self, hp):\n        if hp > 0:\n            self.__hp = hp\n        else:\n            print(\"HP must be greater than 0.\")\n

To be continued...

"},{"location":"fundamental/python/classes/#practice","title":"Practice","text":"

\u5176\u5be6\u9019\u7ae0\u6211\u4e0d\u592a\u6e05\u695a\u8981\u6559\u5230\u54ea\u500b\u5730\u6b65\uff0c\u56e0\u70ba\u9019\u662f\u504f\u958b\u767c\u7684\u6771\u897f\uff0c\u4f46\u6211\u662f\u60f3\u6559\u4f60\u89e3\u984c\uff0c\u6240\u4ee5\u5167\u5bb9\u9084\u6703\u518d\u8b8a\u52d5\uff0c\u4f46\u81f3\u5c11\u4f60\u61c9\u8a72\u77e5\u9053\u600e\u9ebc\u5b9a\u7fa9\u985e\u5225\uff0c\u4ee5\u53ca\u600e\u9ebc\u7528\u5c6c\u6027\u548c\u65b9\u6cd5\u3002

\u4e0b\u9762\u7684\u7df4\u7fd2\uff0c\u5176\u5be6\u4e5f\u53ef\u4ee5\u4e0d\u7528\u985e\u5225\u4f86\u5beb\uff0c\u4f46\u6211\u60f3\u8b93\u4f60\u719f\u6089\u4e00\u4e0b\u985e\u5225\u7684\u4f7f\u7528\u3002

Itsa - [C_AR76-\u6613] \u63d0\u6b3e\u6a5f\u7a0b\u5f0f

Reference code
class ATM:\n    def __init__(self, data):\n        self.accounts = {}\n        for account, password, money in data:\n            self.accounts[(account, password)] = money\n\n    def get_money(self, account, password):\n        if (account, password) in self.accounts:\n            print(self.accounts[(account, password)])\n        else:\n            print('error')\n\n\ndata = [[123, 456, 9000], [456, 789, 5000], [789, 888, 6000], [336, 558, 10000], [775, 666, 12000],\n        [566, 221, 7000]]\n\natm = ATM(data)\nN = int(input())\n\nfor _ in range(N):\n    account, password = map(int, input().split())\n    atm.get_money(account, password)\n

Itsa - [C_AR97-\u6613] \u77e9\u9663\u5206\u7d20\u4e58\u7a4d

Reference code
class Matrix:\n    def __init__(self, matrix):\n        self.matrix = matrix\n        self.rows = len(matrix)\n        self.cols = len(matrix[0])\n\n    def entrywise_product(self, other):\n        result = [[0 for _ in range(self.cols)] for _ in range(self.rows)]\n\n        for i in range(self.rows):\n            for j in range(self.cols):\n                result[i][j] = self.matrix[i][j] * other.matrix[i][j]\n\n        return Matrix(result)\n\n\nm, n = map(int, input().split())\nmat1 = [list(map(int, input().split())) for _ in range(m)]\nmat2 = [list(map(int, input().split())) for _ in range(m)]\n\nmatrix1 = Matrix(mat1)\nmatrix2 = Matrix(mat2)\n\nresult = matrix1.entrywise_product(matrix2)\n\nfor row in result.matrix:\n    print(*row)\n

* \u5728\u9019\u88e1\u7a31\u70ba Unpacking Operator

"},{"location":"fundamental/python/classes/#assignment","title":"Assignment","text":"

Itsa - [C_AR74-\u4e2d] \u5b78\u751f\u8cc7\u6599\u641c\u5c0b\u7a0b\u5f0f

Tip

\u4e0d\u7528\u6015\uff0c\u4f60\u53ef\u4ee5\u7684\u3002

"},{"location":"fundamental/python/dictionaries/","title":"Dictionaries","text":""},{"location":"fundamental/python/dictionaries/#introduction","title":"Introduction","text":"

\u4f86\u4ecb\u7d39\u672c\u4e3b\u984c\u4e2d\u6700\u5f8c\u4e00\u500b\u8cc7\u6599\u7d50\u69cb\uff1a\u5b57\u5178(Dictionaries)\uff0c\u662f\u7531\u9375(Key)\u548c\u503c(Value)\u6240\u7d44\u6210\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u7d93\u7531\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u9375\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u800c\u503c\u5247\u53ef\u4ee5\u662f\u4efb\u4f55\u578b\u614b\u7684\u8cc7\u6599\u3002

Question

  1. \u5e8f\u5c0d\u53ef\u4ee5\u505a\u70ba\u5b57\u5178\u7684\u9375\u55ce?
  2. \u4e32\u5217\u53ef\u4ee5\u505a\u70ba\u5b57\u5178\u7684\u9375\u55ce?
  3. \u5b57\u5178\u53ef\u4ee5\u505a\u70ba\u5b57\u5178\u7684\u503c\u55ce?

\u7559\u7d66\u4f60\u601d\u8003\uff0c\u4f60\u4e00\u5b9a\u77e5\u9053\u7b54\u6848\uff0c\u5c31\u7b97\u4f60\u6c92\u8fa6\u6cd5\u99ac\u4e0a\u77e5\u9053\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5beb\u7a0b\u5f0f\u4f86\u6e2c\u8a66\u3002

"},{"location":"fundamental/python/dictionaries/#create_a_dictionary","title":"Create a Dictionary","text":"

\u4f60\u53ef\u4ee5\u7528{}\u3001dict() \u6216\u8005 Comprehension \u4f86\u5efa\u7acb\u4e00\u500b\u5b57\u5178\u3002

scores = {\"Compiler\": 100, \"AWS\": 95, \"Data Science\": 92}\nprint(scores)\n\n# when keys are strings, using keyword arguments\ndirections = dict(North=\"\u2191\", South=\"\u2193\", East=\"\u2192\", West=\"\u2190\")\nprint(directions)\n\ncapital = dict([(\"Taiwan\", \"Taipei\"), (\"Japan\", \"Tokyo\"), (\"Korea\", \"Seoul\")])\nprint(capital)\n\n# comprehension\ncube = {x: x ** 3 for x in range(-2, 3)}\nprint(cube)\n
Output
{'Compiler': 100, 'AWS': 95, 'Data Science': 92}\n{'North': '\u2191', 'South': '\u2193', 'East': '\u2192', 'West': '\u2190'}\n{'Taiwan': 'Taipei', 'Japan': 'Tokyo', 'Korea': 'Seoul'}\n{-2: -8, -1: -1, 0: 0, 1: 1, 2: 8}\n

\u90a3\u70ba\u4ec0\u9ebc\u8aaa\u5b57\u5178\u662f\u6709\u5e8f\u7684\u5462?\u4f60\u591a\u57f7\u884c\u5e7e\u6b21\uff0c\u8f38\u51fa\u7684\u9806\u5e8f\u8207\u4f60\u5efa\u7acb\u7684\u9806\u5e8f\u662f\u4e00\u6a23\u7684\u55ce?

"},{"location":"fundamental/python/dictionaries/#operations","title":"Operations","text":""},{"location":"fundamental/python/dictionaries/#accessing_and_modifying_elements","title":"Accessing and Modifying elements","text":"

\u4f60\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u53d6\u5f97\u503c\uff0c\u4e5f\u53ef\u4ee5\u900f\u904e\u9375\u4f86\u4fee\u6539\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u6703\u5f97\u5230 KeyError\u3002

recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"King Gnu\"] = \"SPECIALZ\"\nprint(recent_listening[\"King Gnu\"])\n\nrecent_listening[\"HEALTH\"] = \"Blue Monday\"\nprint(recent_listening[\"HEALTH\"])\n\nprint(recent_listening[\"ALI\"])\n
Output
\u98db\u884c\u8247\nSPECIALZ\nBlue Monday\nKeyError: 'ALI'\n

King Gnu - \u98db\u884c\u8247

Blue Monday

\u90a3\u9ebc\u8a72\u5982\u4f55\u6aa2\u67e5\u9375\u662f\u5426\u5b58\u5728\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 in \u4f86\u6aa2\u67e5\uff0c\u4f86\u907f\u514d\u932f\u8aa4\u3002

recent_listening = {\"King Gnu\": \"\u98db\u884c\u8247\"}\n\nprint(\"King Gnu\" in recent_listening)\nprint(\"ALI\" in recent_listening)\n
Output
True\nFalse\n

Question

\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u5b57\u5178\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u9375\u5b58\u5728?

"},{"location":"fundamental/python/dictionaries/#removing_elements","title":"Removing elements","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 del \u4f86\u522a\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002

\u4f8b\u5982\u6211\u9019\u9663\u5b50\u90fd\u4e0d\u5beb Java \u4e86\uff0c\u6240\u4ee5\u628a Java \u5f9e\u6211\u7684\u5fc3\u4e2d\u522a\u9664\u3002

my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\nprint(my_love)\n\ndel my_love[\"Java\"]\nprint(my_love)\n
Output
{'python': 100, 'Java': 70, 'Js': 60}\n{'python': 100, 'Js': 60}\n
"},{"location":"fundamental/python/dictionaries/#iterating","title":"Iterating","text":"

\u76f4\u63a5\u4f86\u770b\u4f8b\u5b50\u5427\u3002

my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love:\n    print(key, my_love[key])\n
Output
python 100\nJava 70\nJs 60\n

\u5982\u679c\u4f60\u53ea\u60f3\u8981\u53d6\u5f97\u9375\u6216\u8005\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 keys() \u6216\u8005 values()\u3002

my_love = {\"python\": 100, \"Java\": 70, \"Js\": 60}\n\nfor key in my_love.keys():\n    print(key)\n\nprint(my_love.keys())\n\nfor value in my_love.values():\n    print(value)\n\nprint(list(my_love.values()))\n
Output
python\nJava\nJs\ndict_keys(['python', 'Java', 'Js'])\n100\n70\n60\n[100, 70, 60]\n

\u90a3\u5982\u679c\u4f60\u60f3\u8981\u540c\u6642\u53d6\u5f97\u9375\u548c\u503c\u5462?\u4f60\u53ef\u4ee5\u4f7f\u7528 items()\u3002

my_cat = {\"name\": \"Fat Orange\", \"age\": 12, \"is_cute\": True}\n\nfor key, value in my_cat.items():\n    print(f\"{key}: {value}\")\n
Output
name: Fat Orange\nage: 12\nis_cute: True\n

\u6211\u611b\u6211\u7684\u6a58\u8c93\u3002

"},{"location":"fundamental/python/dictionaries/#methods","title":"Methods","text":""},{"location":"fundamental/python/dictionaries/#get","title":"get","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 get() \u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u5f88\u65b9\u4fbf\u5594\u3002

my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info['name'])\nprint(my_info.get('hobbies')[1])\n\n# print(my_info['is_handsome']) # This will cause an error\nprint(my_info.get('is_handsome', 'Of course I am!'))\n
Output
Sean\nworking out\nOf course I am!\n

\u5e0c\u671b\u4f60\u4e0d\u8981\u89ba\u5f97\u6211\u81ea\u6200\u3002

"},{"location":"fundamental/python/dictionaries/#pop","title":"pop","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 pop() \u4f86\u53d6\u5f97\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u540c\u6642\u522a\u9664\u8a72\u9375\u503c\u5c0d\u3002

my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nprint(my_info.pop('age'))\nprint(my_info)\n
Output
20\n{'name': 'Sean', 'hobbies': ['coding', 'working out']}\n
"},{"location":"fundamental/python/dictionaries/#clear","title":"clear","text":"

\u4f60\u53ef\u4ee5\u4f7f\u7528 clear() \u4f86\u6e05\u7a7a\u5b57\u5178\u3002

my_info = {'name': 'Sean', 'age': 20, 'hobbies': ['coding', 'working out']}\nmy_info.clear()\nprint(my_info)\n
Output
{}\n

umm... \u518d\u904e\u4e0d\u4e45\u6211\u5c31\u8981 21 \u6b72\u4e86\u3002

\u6211\u8a8d\u70ba\u9019\u4e9b\u65b9\u6cd5\u5c31\u5920\u7528\u4e86\uff0c\u5176\u4ed6\u7684\u4f60\u53ef\u4ee5\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u56c9~

"},{"location":"fundamental/python/dictionaries/#practice","title":"Practice","text":"

Itsa - [C_ST19-\u6613] \u6642\u9593\u8f49\u63db

Reference code

n = int(input())\n\ntime_diff = {\"TW\": 0, \"JA\": 60, \"USE\": -720, \"USC\": -780, \"USW\": -840, \"UK\": -480}\n\nfor _ in range(n):\n    time, cur, to = input().split()\n    minutes = int(time[:2]) * 60 + int(time[3:])\n    minutes += time_diff[to] - time_diff[cur]\n\n    if minutes < 0:\n        minutes += 1440\n    minutes %= 1440\n\n    h, m = divmod(minutes, 60)\n    print(f\"{h:02d}{m:02d} {to}\")\n
\u4e00\u5f8b\u5148\u8f49\u63db\u6210\u5206\u9418\uff0c\u518d\u9032\u884c\u6642\u5340\u8f49\u63db\uff0c\u6700\u5f8c\u518d\u8f49\u63db\u6210\u5c0f\u6642\u548c\u5206\u9418\u3002

divmod \u56de\u50b3\u4e00\u500b\u5e8f\u5c0d\uff0c\u5546\u8207\u9918\u6578\u3002

"},{"location":"fundamental/python/dictionaries/#assignment","title":"Assignment","text":"

Itsa - [C_AR111-\u6613] \u5c0d\u8a71\u6a5f\u5668\u4eba

Itsa - [C_AR152-\u6613] \u6b63\u6574\u6578\u7d71\u8a08

Tip

sorted() and key

Itsa - [C_AR42-\u6613] \u904e\u534a\u5143\u7d20

Itsa - [C_AR188-\u6613] \u9663\u5217\u5143\u7d20

Tip

\u9664\u4e86\u7528\u5b57\u5178\u4f86\u89e3\u984c\u4e4b\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5b78 Boyer\u2013Moore majority vote algorithm

"},{"location":"fundamental/python/functions/","title":"Functions","text":""},{"location":"fundamental/python/functions/#introduction","title":"Introduction","text":"

\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u56de\u60f3 Repetition Structures - Nested Loop \u4e2d\u7684\u4f8b\u5b50:

\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\([1, n]\\) \u9593\u7684\u6700\u5927\u8cea\u6578\u3002

n = int(input())\n\nfor i in range(n, 0, -1):\n    is_prime = True\n    for j in range(2, i):\n        if i % j == 0:\n            is_prime = False\n            break\n\n    if is_prime:\n        print(f\"{i} is the largest prime in [1, {n}]\")\n        break\n

\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u529f\u80fd\u662f\u6b63\u78ba\u7684\uff0c\u4f46\u662f\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u4e0d\u9ad8\uff0c\u56e0\u70ba\u7a0b\u5f0f\u78bc\u7684\u9577\u5ea6\u592a\u9577\uff0c\u4e26\u4e14\u7a0b\u5f0f\u78bc\u7684\u908f\u8f2f\u4e0d\u5920\u6e05\u6670\u3002

\u6211\u5011\u53ef\u4ee5\u628a\u5224\u65b7\u8cea\u6578\u7684\u90e8\u5206\u62bd\u51fa\u4f86\uff0c\u4e26\u4e14\u5c07\u4ed6\u5305\u88dd\u6210\u4e00\u500b\u51fd\u5f0f:

def is_prime(x):\n    for i in range(2, x):\n        if x % i == 0:\n            return False\n    return True\n\n\nn = int(input())\n\nfor i in range(n, 0, -1):\n    if is_prime(i):\n        print(f\"{i} is the largest prime in [1, {n}]\")\n        break\n

\u9019\u6a23\u7684\u597d\u8655\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u76f8\u540c\u7684\u7a0b\u5f0f\u78bc\u91cd\u8907\u4f7f\u7528\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u3002

\u90a3\u5982\u679c\u6211\u60f3\u8981\u9032\u4e00\u6b65\u7a0b\u5f0f\u78bc\u5c01\u88dd\u6210\u4e00\u500b\u63a5\u53d7\u6b63\u6574\u6578 \\(n\\) \u7684\u51fd\u5f0f\uff0c\u4e26\u4e14\u56de\u50b3 \\([1, n]\\) \u9593\u7684\u6700\u5927\u8cea\u6578\u5462?

def is_prime(x):\n    for i in range(2, x):\n        if x % i == 0:\n            return False\n    return True\n\n\ndef largest_prime(n):\n    for i in range(n, 0, -1):\n        if is_prime(i):\n            return i\n\n\nn = int(input())\n\nprint(f\"{largest_prime(n)} is the largest prime in [1, {n}]\")\n

\u4e0d\u66c9\u5f97\u4f60\u6709\u6c92\u6709\u611f\u53d7\u5230\uff0c\u7576\u7a0b\u5f0f\u78bc\u8d8a\u4f86\u8d8a\u9577\uff0c\u6211\u5011\u5c31\u8d8a\u9700\u8981\u51fd\u5f0f\u4f86\u5e6b\u52a9\u6211\u5011\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5\u66f4\u5bb9\u6613\u7684\u95b1\u8b80\u7a0b\u5f0f\u78bc\u3002

\u9019\u500b\u5f15\u5b50\u544a\u8a34\u6211\u5011\uff0c\u51fd\u5f0f\u662f\u4e00\u500b\u53ef\u4ee5\u5c07\u7a0b\u5f0f\u78bc\u5305\u88dd\u6210\u4e00\u500b\u7368\u7acb\u7684\u55ae\u4f4d\uff0c\u4e26\u4e14\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u7684\u5de5\u5177\u3002

"},{"location":"fundamental/python/functions/#define_a_function","title":"Define a Function","text":"

\u51fd\u5f0f\u7684\u5b9a\u7fa9\u662f\u4ee5 def \u958b\u982d\uff0c\u5f8c\u9762\u63a5\u8457\u51fd\u5f0f\u7684\u540d\u7a31\uff0c\u4ee5\u53ca\u62ec\u865f\u5167\u7684\u53c3\u6578(Parameter)\uff0c\u7e2e\u6392\u5167\u7684\u7a0b\u5f0f\u78bc\u5c31\u662f\u51fd\u5f0f\u7684\u5167\u5bb9\u3002

\u4f60\u53ef\u4ee5\u9078\u64c7\u662f\u5426\u8981\u5728\u51fd\u5f0f\u4e2d\u52a0\u4e0a return \u4f86\u56de\u50b3\u503c\uff0c\u5982\u679c\u6c92\u6709\uff0c\u51fd\u5f0f\u5247\u6703\u56de\u50b3 None\u3002

\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f greet\uff0c\u4ed6\u63a5\u53d7\u4e00\u500b\u53c3\u6578 name\uff0c\u4e26\u5370\u51fa Hello, {name}:

def greet(name):\n    print(f\"Hello, {name}\")\n\n\nprint(greet(\"World\"))\n
Output
Hello, World\nNone\n

greet(\"World\") \u6703\u5148\u5370\u51fa Hello, World\uff0c\u63a5\u8457\u56de\u50b3 None\u3002

\u5b57\u4e32 \"World\" \u88ab\u7a31\u70ba\u5f15\u6578(Argument)\uff0c\u800c name \u5247\u88ab\u7a31\u70ba\u53c3\u6578(Parameter)\u3002\u4f46\u4e5f\u4e0d\u7528\u592a\u62d8\u6ce5\u3002

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f freq(x) \u63a5\u53d7\u4e00\u500b\u6578\u5b57\u5b57\u4e32 x \uff0c\u56de\u50b3 0-9 \u7684\u51fa\u73fe\u6b21\u6578\uff0c\u4ee5\u5e8f\u5c0d\u7684\u65b9\u5f0f\u56de\u50b3:

def freq(x: str) -> tuple:\n    table = [0] * 10\n    for digit in x:\n        table[int(digit)] += 1\n\n    return tuple(table)\n\n\na, b = \"114514\", \"111445\"\nprint(freq(a) == freq(b))\n
Output
True\n

\u6211\u5011\u53ef\u4ee5\u5728\u53c3\u6578\u5f8c\u9762\u52a0\u4e0a : \u4f86\u6307\u5b9a\u53c3\u6578\u7684\u578b\u5225\uff0c\u4e26\u4e14\u5728\u51fd\u5f0f\u5f8c\u9762\u52a0\u4e0a -> \u4f86\u6307\u5b9a\u56de\u50b3\u503c\u7684\u578b\u5225\u3002

"},{"location":"fundamental/python/functions/#default_argument_values","title":"Default Argument Values","text":"

\u6709\u6642\u5019\u6211\u5011\u6703\u5e0c\u671b\u51fd\u5f0f\u7684\u53c3\u6578\u6709\u9810\u8a2d\u503c\uff0c\u9019\u6a23\u5728\u547c\u53eb\u51fd\u5f0f\u6642\u5c31\u4e0d\u9700\u8981\u586b\u5165\u5f15\u6578\u3002

\u4f8b\u5982\uff0c\u51fd\u5f0f max_of(x, k=1) \u63a5\u53d7\u5169\u500b\u53c3\u6578 x \u4e32\u5217\u8207 k \u6578\u5b57\uff0c\u56de\u50b3 x \u4e2d\u6700\u5927\u7684 k \u500b\u6578\u5b57\uff0c\u4ee5\u4e32\u5217\u7684\u65b9\u5f0f\u56de\u50b3\uff0c\u800c k \u7684\u9810\u8a2d\u503c\u70ba 1:

def max_of(x: list[int], k=1):\n    return sorted(x, reverse=True)[:k]\n\n\ny = [4, 8, 5, 3, 9]\nprint(max_of(y))\nprint(max_of(y, 2))\nprint(max_of(y, k=3))\n
Output
[9]\n[9, 8]\n[9, 8, 5]\n

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u51fd\u5f0f weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5) \u63a5\u53d7\u4e09\u500b\u53c3\u6578 math, english, programming \u4ee5\u53ca\u4e09\u500b\u9810\u8a2d\u503c math_weight=0.25, english_weight=0.25, programming_weight=0.5\uff0c\u56de\u50b3\u52a0\u6b0a\u5206\u6578:

def weight_score(math, english, programming, math_weight=0.25, english_weight=0.25, programming_weight=0.5):\n    return math * math_weight + english * english_weight + programming * programming_weight\n\n\nsean_score = weight_score(80, 90, 100)\nyaris_score = weight_score(math=82, math_weight=0.3, english=90, english_weight=0.3, programming=100,\n                           programming_weight=0.4)\nprint(sean_score, yaris_score)\n
Output
92.5 91.6\n

\u6709\u9810\u8a2d\u503c\u7684\u53c3\u6578\u5fc5\u9808\u653e\u5728\u6c92\u6709\u9810\u8a2d\u503c\u7684\u53c3\u6578\u5f8c\u9762\u3002\u4f46\u5982\u679c\u4f60\u5728\u547c\u53eb\u51fd\u5f0f\u7684\u6642\u5019\uff0c\u6307\u5b9a\u4e86\u53c3\u6578\u7684\u540d\u7a31\uff0c\u5247\u53ef\u4ee5\u4e0d\u7528\u9075\u5b88\u9019\u500b\u898f\u5247\u3002

\u5176\u5be6\u9084\u6709\u4e00\u4e9b\u795e\u5947\u7684\u7528\u6cd5\uff0c\u4f46\u5c31\u4e0d\u5728\u9019\u88e1\u8a0e\u8ad6\u4e86\u3002

"},{"location":"fundamental/python/functions/#lambda_function","title":"Lambda Function","text":"

\u9084\u8a18\u5f97 map \u55ce? \u4ed6\u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u61c9\u7528\u5230\u4e00\u500b\u4e32\u5217\u4e0a\u3002

\u5148\u56de\u61b6\u4e00\u4e0b\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u4f7f\u7528 map:

def square(x):\n    return x ** 2\n\n\nlst = [1, 2, 3, 4, 5]\nprint(list(map(square, lst)))\n\nsong_name = [\"I\", \"Really\", \"Want\", \"to\", \"Stay\", \"At\", \"Your\", \"House\"]\nprint(list(map(len, song_name)))\n
Output
[1, 4, 9, 16, 25]\n[1, 6, 4, 2, 4, 2, 4, 5]\n

I Really Want to Stay At Your House\u201d by Rosa Walton

\u9019\u7a2e\u63a5\u53d7\u51fd\u5f0f\u70ba\u53c3\u6578\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u9ad8\u968e\u51fd\u5f0f(Higher-Order Function)\u3002

\u4f46\u662f\uff0c\u5982\u679c\u51fd\u5f0f\u53ea\u6703\u88ab\u4f7f\u7528\u4e00\u6b21\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 lambda \u4f86\u5b9a\u7fa9\u4e00\u500b\u533f\u540d\u51fd\u5f0f:

lst = [1, 2, 3, 4, 5]\nprint(list(map(lambda x: x ** 2, lst)))\n
Output
[1, 4, 9, 16, 25]\n

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u7528\u904e min \u55ce? \u4ed6\u53ef\u4ee5\u627e\u51fa\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u6700\u5c0f\u503c\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u81ea\u8a02\u898f\u5247\uff0c\u4f8b\u5982\u6709\u4e00\u500b\u5ea7\u6a19\u4e32\u5217 points\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u627e\u51fa\u6700\u9760\u8fd1\u539f\u9ede\u7684\u9ede:

points = [(9, 2), (3, 4), (5, 6), (7, 8), (4, 8), (1, 3)]\nprint(min(points, key=lambda x: x[0] ** 2 + x[1] ** 2))\n
Output
(1, 3)\n

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u5169\u500b\u4e32\u5217 a \u8207 b\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u8a08\u7b97\u5169\u500b\u4e32\u5217\u7684\u5167\u7a4d:

a, b = [1, 2, 3], [4, 5, 6]\nprint(sum(map(lambda x, y: x * y, a, b)))\n
Output
32\n
"},{"location":"fundamental/python/functions/#factory_function","title":"Factory Function","text":"

\u6709\u6642\u5019\u6211\u5011\u6703\u5e0c\u671b\u51fd\u5f0f\u56de\u50b3\u53e6\u4e00\u500b\u51fd\u5f0f\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u5de5\u5ee0\u51fd\u5f0f(Factory Function)\u3002

\u8209\u500b\u4f8b\u5b50\uff0c\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f make_power_fun(n) \uff0c\u4ed6\u63a5\u53d7\u4e00\u500b\u6578\u5b57 n\uff0c\u56de\u50b3\u4e00\u500b\u51fd\u5f0f\uff0c\u9019\u500b\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u6578\u5b57 x\uff0c\u56de\u50b3 x \u7684 n \u6b21\u65b9:

def make_power_fun(n):\n    return lambda x: x ** n\n\n\npower_fun_2 = make_power_fun(2)\npower_fun_3 = make_power_fun(4)\n\nprint(power_fun_2(3))\nprint(power_fun_3(3))\n
Output
9\n81\n

\u9019\u6a23\u7684\u597d\u8655\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5730\u65b9\u4f7f\u7528\u4e0d\u540c\u7684 n\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u8907\u5b9a\u7fa9\u51fd\u5f0f\u3002

"},{"location":"fundamental/python/functions/#nested_function","title":"Nested Function","text":"

\u51fd\u5f0f\u53ef\u4ee5\u88ab\u5b9a\u7fa9\u5728\u53e6\u4e00\u500b\u51fd\u5f0f\u7684\u5167\u90e8\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u5de2\u72c0\u51fd\u5f0f(Nested Function)\u3002

\u4f8b\u5982\uff0c\u51fd\u5f0f collect_anagrams(words, target) \u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u4e32\u5217 words \u8207\u4e00\u500b\u5b57\u4e32 target\uff0c\u56de\u50b3 words \u4e2d\u8207\u4e92\u70ba target \u662f\u76f8\u540c\u5b57\u6bcd\u7570\u5e8f\u8a5e\u7684\u4e32\u5217:

\u65bc\u662f\u6211\u5728\u51fd\u5f0f\u7684\u5167\u90e8\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u8f14\u52a9\u51fd\u5f0f(Helper Function) is_anagram(x, y)\uff0c\u4ed6\u63a5\u53d7\u5169\u500b\u5b57\u4e32 x \u8207 y\uff0c\u56de\u50b3 x \u8207 y \u662f\u5426\u70ba\u76f8\u540c\u5b57\u6bcd\u7570\u5e8f\u8a5e:

def collect_anagrams(words, target):\n    def is_anagram(x, y):\n        return sorted(x) == sorted(y)\n\n    return [word for word in words if is_anagram(word, target)]\n\n\nstrs = [\"now\", \"won\", \"own\", \"no\", \"on\", \"www\"]\nprint(collect_anagrams(strs, \"onw\"))\n
Output
['now', 'won', 'own']\n

\u9019\u500b\u5728\u5237 LeeCode \u6642\u6703\u5e38\u5e38\u7528\u5230\u3002

"},{"location":"fundamental/python/functions/#recursion","title":"Recursion","text":"

\u4f86\u5230\u672c\u7ae0\u7684\u91cd\u982d\u6232\uff0c\u905e\u8ff4(Recursion)\u3002

\u905e\u8ff4\u662f\u4e00\u7a2e\u51fd\u5f0f\u547c\u53eb\u81ea\u5df1\u7684\u6280\u5de7\uff0c\u9019\u6a23\u7684\u51fd\u5f0f\u88ab\u7a31\u70ba\u905e\u8ff4\u51fd\u5f0f(Recursive Function)\u3002

\u5148\u4f86\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(n!\\)\uff0c\u8ff4\u5708\u7684\u5beb\u6cd5\u662f\u9019\u6a23\u7684:

def factorial(n):\n    result = 1\n    for i in range(1, n + 1):\n        result *= i\n    return result\n\n\nprint(factorial(0))\nprint(factorial(5))\n
Output
1\n120\n

\u8a18\u5f97\u9ad8\u4e2d\u6578\u5b78\u8ab2\u672c\u4e0a\u7684\u5b9a\u7fa9\u55ce?

\\[ \\text{factorial}(n) = \\begin{cases} 1 & \\text{if } n = 0 \\text{, base case}\\\\ n \\times \\text{factorial}(n-1) & \\text{if } n > 0 \\text{, recursive case}\\end{cases} \\]

\u9019\u6a23\u7684\u5b9a\u7fa9\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ffb\u8b6f\u6210\u7a0b\u5f0f\u78bc:

def factorial(n):\n    if n == 0:\n        return 1\n    else:\n        return n * factorial(n - 1)\n\n\nprint(factorial(0))\nprint(factorial(5))\n
Output
1\n120\n

\u7576 n == 0 \u6642\uff0c\u6211\u5011\u7a31\u70ba\u57fa\u672c\u60c5\u6cc1(Base Case)\uff0c\u9019\u662f\u905e\u8ff4\u7684\u7d42\u6b62\u689d\u4ef6\uff0c\u7576 n > 0 \u6642\uff0c\u6211\u5011\u7a31\u70ba\u905e\u8ff4\u60c5\u6cc1(Recursive Case)\uff0c\u9019\u662f\u905e\u8ff4\u7684\u57f7\u884c\u689d\u4ef6\u3002

\u6bcf\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f\u90fd\u61c9\u8a72\u6709\u4e00\u500b\u57fa\u672c\u60c5\u6cc1\uff0c\u9019\u6a23\u7684\u905e\u8ff4\u624d\u6703\u7d42\u6b62\uff0c\u5426\u5247\u5c31\u6703\u9677\u5165\u7121\u7aae\u905e\u8ff4\uff0c\u54a6?\u6709\u6c92\u6709\u5f88\u719f\u6089?\u9084\u8a18\u5f97\u7121\u7aae\u8ff4\u5708\u55ce?

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97\u8cbb\u6c0f\u6578\u5217\u7684\u7b2c \\(n\\) \u9805\uff0c\u8ff4\u5708\u7684\u5beb\u6cd5\u662f\u9019\u6a23\u7684:

def fibonacci(n):\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\n\nprint(fibonacci(0))\nprint(fibonacci(10))\n
Output
0\n55\n

\u540c\u6a23\u7684\uff0c\u9084\u8a18\u5f97\u9ad8\u4e2d\u6578\u5b78\u8ab2\u672c\u4e0a\u7684\u5b9a\u7fa9\u55ce?

\\[ \\text{fibonacci}(n) = \\begin{cases} 0 & \\text{if } n = 0 \\text{, base case}\\\\ 1 & \\text{if } n = 1 \\text{, base case}\\\\ \\text{fibonacci}(n-1) + \\text{fibonacci}(n-2) & \\text{if } n > 1 \\text{, recursive case}\\end{cases} \\]

\u9019\u6a23\u7684\u5b9a\u7fa9\u5c31\u53ef\u4ee5\u76f4\u63a5\u7ffb\u8b6f\u6210\u7a0b\u5f0f\u78bc:

def fibonacci(n):\n    if n == 0:\n        return 0\n    elif n == 1:\n        return 1\n    else:\n        return fibonacci(n - 1) + fibonacci(n - 2)\n\n\nprint(fibonacci(0))\nprint(fibonacci(10))\n
Output
0\n55\n

\u4f46\u662f\uff0c\u905e\u8ff4\u7684\u6548\u7387\u901a\u5e38\u6bd4\u8ff4\u5708\u4f4e\uff0c\u56e0\u70ba\u905e\u8ff4\u6703\u9020\u6210\u5927\u91cf\u7684\u51fd\u5f0f\u547c\u53eb\uff0c\u800c\u4e14\u5bb9\u6613\u9020\u6210\u5927\u91cf\u7684\u91cd\u8907\u8a08\u7b97\u3002

\u518d\u4f86\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8a08\u7b97\u6574\u6578 a, b \u7684\u6700\u5927\u516c\u56e0\u6578\uff0c\u4f60\u53ef\u80fd\u6709\u807d\u904e\u8f3e\u8f49\u76f8\u9664\u6cd5\uff0c\u53c8\u7a31\u70ba\u6b50\u5e7e\u91cc\u5f97\u6f14\u7b97\u6cd5(Euclidean algorithm)\uff0c\u4ed6\u7684\u5b9a\u7fa9\u662f\u9019\u6a23\u7684:

\\[ \\text{gcd}(a, b) = \\begin{cases} a & \\text{if } b = 0 \\text{, base case}\\\\ \\text{gcd}(b, a \\mod b) & \\text{if } b > 0 \\text{, recursive case}\\end{cases} \\]

\u76f4\u63a5\u5beb\u6210\u7a0b\u5f0f\u78bc:

def gcd(a, b):\n    if b == 0:\n        return a\n    else:\n        return gcd(b, a % b)\n\n\nprint(gcd(12, 18))\nprint(gcd(18, 12))\nprint(gcd(4843, 1234))\n
Output
6\n6\n1\n

\u9019\u500b\u8981\u5beb\u6210\u8fed\u4ee3(Iterative)\u7248\u672c\u9084\u6bd4\u8f03\u96e3\u3002

\u95dc\u65bc\u905e\u8ff4\uff0c\u5c31\u5148\u8b1b\u5230\u9019\u88e1\uff0c\u672a\u4f86\u9032\u5165\u6f14\u7b97\u6cd5\u7684\u7ae0\u7bc0\u6642\uff0c\u6703\u518d\u6df1\u5165\u8a0e\u8ad6\uff0c\u800c\u4e14\u6703\u6709\u66f4\u591a\u7684\u4f8b\u5b50\u3002

"},{"location":"fundamental/python/functions/#practice","title":"Practice","text":"

Itsa - [C_MM48-\u6613] F91

Reference code
def f91(n):\n    if n <= 100:\n        return f91(f91(n + 11))\n    elif n >= 101:\n        return n - 10\n\n\nk = int(input())\nn = list(map(int, input().split()))\nfor x in n:\n    print(f91(x))\n

\u4f60\u4ed4\u7d30\u89c0\u5bdf\u4e00\u4e0b\uff0c\u5176\u5be6\u53ef\u4ee5\u5beb\u6210\u9019\u6a23:

def f91(z): \n    if z >= 101: \n        return z - 10 \n    else: \n        return 91 \n\n\nk = int(input()) \nn = list(map(int, input().split())) \nfor x in n: \n    print(f91(x)) \n

Itsa - [C_RU13-\u6613] \u5927\u4e00\u9ede\u7684Fibonacci

Reference code

MAX = 47\nfibonacci = [0] * MAX\nfibonacci[1] = 1\n\nfor i in range(2, MAX):\n    fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]\n\nwhile True:\n    n = int(input())\n    if n == -1:\n        break\n    print(fibonacci[n + 1])\n
\u9019\u7a2e\u5beb\u6cd5\u53eb\u505a\u52d5\u614b\u898f\u5283(Dynamic Programming)\uff0c\u672a\u4f86\u6703\u518d\u8a0e\u8ad6\uff0c\u800c\u4e14\u6703\u5f88\u982d\u75db\u3002

"},{"location":"fundamental/python/functions/#assignment","title":"Assignment","text":"

Itsa - [C_MM103-\u6613] \u8cbb\u5f0f\u6578\u5217

Itsa - [C_MM143-\u6613] \u6c42Emirp

Itsa - [C_MM144-\u6613] \u6c42\u7d44\u5408\u6578C(n,r)

Itsa - \u984c\u76ee10. \u8f3e\u8f49\u76f8\u9664\u6cd5

Itsa - [C_RU10-\u4e2d] \u722c\u6a13\u68af

Itsa - [C_RU14-\u6613] \u597d\u9ad8\u7684\u6c34\u6676\u5854

Itsa - [C_RU23-\u6613] \u905e\u8ff4\u7df4\u7fd22f(n)=f(n-1)+2

Itsa - [C_RU24-\u6613] \u905e\u8ff4\u7df4\u7fd2f(n)\uff1d2f(n-1)/(1+2f(n-1))

Itsa - [C_RU25-\u6613] \u905e\u8ff4\u7df4\u7fd2f(n)\uff1d3f(n-1)+g(n-1)\ufe50g(n)\uff1d-f(n-1)+g(n-1)

"},{"location":"fundamental/python/lists/","title":"Lists","text":""},{"location":"fundamental/python/lists/#introduction","title":"Introduction","text":"

\u4e0d\u66c9\u5f97\u4f60\u770b\u5230\u9019\u88e1\u6709\u6c92\u6709\u767c\u73fe\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u90fd\u662f\u7528\u4e00\u500b\u4e00\u500b\u7684\u8b8a\u6578\u4f86\u5132\u5b58\u8cc7\u6599\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u8981\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u9019\u6a23\u7684\u65b9\u5f0f\u5c31\u6703\u8b8a\u5f97\u5f88\u9ebb\u7169\u3002

\u6240\u4ee5\u9019\u88e1\u8ddf\u4f60\u4ecb\u7d39 List(\u4e32\u5217)\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5132\u5b58\u5f88\u591a\u7b46\u8cc7\u6599\uff0c\u800c\u4e14\u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u4f8b\u5982: \u6578\u5b57\u3001\u5b57\u4e32\u3001\u5e03\u6797\u503c\u7b49\u7b49\u3002

"},{"location":"fundamental/python/lists/#create_a_list","title":"Create a list","text":"

\u8981\u5efa\u7acb\u4e00\u500b List\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u6211\u5011\u8981\u5132\u5b58\u7684\u8cc7\u6599\uff0c\u8cc7\u6599\u4e4b\u9593\u7528\u9017\u865f , \u9694\u958b\u3002

\u4f86\u8209\u500b\u4f8b\u5b50\uff0c\u5efa\u7acb\u4e09\u500b List\uff0c\u4e00\u500b\u90fd\u5132\u5b58\u6574\u6578\uff0c\u4e00\u500b\u662f\u90fd\u5132\u5b58\u5b57\u4e32\uff1b\u6700\u5f8c\u4e00\u500b\u5247\u653e\u4e86\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\u3002

numbers = [1, 2, 3, 4, 5]\nprint(numbers)\n\nwhat_i_learned = [\"C\", \"C++\", \"Python\", \"Java\", \"C#\", \"JavaScript\", \"TypeScript\"]\nprint(what_i_learned)\n\nstudent_info = [\"Sean\", 20, \"Computer Science\", 4.0, True, \"aLIEz\"]\nprint(student_info)\n\nprint(type(numbers), type(what_i_learned), type(student_info))\n
ouput
[1, 2, 3, 4, 5]\n['C', 'C++', 'Python', 'Java', 'C#', 'JavaScript', 'TypeScript']\n['Sean', 20, 'Computer Science', 4.0, True, 'aLIEz']\n<class 'list'> <class 'list'> <class 'list'>\n

Sawano Hiroyuki - aLIEz Aldnoah.Zero Full Lyrics

\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u6703\u807d\u904e Array(\u9663\u5217)\uff0cList \u5c31\u662f Python \u4e2d\u7684 Array\uff0c\u4f46\u53c8\u66f4\u5f37\u5927\uff0c\u56e0\u70ba List \u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u7684\u8cc7\u6599\u578b\u614b\uff0c\u800c\u901a\u5e38 Array \u53ea\u80fd\u5132\u5b58\u4e00\u7a2e\u8cc7\u6599\u578b\u614b\u3002

\u4f46 Python \u4e5f\u6709\u63d0\u4f9b\u771f\u6b63\u7684\u300c\u9663\u5217\u300d\uff0c\u4f46\u6211\u5148\u4e0d\u63d0\uff0c\u8acb\u4f60\u7a0d\u5fae\u6709\u500b\u5370\u8c61\u5c31\u597d\u3002

\u984c\u5916\u8a71\uff0c\u7576\u521d\u5f9e C, C++, Java \u8f49\u5230 Python \u7684\u6642\u5019\uff0c\u771f\u5fc3\u89ba\u5f97\u600e\u9ebc\u53ef\u4ee5\u9019\u9ebc\u96a8\u4fbf\uff0c\u9023\u578b\u614b\u90fd\u4e0d\u7528\u5ba3\u544a\uff0c\u73fe\u5728\u89ba\u5f97\u771f\u9999\u3002

\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97\uff0c\u6211\u5011\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\uff0c\u6709\u63d0\u5230 range \u9019\u500b\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6574\u6578\u7684\u5e8f\u5217\uff0c\u4f8b\u5982: range(5) \u6703\u7522\u751f\u4e00\u500b\u5f9e \\(0\\) \u5230 \\(4\\) \u7684\u6574\u6578\u5e8f\u5217\u3002

list(range(5)) \u9019\u6a23\u5c31\u53ef\u4ee5\u5c07 range \u7269\u4ef6\u8f49\u63db\u6210 List\u3002

x = range(5)\nprint(type(x))\nnumbers = list(x)\nprint(numbers)\n
ouput
<class 'range'>\n[0, 1, 2, 3, 4]\n

\u90a3\u80fd\u5426\u628a\u5b57\u4e32\u8f49\u63db\u6210 List \u5462? \u7576\u7136\u53ef\u4ee5!

x = \"Signals\"\nprint(type(x))\nwords = list(x)\nprint(words)\n
ouput
<class 'str'>\n['S', 'i', 'g', 'n', 'a', 'l', 's']\n

Lazer Boomerang - Signals (Official Audio)

\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u4f55\u521d\u59cb\u5316\u4e00\u500b\u5143\u7d20\u90fd\u662f \\(0\\) \u7684\u4e32\u5217\u3002

n = 5\nzeros = [0] * n\nprint(zeros)\n
ouput
[0, 0, 0, 0, 0]\n

\u5982\u4f55\u5c07\u5169\u500b\u4e32\u5217\u5408\u4f75\u6210\u4e00\u500b\u4e32\u5217\u5462?

a = [1, 2, 3]\nb = [4, 5, 6]\nc = a + b\nprint(c)\n
ouput
[1, 2, 3, 4, 5, 6]\n

\u9084\u6709\u4e00\u500b\u65b9\u6cd5\uff0c\u5c31\u662f\u4f7f\u7528 extend \u9019\u500b\u65b9\u6cd5\uff0c\u9019\u5728\u5f8c\u9762\u6703\u4ecb\u7d39\u5230\u3002

\u6211\u76f8\u4fe1\u4ee5\u4e0a\u9019\u4e9b\u4f8b\u5b50\u80fd\u8b93\u4f60\u6293\u5230\u4e00\u4e9b\u6982\u5ff5\uff0c\u63a5\u4e0b\u4f86\u5c07\u4ecb\u7d39\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u8207\u65b9\u6cd5\uff0c\u8b93\u4f60\u66f4\u719f\u6089 List\u3002

"},{"location":"fundamental/python/lists/#operations","title":"Operations","text":""},{"location":"fundamental/python/lists/#accessing_elements","title":"Accessing elements","text":"

\u8981\u5b58\u53d6 List \u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7d22\u5f15\u503c\u5f9e \\(0\\) \u958b\u59cb\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528\u8ca0\u6578\uff0c\u8ca0\u6578\u7684\u7d22\u5f15\u503c\u662f\u5f9e\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u958b\u59cb\u7b97\u8d77\u3002

numbers = [1, 2, 3, 4, 5]\n\n# First element\nprint(numbers[0])\n# Last element\nprint(numbers[-1])\nprint(numbers[4])\n
ouput
1\n5\n5\n

\u8b93\u6211\u5011\u914d\u5408\u8ff4\u5708\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\n\nfor i in range(len(numbers)):\n    print(numbers[i], end=' ')\n
ouput
1 2 3 4 5\n

len \u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u4e32\u5217\u7684\u9577\u5ea6\uff0c\u4e5f\u5c31\u662f\u4e32\u5217\u4e2d\u5143\u7d20\u7684\u500b\u6578\u3002

\u4e0d\u66c9\u5f97\u4f60\u9084\u8a18\u4e0d\u8a18\u5f97 Repetiton Structures - Foreach \u7684\u5167\u5bb9\uff0c\u6211\u5011\u7528\u9019\u500b\u6982\u5ff5\u4f86\u5370\u51fa\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\n\nfor number in numbers:\n    print(number, end=' ')\n
ouput
1 2 3 4 5\n
"},{"location":"fundamental/python/lists/#slicing","title":"Slicing","text":"

\u5207\u7247(Slicing)\u53ef\u4ee5\u7528\u4f86\u53d6\u5f97\u4e32\u5217\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982:

numbers = [1, 2, 3, 4, 5, 6]\n\nprint(numbers[0:4])\nprint(numbers[:4])\nprint(numbers[:-2])\nprint(numbers[1:3])\nprint(numbers[:6:2])\nprint(numbers[::-1])\nprint(numbers[:])\n
ouput
[1, 2, 3, 4]\n[1, 2, 3, 4]\n[1, 2, 3, 4]\n[2, 3]\n[1, 3, 5]\n[6, 5, 4, 3, 2, 1]\n[1, 2, 3, 4, 5, 6]\n

@EditTime : 2024-01-31 21:56

\u4f60\u5f88\u8070\u660e\uff0c\u61c9\u8a72\u767c\u73fe\u4e86\u7528\u6cd5\u8207 range \u4e00\u6a23\uff0c\u5206\u5225\u662f\u8d77\u59cb\u7d22\u5f15\u503c\u3001\u7d50\u675f\u7d22\u5f15\u503c\u3001\u9593\u9694\uff0c\u5982\u679c\u4e0d\u5beb\u7684\u8a71\uff0c\u9810\u8a2d\u503c\u5206\u5225\u662f \\(0\\)\u3001\u4e32\u5217\u9577\u5ea6\u3001\\(1\\)\u3002

"},{"location":"fundamental/python/lists/#modifying_elements","title":"Modifying elements","text":"

\u8981\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u865f []\uff0c\u4e26\u5728\u4e2d\u62ec\u865f\u4e2d\u653e\u5165\u5143\u7d20\u7684\u7d22\u5f15\u503c\uff0c\u7136\u5f8c\u518d\u6307\u5b9a\u65b0\u7684\u503c\u3002

music_info = [\"Time_To_Pretend\", \"Lazer Boomerang\", \" 2019/4/26\", 1, False]\nmusic_info[0] = \"Time To Pretend\"\nmusic_info[3] = 2\nmusic_info[-1] = True\nprint(music_info)\n
ouput
['Time To Pretend', 'Lazer Boomerang', ' 2019/4/26', 2, True]\n

Lazer Boomerang - Time To Pretend (Official Audio)

\u6211\u5011\u53ef\u4ee5\u4fee\u6539\u4e32\u5217\u4e2d\u7684\u503c\uff0c\u9019\u7a31\u70ba\u53ef\u8b8a\u52d5\u7684(mutable)\uff0c\u800c\u5b57\u4e32\u5247\u4e0d\u884c\uff0c\u9019\u7a31\u70ba\u4e0d\u53ef\u8b8a\u52d5\u7684(immutable)\u3002

sad_cat = \"\ud83d\ude3f\"\nsad_cat[0] = \"\ud83d\ude38\"\n
ouput
TypeError: 'str' object does not support item assignment\n
"},{"location":"fundamental/python/lists/#checking_elements","title":"Checking elements","text":"

\u8981\u6aa2\u67e5\u4e32\u5217\u4e2d\u662f\u5426\u6709\u67d0\u500b\u5143\u7d20\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 in \u9019\u500b\u904b\u7b97\u5b50\u3002

numbers = [1, 2, 3, 4, 5]\nprint(1 in numbers)\nprint(6 not in numbers)\n
ouput
True\nTrue\n

Question

\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u4e32\u5217\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u6700\u58de\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?

\u7b54\u6848\u662f \\(n\\) \u6b21\u5594\uff0c\u56e0\u70ba\u4e00\u500b\u4e00\u500b\u627e\uff0c\u76f4\u5230\u627e\u5230\u6216\u662f\u627e\u5b8c\u70ba\u6b62\uff0c\u50cf\u662f\u4f8b\u5b50\u4e2d\u7684 6\u3002

\u5728\u6f14\u7b97\u6cd5(Algorithm)\u7684\u7ae0\u7bc0\u4e2d\u6703\u63d0\u5230\u6642\u9593\u8907\u96dc\u5ea6(Time Complexity)\u9019\u500b\u89c0\u5ff5\uff0c\u9019\u88e1\u5c31\u5148\u7d66\u4f60\u4e00\u500b\u5370\u8c61\u5c31\u597d\u4e86~

"},{"location":"fundamental/python/lists/#methods","title":"Methods","text":"

\u4f86\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u4e32\u5217\u65b9\u6cd5\u3002

"},{"location":"fundamental/python/lists/#append","title":"append","text":"

append \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u4e00\u500b\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.append(6)\nprint(numbers)\nnumbers.append([7, 8, 9])\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5, 6]\n[1, 2, 3, 4, 5, 6, [7, 8, 9]]\n

\u5e0c\u671b\u7b2c\u4e8c\u500b append \u6c92\u6709\u5687\u5230\u4f60\uff0c\u5b83\u53ef\u4ee5\u65b0\u589e\u4e00\u500b\u4e32\u5217\uff0c\u4f46\u662f\u9019\u500b\u4e32\u5217\u6703\u8b8a\u6210\u4e32\u5217\u4e2d\u7684\u4e00\u500b\u5143\u7d20\u3002\u5982\u679c\u4f60\u662f\u60f3\u8981\u628a 7\u30018\u30019 \u9019\u4e09\u500b\u5143\u7d20\u52a0\u5230\u4e32\u5217\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 extend \u9019\u500b\u65b9\u6cd5\u3002

"},{"location":"fundamental/python/lists/#extend","title":"extend","text":"

extend \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e32\u5217\u7684\u6700\u5f8c\u9762\u65b0\u589e\u53e6\u4e00\u500b\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.extend([6, 7, 8])\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5, 6, 7, 8]\n
"},{"location":"fundamental/python/lists/#pop","title":"pop","text":"

pop \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u88ab\u79fb\u9664\u7684\u5143\u7d20\u3002

fruits = [\"apple\", \"banana\", \"cherry\", \"durian\", \"elderberry\"]\nprint(fruits.pop())\nprint(fruits)\nprint(fruits.pop(1))\nprint(fruits)\n
ouput
elderberry\n['apple', 'banana', 'cherry', 'durian']\nbanana\n['apple', 'cherry', 'durian']\n

\u901a\u5e38\uff0c\u6211\u5011\u6703\u4f7f\u7528 pop \u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff0c\u56e0\u70ba\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u7d22\u5f15\u503c\u8d85\u51fa\u7bc4\u570d\u7684\u932f\u8aa4\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7d22\u5f15\u503c\uff0c\u4f86\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u4e0d\u5efa\u8b70\u3002

"},{"location":"fundamental/python/lists/#clear","title":"clear","text":"

clear \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u79fb\u9664\u4e32\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.clear()\nprint(numbers)\n
ouput
[]\n
"},{"location":"fundamental/python/lists/#reverse","title":"reverse","text":"

reverse \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u53cd\u8f49\u4e32\u5217\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers.reverse()\nprint(numbers)\n
ouput
[5, 4, 3, 2, 1]\n
"},{"location":"fundamental/python/lists/#sort","title":"sort","text":"

sort \u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u6392\u5e8f\u4e32\u5217\uff0c\u9810\u8a2d\u662f\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u5982\u679c\u8981\u7531\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u53ef\u4ee5\u6307\u5b9a reverse=True\u3002

numbers = [5, 4, 3, 2, 1]\nnumbers.sort()\nprint(numbers)\nnumbers.sort(reverse=True)\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5]\n[5, 4, 3, 2, 1]\n

\u751a\u81f3\u4f60\u53ef\u4ee5\u8a2d\u5b9a key\uff0c\u4f86\u6c7a\u5b9a\u6392\u5e8f\u7684\u4f9d\u64da\uff0c\u4f8b\u5982: key=len\uff0c\u5c31\u662f\u6839\u64da\u5143\u7d20 x \u7684 len(x) \u4f86\u6392\u5e8f\u3002

fruits = [\"apple\", \"banana\", \"watermelon\", \"pineapple\"]\nfruits.sort(key=len)\nprint(fruits)\n
ouput
['apple', 'banana', 'pineapple', 'watermelon']\n
"},{"location":"fundamental/python/lists/#copy","title":"copy","text":"

copy \u9019\u500b\u65b9\u6cd5\u56de\u50b3\u4e00\u500b\u6dfa\u8907\u88fd(Shallow Copy)\u7684\u4e32\u5217\u3002

numbers = [1, 2, 3, 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1] = 10\nprint(numbers_copy)\nprint(numbers)\n
ouput
[1, 2, 3, 4, 5]\n[1, 2, 3, 4, 5]\nAfter changing numbers[1]\n[1, 2, 3, 4, 5]\n[1, 10, 3, 4, 5]\n

\u9019\u500b\u4f8b\u5b50\u9084\u4e0d\u80fd\u770b\u51fa\u6dfa\u8907\u88fd\uff0c\u4fee\u6539\u4e86 numbers \u7684\u7b2c\u4e8c\u500b\u5143\u7d20\uff0cnumbers_copy \u4e0d\u6703\u8ddf\u8457\u6539\u8b8a\u3002

numbers = [1, [2, 3], 4, 5]\nnumbers_copy = numbers.copy()\nprint(numbers_copy)\nprint(numbers)\n\nprint(\"After changing numbers[1]\")\n\nnumbers[1][0] = 6\nprint(numbers_copy)\nprint(numbers)\n
ouput
[1, [2, 3], 4, 5]\n[1, [2, 3], 4, 5]\nAfter changing numbers[1]\n[1, [6, 3], 4, 5]\n[1, [6, 3], 4, 5]\n

\u90a3\u4ec0\u9ebc\u53c8\u662f\u6df1\u8907\u88fd(Deep Copy)\u5462? \u9019\u500b\u5c31\u8981\u7b49\u5230\u6211\u5011\u5b78\u5230\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08(Object-Oriented Programming)\u7684\u6642\u5019\uff0c\u518d\u4f86\u8ddf\u4f60\u4ecb\u7d39\uff0c\u6211\u4e0d\u61c9\u8a72\u6316\u5751\u7684\u3002

\u9084\u6709\u66f4\u591a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u53c3\u8003 Python Documentation - List\uff0c\u4f46\u6211\u4e0a\u9762\u4ecb\u7d39\u7684\u65b9\u6cd5\uff0c\u4f60\u81f3\u5c11\u8981\u6703\uff0c\u4f60\u4e5f\u8981\u990a\u6210\u770b\u5b98\u65b9\u6587\u4ef6\u7684\u7fd2\u6163\u3002

"},{"location":"fundamental/python/lists/#list_comprehension","title":"List Comprehension","text":"

\u4e32\u5217\u7d9c\u5408\u904b\u7b97(List comprehension)\u8b93\u4f60\u7528\u4e00\u884c\u7a0b\u5f0f\u78bc\uff0c\u5c31\u53ef\u4ee5\u5efa\u7acb\u51fa\u64c1\u6709\u67d0\u9805\u6027\u8cea\u5143\u7d20\u7684\u4e32\u5217\u3002

\u8209\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b\uff0c\u5176\u4e2d b[i] = a[i] ** 2\uff0c\u4e5f\u5c31\u662f b \u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0d\u61c9 a \u5143\u7d20\u7684\u5e73\u65b9\u3002

\u6211\u5011\u5148\u4f7f\u7528\u8ff4\u5708\u4f86\u5be6\u4f5c:

a = [1, 2, 3, 4, 5]\nb = []\n\nfor num in a:\n    b.append(num ** 2)\n\nprint(b)\n
ouput
[1, 4, 9, 16, 25]\n

\u518d\u4f86\u4f7f\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97:

a = [1, 2, 3, 4, 5]\nb = [num ** 2 for num in a]\n\nprint(b)\n
ouput
[1, 4, 9, 16, 25]\n

\u975e\u5e38\u5730\u512a\u96c5\uff0c\u6211\u5011\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u4e32\u5217 a\uff0c\u8f38\u51fa\u4e00\u500b\u4e32\u5217 b\uff0c\u5176\u4e2d b \u7684\u5143\u7d20\u90fd\u662f a \u5143\u7d20\u4e2d\u7684\u5076\u6578\u3002

a = [1, 2, 3, 4, 5]\nb = [num for num in a if num % 2 == 0]\n\nprint(b)\n
ouput
[2, 4]\n

\u9084\u8a18\u5f97 Operators - Bonus: map for input \u63d0\u5230\u7684 map \u55ce?

\u5148\u4f86\u8907\u7fd2\u4e00\u4e0b\uff0cmap \u53ef\u4ee5\u5c07\u4e00\u500b\u51fd\u5f0f\u5957\u7528\u5230\u4e00\u500b\u5e8f\u5217\u7684\u6bcf\u4e00\u500b\u5143\u7d20\uff0c\u4e26\u56de\u50b3\u4e00\u500b map \u7269\u4ef6\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 list \u5c07 map \u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\u3002

a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = map(int, a)\nprint(type(b))\n\nc = list(b)\nprint(c)\n
ouput
<class 'map'>\n[1, 2, 3, 4, 5]\n

@EditTime : 2024-02-01 23:21

\u90a3\u80fd\u4e0d\u80fd\u7528\u4e32\u5217\u7d9c\u5408\u904b\u7b97\u4f86\u5be6\u4f5c\u5462?

a = [\"1\", \"2\", \"3\", \"4\", \"5\"]\nb = [int(num) for num in a]\nprint(b)\n
ouput
[1, 2, 3, 4, 5]\n

\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002

\u9084\u6709\u5f88\u591a\u61c9\u7528\uff0c\u4f8b\u5982: \u4e32\u5217\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5c0f\u5beb\uff0c\u6211\u5011\u60f3\u8981\u628a\u5b83\u5011\u8f49\u63db\u6210\u5927\u5beb\u3002

lowercase = [\"a\", \"b\", \"c\", \"d\", \"e\"]\nuppercase = [letter.upper() for letter in lowercase]\nprint(uppercase)\n
ouput
['A', 'B', 'C', 'D', 'E']\n

\u6ce8\u610f\u56c9\uff0c\u9019\u908a\u7528\u5230\u7684\u662f\u65b9\u6cd5\u5594\u3002

"},{"location":"fundamental/python/lists/#nested_lists","title":"Nested Lists","text":"

\u5de2\u72c0\u4e32\u5217(Nested Lists)\uff0c\u5c31\u662f\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u4e5f\u662f\u4e32\u5217\u3002

"},{"location":"fundamental/python/lists/#2d","title":"2D","text":"

\u5148\u4f86\u8209\u4e00\u500b\u4f60\u53ef\u80fd\u807d\u904e\u7684\u540d\u8a5e\uff0c\u4e8c\u7dad\u9663\u5217(2D Array)\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5de2\u72c0\u4e32\u5217\u4f86\u5be6\u4f5c\u4e8c\u7dad\u9663\u5217\uff0c\u4f8b\u5982\u5169\u500b \\(3 \\times 3\\) \u7684\u4e8c\u7dad\u9663\u5217\uff0c\u4f86\u9032\u884c\u77e9\u9663\u52a0\u6cd5\u3002

matrix_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nmatrix_2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]\n\nmatrix_3 = [[0 for i in range(3)] for j in range(3)]\n\nfor i in range(3):\n    for j in range(3):\n        matrix_3[i][j] = matrix_1[i][j] + matrix_2[i][j]\n\nprint(matrix_3)\n
ouput
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]\n

\u6211\u5e0c\u671b matrix_3 \u7684\u521d\u59cb\u5316\u6c92\u6709\u5687\u5230\u4f60\uff0c\u8acb\u4f60\u628a [0 for i in range(3)] \u7576\u6210\u4e00\u500b\u6574\u9ad4 E\uff0c\u518d\u628a [E for j in range(3)] \u7576\u6210\u4e00\u500b\u6574\u9ad4\uff0c\u9019\u6a23\u5c31\u4e0d\u6703\u89ba\u5f97\u5f88\u8907\u96dc\u4e86\uff0ci, j \u90fd\u53ea\u662f\u8a08\u6578\u7528\u7684\u800c\u5df2\uff0c\u4e0d\u8981\u88ab\u5b83\u5011\u5687\u5230\u3002

"},{"location":"fundamental/python/lists/#jagged","title":"Jagged","text":"

\u92f8\u9f52\u72c0\u4e32\u5217(Jagged Lists)\uff0c\u5c31\u662f\u5141\u8a31\u5de2\u72c0\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7684\u9577\u5ea6\u4e0d\u4e00\u6a23\u3002

\u984c\u5916\u8a71\uff0c\u4e4b\u524d\u5728\u5b78 C \u7684\u6642\u5019\uff0c\u53ea\u80fd\u7528\u6307\u6a19\u4f86\u5be6\u4f5c\uff0c\u4f46\u7528\u8d77\u4f86\u771f\u7684\u5f88\u9ebb\u7169\u3002

jagged = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]\n\nfor row in jagged:\n    for elem in row:\n        print(elem, end=' ')\n    print()\n
ouput
1 2 3 \n4 5 \n6 7 8 9 \n

\u5e0c\u671b\u9019\u500b\u8ff4\u5708\u7684\u5beb\u6cd5\u4e0d\u6703\u5687\u5230\u4f60\uff0crow \u662f jagged \u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u4e32\u5217\uff0celem \u662f row \u4e2d\u7684\u4e00\u500b\u5143\u7d20\uff0c\u4e5f\u5c31\u662f\u4e00\u500b\u6574\u6578\u3002

Python \u7684\u8a9e\u6cd5\u771f\u7684\u5f88\u9748\u6d3b\uff0c\u4f46\u4e5f\u8981\u5f88\u5c0f\u5fc3\uff0c\u4ed6\u63d0\u4f9b\u4e86\u5f88\u591a\u5de5\u5177\uff0c\u4f46\u5982\u679c\u4f60\u4e0d\u6e05\u695a\u4ed6\u662f\u600e\u9ebc\u904b\u4f5c\u7684\uff0c\u5c31\u6703\u5bb9\u6613\u6703\u5beb\u51fa\u6548\u7387\u4f4e\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u4e5f\u662f\u96d9\u9762\u5203\uff0c\u6240\u4ee5\u8981\u591a\u591a\u7df4\u7fd2\uff0c\u591a\u591a\u601d\u8003\uff0c\u4f60\u53ef\u4ee5\u7684\u3002

"},{"location":"fundamental/python/lists/#practice","title":"Practice","text":"

Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578

Reference code

table = []\nfor i in range(6, 2 ** 13, 2):\n    s = 0\n    for j in range(1, i):\n        if i % j == 0:\n            s += j\n    if s == i:\n        table.append(i)\n\nwhile True:\n    N = int(input())\n    print(' '.join([str(x) for x in table if x <= N]))\n
\u5efa\u7acb\u4e00\u500b\u4e32\u5217 table\uff0c\u88e1\u9762\u653e\u4e86 \\(6\\) \u5230 \\(2^{13}\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\uff0c\u63a5\u8457\u8f38\u5165\u4e00\u500b\u6574\u6578 \\(N\\)\uff0c\u8f38\u51fa \\(1\\) \u5230 \\(N\\) \u4e4b\u9593\u7684\u5b8c\u7f8e\u6578\u3002

\u90a3\u9ebc \\(2^{13}\\) \u662f\u600e\u9ebc\u4f86\u7684?\u311c\uff0c\u6211\u5237\u5f8c\u53f0\u6e2c\u8cc7\u63a8\u6572\u4f86\u7684\u3002

join \u65b9\u6cd5\u53ef\u4ee5\u5c07\u5b57\u4e32\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u7528\u6307\u5b9a\u7684\u5b57\u4e32\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u908a\u7528\u7a7a\u767d\u5b57\u4e32 ' ' \u4f86\u9023\u63a5\u3002

\u9019\u7a2e\u6280\u5de7\u7a31\u70ba\u300c\u5efa\u8868\u300d\uff0c\u4e8b\u5be6\u4e0a\u5b8c\u7f8e\u6578\u5f88\u758f\u6563\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u81ea\u5df1\u5efa\u5b8c\u8868\u5f8c\u76f4\u63a5\u5beb\u6b7b\uff0c\u4f46\u9019\u6a23\u7684\u8a71\uff0c\u7a0b\u5f0f\u78bc\u5c31\u4e0d\u5177\u6709\u901a\u7528\u6027\u3002

table = [6, 28, 496, 8128]\n\nN = int(input())\nprint(' '.join(str(x) for x in table if x <= N))\n

\u5982\u679c\u5c31\u55ae\u7d14\u89e3\u984c\u7684\u8a71\uff0c\u9019\u6a23\u4e5f\u662f\u53ef\u4ee5\u7684\u3002

\u6b63\u898f\u7684\u984c\u76ee\u90fd\u6703\u898f\u7bc4\u6e2c\u8cc7\u7bc4\u570d\uff0c\u5982\u679c\u4f60\u4ee5\u5f8c\u4e0a\u5927\u5b78\u5f8c\u9047\u5230\u4e0d\u592a\u9748\u5149\u7684\u52a9\u6559\u6c92\u6709\u6a19\u6e2c\u8cc7\u7bc4\u570d\uff0c\u8a18\u5f97\u96fb\u6b7b\u4ed6\u5011\u3002

Itsa - [C_MM44-\u6613] The Numbers

Reference code
N, M = input().split()\n\ncnt = 0\nfor i in range(0, 6):\n    if M[i] == N[0] and M[i + 1] == N[1]:\n        cnt += 1\n\nprint(cnt)\n

\u5b57\u4e32\u4e5f\u53ef\u4ee5\u50cf\u662f\u4e32\u5217\u4e00\u6a23\uff0c\u4f7f\u7528\u7d22\u5f15\u503c\u4f86\u5b58\u53d6\u5143\u7d20\u3002

Itsa - [C_AR01-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 I

Reference code
arr = input().split()\nprint(' '.join(arr[::-1]))\n

arr.reverse() \u4e5f\u884c\uff0c\u4f46\u662f\u9019\u6a23\u6703\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u6539\u8b8a\u539f\u672c\u7684\u4e32\u5217\uff0c\u5c31\u4f7f\u7528 arr[::-1]\u3002

arr = input().split()\narr.reverse()\nprint(' '.join(arr))\n

Itsa - [C_AR03-\u6613] \u8a08\u7b97\u9663\u5217\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7acb\u65b9\u548c

Reference code
arr = map(int, input().split())\nans = 0\n\nfor num in arr:\n    ans += num ** 3\n\nprint(ans)\n

\u6211\u4e26\u6c92\u6709\u628a map \u7269\u4ef6\u8f49\u63db\u6210\u4e32\u5217\uff0c\u56e0\u70ba\u6211\u53ea\u9700\u8981\u4e00\u6b21\u8fed\u4ee3\uff0c\u6240\u4ee5\u76f4\u63a5\u7528 map \u7269\u4ef6\u5c31\u597d\u3002

\u4f86\u770b\u4e00\u884c\u89e3:

print(sum(int(x) ** 3 for x in input().split()))\n

\u53c3\u8003\u5c31\u597d\u3002

Itsa - [C_AR09-\u6613] \u5169\u6578\u5dee\u503c

Reference code
arr1 = list(map(int, input().split(',')))\narr1.sort()\narr2 = arr1.copy()\narr2.sort(reverse=True)\n\nmax_num,min_num  = 0, 0\nfor i in range(len(arr1) - 1, -1, -1):\n    # convert list to number\n    max_num = max_num * 10 + arr1[i]\n    min_num = min_num * 10 + arr2[i]\n\nprint(max_num - min_num)\n

\u4e0d\u61c2\u6392\u5e8f\u6c92\u95dc\u4fc2\uff0c\u6211\u5011\u5148\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u4ee5\u5f8c\u6211\u6703\u6559\u4f60\u539f\u7406\u3002

\u800c\u9019\u984c\u7684\u91cd\u9ede\u662f\uff0c\u5982\u4f55\u5c07\u4e32\u5217\u8f49\u63db\u6210\u6578\u5b57\u3002\u5f9e\u500b\u4f4d\u6578\u958b\u59cb\uff0c\u6bcf\u6b21\u4e58 \\(10\\)\uff0c\u518d\u52a0\u4e0a\u4e0b\u4e00\u500b\u6578\u5b57\u3002

Itsa - [C_AR022-\u6613] \u5b57\u6bcd\u51fa\u73fe\u7684\u983b\u7387

Reference code
string = input()\n\nfreq = [0] * 26\n\nfor c in string:\n    if c.isalpha():\n        freq[ord(c.lower()) - ord('a')] += 1\n\nprint(' '.join(str(x) for x in freq))\n

\u9019\u984c\u9700\u8981\u4e00\u500b\u5148\u5099\u77e5\u8b58\uff0cASCII \u78bc\uff0cord \u51fd\u5f0f\u53ef\u4ee5\u53d6\u5f97\u5b57\u5143\u7684 ASCII \u78bc\u3002

\u518d\u5229\u7528 isalpha \u65b9\u6cd5\uff0c\u4f86\u5224\u65b7\u5b57\u5143\u662f\u5426\u70ba\u5b57\u6bcd\uff0c\u4ee5\u53ca lower \u65b9\u6cd5\uff0c\u4f86\u5c07\u5b57\u6bcd\u8f49\u63db\u6210\u5c0f\u5beb\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u5b57\u6bcd\u6620\u5c04\u5230 0 \u5230 25 \u4e4b\u9593\u3002

\u95dc\u65bc\u5b57\u4e32\u7684\u4e3b\u984c\uff0c\u5f8c\u9762\u6703\u518d\u8ddf\u4f60\u4ecb\u7d39\u3002

Itsa - [C_AR025-\u6613] \u8a08\u7b97ASCII\u5b57\u5143

Reference code
string = input()\n\nfreq = [0] * 128\n\n\nfor i in range(len(string)):\n    freq[ord(string[i])] += 1\n\nfor i in range(len(freq) - 1, -1, -1):\n    if freq[i] > 0:\n        print(f'{i} {freq[i]}')\n

\u9019\u984c\u8ddf\u4e0a\u4e00\u984c\u5f88\u50cf\u3002

Itsa - [C_AR029-\u96e3] \u6587\u5b57\u7de8\u78bc

Reference code
plain_text = input()\n\nN = len(plain_text)\nM = 1\nwhile M * M < N:\n    M += 1\n\narr = [[' ' for _ in range(M)] for _ in range(M)]\n\nfor i in range(N):\n    arr[i // M][i % M] = plain_text[i]\n\ncipher_text = []\nfor i in range(M):\n    for j in range(M):\n        cipher_text.append(arr[j][i])\n\nprint(''.join(cipher_text))\n

\u77e9\u9663\u8f49\u7f6e\uff0c\u6240\u4ee5 arr[j][i] \u8b8a\u6210 arr[i][j]\u3002

"},{"location":"fundamental/python/lists/#assignment","title":"Assignment","text":"

Itsa - [C_MM42-\u4e2d] \u6c42(-1)^(n+1)x[1/(2n-1)]\u7684\u548c

Itsa - [C_AR02-\u6613] \u4e00\u7dad\u9663\u5217\u53cd\u8f49 II

Itsa - [C_AR10-\u4e2d] \u65b0\u901a\u8a71\u8cbb\u7387

Itsa - [C_AR021-\u6613] \u6210\u7e3e\u7d71\u8a08

Itsa - [C_AR023-\u6613] \u5b57\u6839\u8207\u5b50\u5b57\u4e32

Itsa - [C_AR031-\u4e2d] \u4e00\u7dad\u77e9\u9663\u8868\u793a\u4e8c\u7dad\u5e73\u9762\u7a7a\u9593

Itsa - [C_AR33-\u6613] \u8f49\u7f6e\u77e9\u9663

Itsa - [C_AR34-\u6613] \u8eab\u5206\u8b49\u9a57\u8b49\u5668

Itsa - [C_AR35-\u6613] \u751f\u8096\u554f\u984c

Itsa - [C_AR41-\u6613] \u4e00\u6574\u6578\u5e8f\u5217\u6240\u542b\u4e4b\u6574\u6578\u500b\u6578\u53ca\u5e73\u5747\u503c

Itsa - [C_AR46-\u6613] \u9663\u5217\u5e73\u65b9\u548c\u554f\u984c

Itsa - [C_AR48-\u6613] \u6578\u5b57\u52a0\u5bc6

@EditTime : 2024-02-03 21:30

"},{"location":"fundamental/python/operators/","title":"Operators","text":""},{"location":"fundamental/python/operators/#introduction","title":"Introduction","text":"

\u5728\u9019\u500b\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u904b\u7b97\u5b50\uff0c\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u5c0d\u8b8a\u6578\u505a\u904b\u7b97\uff0c\u4f8b\u5982: \u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u7b49\u7b49\u3002

"},{"location":"fundamental/python/operators/#arithmetic_operators","title":"Arithmetic Operators","text":"

\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u6211\u5011\u719f\u6089\u7684\u56db\u5247\u904b\u7b97\u5c31\u662f\u7b97\u8853\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u5206\u5225\u662f +\u3001-\u3001*\u3001/\uff0c\u5206\u5225\u4ee3\u8868\u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3002

x, y = 10, 3\nprint(x + y)\nprint(x - y)\nprint(x * y)\nprint(x / y)\nz = x // y\nprint(z)\nprint(type(z))\nprint(x % y)\nprint(x ** y)\n
ouput
13\n7\n30\n3.3333333333333335\n3\n<class 'int'>\n1\n1000\n

\u6211\u76f8\u4fe1\u770b\u5b8c\u8f38\u51fa\u5f8c\uff0c\u4f60\u80fd\u5927\u81f4\u660e\u77ad\u5404\u500b\u904b\u7b97\u5b50\u7684\u4f5c\u7528\uff0c\u5176\u4e2d\u60f3\u8acb\u4f60\u7559\u610f * \u4e58\u6cd5\u904b\u7b97\u5b50\uff0c\u8acb\u4f60\u4e0d\u8981\u6253\u6210 x\uff0c\u4ee5\u53ca / \u8207 // \u7684\u5dee\u5225\uff0c\u5f8c\u8005\u6703\u5c07\u7d50\u679c\u5411\u4e0b\u53d6\u6574\u3002\u9084\u6709\u6bd4\u8f03\u7279\u5225\u7684 % \uff0c\u7559\u610f\u5230 10 = 3 * 3 + 1\uff0c\u5c31\u662f\u53d6\u9918\u6578\uff1b\u800c\u770b\u8d77\u4f86\u6700\u5947\u7279\u7684 ** \uff0c\u4ed6\u7684\u4f5c\u7528\u662f\u6c42\u51aa\u3002

\u6211\u5011\u4f86\u8907\u7fd2\u4e00\u4e0b\u570b\u5c0f\u6578\u5b78:

# Upper case for constant\nPI = 3.14\nr = 2\narea = PI * (r ** 2)\nperimeter = 2 * PI * r\nprint(f'Area: {area}, Perimeter: {perimeter}')\n\n# Sum of 1 to 100\ntotal = (1 + 100) * 100 // 2\nprint(f'The sum of 1 to 100 is {total}')\n

ouput
Area: 12.56, Perimeter: 12.56\nThe sum of 1 to 100 is 5050\n

\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c\u904b\u7b97\u5b50\u4e4b\u9593\u6709\u512a\u5148\u9806\u5e8f\uff0c\u7576\u4f60\u4e0d\u78ba\u5b9a\u7d50\u679c\u7684\u6642\u5019\uff0c\u8acb\u5584\u7528\u62ec\u865f\u3002

x = 'A' + 'n' + 'i' + 'm' + 'a' + 'l' + 's'\nprint(x)\n
ouput
Animals\n

Martin Garrix - Animals (Official Video)

\u518d\u8acb\u4f60\u7559\u610f\u4e00\u4ef6\u4e8b\uff0c\u4e0d\u540c\u985e\u5225\u5c0d\u65bc\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4e0d\u540c\u7684\uff0c\u4f8b\u5982\u5b57\u4e32\u7684 + \u6703\u5c07\u5169\u500b\u5b57\u4e32\u76f8\u9023\uff0c\u800c\u6574\u6578\u7684 + \u5247\u6703\u5c07\u5169\u500b\u6574\u6578\u76f8\u52a0\u3002

Question

  1. 2 ** 3 ** 2 \u7684\u7d50\u679c\u662f\u591a\u5c11?
  2. 2 ** (3 ** 2) \u7684\u7d50\u679c\u662f\u591a\u5c11?
  3. (2 ** 3) ** 2 \u7684\u7d50\u679c\u662f\u591a\u5c11?
"},{"location":"fundamental/python/operators/#comparison_operators","title":"Comparison Operators","text":"

\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7d50\u679c\u6703\u662f True \u6216 False\uff0c\u9019\u500b\u7d50\u679c\u6211\u5011\u7a31\u70ba\u5e03\u6797\u503c\u3002

\u6211\u5011\u4f86\u4ee5\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u6027\u8cea\u4f86\u505a\u4e00\u4e9b\u6bd4\u8f03\u904b\u7b97\u5b50\u7684\u7df4\u7fd2\u3002

a, b, c = 3, 4, 5\n\nprint(a < b)\nprint(a > b)\nprint(a <= b)\nprint(a >= b)\nprint(a ** 2 + b ** 2 == c ** 2)\nprint(a ** 2 + b ** 2 != c ** 2)\nprint((a + b) > c)\n
ouput
False\nTrue\nFalse\nTrue\nFalse\nTrue\n

\u9019\u908a\u8981\u63d0\u9192\u4f60\uff0c == \u662f\u6bd4\u8f03\u904b\u7b97\u5b50\uff0c\u800c = \u662f\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u5011\u7684\u610f\u601d\u662f\u4e0d\u4e00\u6a23\u7684\u3002

\u6700\u5f8c\u4f86\u770b\u4e00\u500b\u4f8b\u5b50:

age = 14\nprint(12 <= age <= 18)\n\nx, y, z = 1, 2, 3\nprint(x < y < z)\nprint(x < y and y < z)\n

ouput
True\nTrue\nTrue\n

\u5728Python\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6bd4\u8f03\u904b\u7b97\u5b50\u9023\u63a5\u8d77\u4f86\uff0c\u9019\u6a23\u7684\u5beb\u6cd5\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002

Question

  1. print(1 < 2 < 3 < 4 < 5) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. print(1 < 2 < 3 < 4 > 5) \u6703\u5370\u51fa\u4ec0\u9ebc?
"},{"location":"fundamental/python/operators/#logical_operators","title":"Logical Operators","text":"

\u6211\u5011\u7e7c\u7e8c\u5f80\u4e0b\u770b\uff0c\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u908f\u8f2f\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u591a\u500b\u5e03\u6797\u503c\u7d50\u5408\u6210\u4e00\u500b\u5e03\u6797\u503c\u3002

has_license = True\nis_drunk = False\nage = 18\n\nprint(f\"Can I drive a car? {has_license and not is_drunk and age >= 18}\")\nprint(not has_license)\nprint(not is_drunk)\nprint(age >= 18 or is_drunk or not has_license)\n
ouput
Can I drive a car? True\nFalse\nTrue\nTrue\n

\u9019\u908a\u8981\u63d0\u9192\u4f60\u7684\u662f\uff0c and \u53ea\u6709\u5728\u6240\u6709\u5e03\u6797\u503c\u90fd\u662f True \u7684\u6642\u5019\uff0c\u7d50\u679c\u624d\u6703\u662f True\uff0c\u800c or \u53ea\u8981\u6709\u4e00\u500b\u5e03\u6797\u503c\u662f True\uff0c\u7d50\u679c\u5c31\u6703\u662f True\u3002

\u56e0\u6b64\u6709\u6240\u8b02\u7684\u77ed\u8def\u6c42\u503c(Short-circuit Evaluation)\uff0c\u7576 and \u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f False\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a08\u7b97\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f False\uff1b\u800c or \u7684\u7b2c\u4e00\u500b\u5e03\u6797\u503c\u662f True\uff0c\u5f8c\u9762\u7684\u5e03\u6797\u503c\u5c31\u4e0d\u6703\u88ab\u8a55\u4f30\uff0c\u56e0\u70ba\u7d50\u679c\u4e00\u5b9a\u662f True\u3002

\u800c not \u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u53ea\u6703\u5c07\u5e03\u6797\u503c\u53cd\u8f49\u3002

Question

  1. print(not True and False) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. print(not True or False) \u6703\u5370\u51fa\u4ec0\u9ebc?
  3. print(not True and not False) \u6703\u5370\u51fa\u4ec0\u9ebc?
  4. print(not True or not False) \u6703\u5370\u51fa\u4ec0\u9ebc?
"},{"location":"fundamental/python/operators/#bitwise_operators","title":"Bitwise Operators","text":"

\u5728\u9019\u908a\u6211\u5011\u8981\u4ecb\u7d39\u4f4d\u5143\u904b\u7b97\u5b50\uff0c\u4ed6\u662f\u5c0d\u4e8c\u9032\u4f4d\u7684\u904b\u7b97\uff0c\u6211\u5011\u53ef\u4ee5\u7528 bin() \u4f86\u89c0\u5bdf\u4e8c\u9032\u4f4d\u7684\u7d50\u679c\u3002

a, b = 2, 3\nprint(f\"a={bin(a)}\")\nprint(f\"b={bin(b)}\")\n\nprint(a & b, bin(a & b))\nprint(a | b, bin(a | b))\nprint(a ^ b, bin(a ^ b))\nprint(~a, bin(~a))\n\nprint(a << 1, bin(a << 1))\nprint(a >> 1, bin(a >> 1))\n
ouput
a=0b10\nb=0b11\n2 0b10\n3 0b11\n1 0b1\n-3 -0b11\n4 0b100\n1 0b1\n

& \u662f\u4f4d\u5143\u7684 and\uff0c | \u662f\u4f4d\u5143\u7684 or\uff0c ^ \u662f\u4f4d\u5143\u9593\u7684\u9032\u884c\u4e92\u65a5\u6216\u904b\u7b97\uff0c ~ \u662f\u4f4d\u5143\u7684 not\uff0c << \u662f\u4f4d\u5143\u7684\u5de6\u79fb\uff0c >> \u662f\u4f4d\u5143\u7684\u53f3\u79fb\u3002

\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u60f3\u60f3\u6211\u5011\u8a72\u5982\u4f55\u5224\u65b7\u4e00\u500b\u6578\u5b57\u662f\u5947\u6578\u9084\u662f\u5076\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 % \u4f86\u5224\u65b7\uff0c\u4f46\u662f\u6211\u5011\u4e5f\u53ef\u4ee5\u7528\u4f4d\u5143\u904b\u7b97\u4f86\u5224\u65b7\u3002

a, b = 5678, 4843\nprint(f\"Is a even? {a % 2 == 0}\")\nprint(f\"Is b odd? {b & 1 == 1}\")\n
ouput
Is a even? True\nIs b odd? True\n

Question

  1. \u5982\u4f55\u5224\u65b7\u4e00\u500b\u6578\u5b57\u662f 2 \u7684\u51aa?
  2. \u5982\u4f55\u5f97\u5230\u4e00\u500b\u6578\u5b57\u7684 16 \u500d\uff0c\u4f46\u4e0d\u80fd\u7528 * \u4e58\u6cd5\u904b\u7b97\u5b50?
"},{"location":"fundamental/python/operators/#assignment_operators","title":"Assignment Operators","text":"

\u6211\u5011\u5df2\u7d93\u5b78\u6703\u4e86\u4e00\u4e9b\u904b\u7b97\u5b50\uff0c\u73fe\u5728\u6211\u5011\u8981\u4f86\u5b78\u7fd2\u4e00\u4e9b\u6307\u6d3e\u904b\u7b97\u5b50\uff0c\u4ed6\u53ef\u4ee5\u5c07\u904b\u7b97\u7d50\u679c\u6307\u5b9a\u7d66\u8b8a\u6578\u3002

\u5148\u4f86\u770b\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 += \u4f86\u5c07\u8b8a\u6578\u52a0\u4e0a\u67d0\u500b\u503c\uff0c\u9019\u500b\u904b\u7b97\u5b50\u53ef\u4ee5\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\u3002

x = 1\nx = x + 2\ny = 1\ny += 2\nprint(x, y)\n
ouput
3 3\n

\u518d\u4f86\u770b\u770b\u5176\u4ed6\u7684\u6307\u6d3e\u904b\u7b97\u5b50\u3002

x = 1\nx += 2\nprint(x)\nx -= 1\nprint(x)\nx *= 3\nprint(x)\nx //= 2\nprint(x)\nx **= 2\nprint(x)\nx <<= 1\nprint(x)\n
ouput
3\n2\n6\n3\n9\n18\n

\u5c0d\u4e86\uff0c\u8acb\u4f60\u5225\u5fd8\u8a18 = \u4e5f\u662f\u4e00\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u3002

x = y = z = 1\nprint(x, y, z)\n
ouput
1 1 1\n

\u5e0c\u671b\u9019\u500b\u4f8b\u5b50\u53ef\u4ee5\u8b93\u4f60\u66f4\u719f\u6089\u6307\u6d3e\u904b\u7b97\u5b50\u3002

Question

  1. \u6709\u6c92\u6709 and= \u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50?
  2. &= \u9019\u500b\u6307\u6d3e\u904b\u7b97\u5b50\u7684\u4f5c\u7528\u662f\u4ec0\u9ebc?
"},{"location":"fundamental/python/operators/#bonus_f-string_for_float","title":"Bonus: f-string for float","text":"

\u70ba\u4e86\u80fd\u8b93\u4f60\u7df4\u7fd2\u4e00\u4e9b\u984c\u76ee\uff0c\u6211\u5148\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u5370\u51fa\u6d6e\u9ede\u6578\u5230\u6307\u5b9a\u4f4d\u6578\u3002

\u5728 Say Hello to Python - Input \u4e2d\uff0c\u6709\u7a0d\u5fae\u63d0\u904e\uff0c\u5982\u679c\u4f60\u5b8c\u5168\u6c92\u5370\u8c61\uff0c\u8acb\u4f60\u56de\u53bb\u8907\u7fd2\u4e00\u4e0b\u3002

\u7d66\u4f60\u6d6e\u9ede\u6578 x \uff0c\u8acb\u4f60\u5370\u51fa x \u7684\u5e73\u65b9\u6839\u8207\u5e73\u65b9\uff0c\u4e26\u4e14\u53ea\u5370\u51fa\u5c0f\u6578\u9ede\u5f8c\u5169\u4f4d\u3002

x = 3.1415926\nprint(f\"The square root of {x} is {x ** 0.5:.2f}\")\nprint(f\"The square of {x} is {x ** 2:.2f}\")\n
ouput
The square root of 3.1415926 is 1.77\nThe square of 3.1415926 is 9.87\n

@EditTime : 2024-01-27 11:52

"},{"location":"fundamental/python/operators/#bonus_map_for_input","title":"Bonus: map for input","text":"

\u518d\u6b21\u8907\u7fd2 Variable and Input - Mutiple Input \u4e2d\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u7528 split() \u4f86\u5c07\u8f38\u5165\u7684\u5b57\u4e32\u5207\u5272\u6210\u591a\u500b\u5b57\u4e32\u3002

\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u5c07\u9019\u4e9b\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u600e\u9ebc\u505a\u5462?

a, b, c = input().split()\nprint(int(a) + int(b) + int(c))\n
input
1 2 3\n
ouput
6\n

\u96d6\u7136\u9019\u6a23\u5beb\u4e5f\u53ef\u4ee5\uff0c\u4f46\u662f\u5982\u679c\u6211\u5011\u60f3\u8981\u8f38\u5165\u5f88\u591a\u500b\u6578\u5b57\uff0c\u9019\u6a23\u5beb\u5c31\u6703\u5f88\u9ebb\u7169\uff0c\u9019\u6642\u5019\u6211\u5011\u53ef\u4ee5\u7528 map() \u4f86\u5e6b\u52a9\u6211\u5011\u3002

a, b, c = map(int, input().split())\nprint(a + b + c)\n
input
4 5 6\n
ouput
15\n

map(function, iterable) \u6703\u5c07 iterable \u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u4e1f\u9032 function \u4e2d\uff0c\u5728\u9019\u88e1\u7684 iterable \u662f input().split()\uff0c\u800c function \u662f int\uff0c\u56e0\u6b64 map(int, input().split()) \u6703\u5c07 input().split() \u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\u90fd\u8f49\u63db\u6210\u6574\u6578\u3002

\u4f60\u53ef\u4ee5\u5617\u8a66\u5c07\u4f7f\u7528\u5225\u7684\u51fd\u5f0f\uff0c\u4f8b\u5982 float \u6216 str\uff0c\u4f46\u8acb\u4f60\u8a18\u5f97\u4e0d\u8981\u52a0\u4e0a\u62ec\u865f\uff0c\u56e0\u70ba\u6211\u5011\u53ea\u662f\u8981\u5c07\u51fd\u5f0f\u7684\u540d\u7a31\u50b3\u9032\u53bb\uff0c\u800c\u4e0d\u662f\u8981\u57f7\u884c\u51fd\u5f0f\u3002

\u6211\u5011\u4f7f\u7528 Unpacking \u7684\u65b9\u5f0f\u4f86\u5c07 map() \u7684\u7d50\u679c\u6307\u6d3e\u7d66\u8b8a\u6578\u3002\u6211\u76f8\u4fe1\u4f60\u9084\u8a18\u5f97\u4ec0\u9ebc\u662f Unpacking \u5427?

\u5982\u679c\u4f60\u4e0d\u592a\u80fd\u7406\u89e3\uff0c\u4e5f\u6c92\u95dc\u4fc2\uff0c\u5148\u5b78\u6703\u600e\u9ebc\u7528\u5c31\u597d\u3002

"},{"location":"fundamental/python/operators/#practice","title":"Practice","text":"

\u6709\u4e86\u672c\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u5176\u5be6\u5df2\u7d93\u53ef\u4ee5\u505a\u5f88\u591a\u984c\u76ee\u4e86\uff0c\u6211\u5011\u4f86\u505a\u4e00\u4e9b\u7df4\u7fd2\u984c\u5427!

Itsa - [C_MM01-\u6613] \u8a08\u7b97\u68af\u578b\u9762\u7a4d

Reference code
a, b, h = map(int, input().split())\narea = (a + b) * h / 2\nprint(f\"Trapezoid area:{area}\")\n

Itsa - [C_MM02-\u6613] \u8a08\u7b97\u4e09\u89d2\u5f62\u9762\u7a4d

Reference code
a, b = map(int, input().split())\narea = a * b / 2\nprint(area)\n

Itsa - [C_MM04-\u6613] \u8a08\u7b97\u7e3d\u548c\u3001\u4e58\u7a4d\u3001\u5dee\u3001\u5546\u548c\u9918\u6578

Reference code
a, b = map(int, input().split())\n\nprint(f\"{a}+{b}={a + b}\")\nprint(f\"{a}*{b}={a * b}\")\nprint(f\"{a}-{b}={a - b}\")\nprint(f\"{a}/{b}={a // b}...{a % b}\")\n

Itsa - [C_MM06-\u6613] \u82f1\u54e9\u8f49\u516c\u91cc

Reference code
mile = int(input())\nkm = mile * 1.6\nprint(f\"{km:.1f}\")\n
"},{"location":"fundamental/python/operators/#assignment","title":"Assignment","text":"

Itsa - [C_MM07-\u6613] \u8a08\u7b97\u5e73\u65b9\u503c\u8207\u7acb\u65b9\u503c

Itsa - [C_MM08-\u6613] \u8a08\u7b97\u5169\u6578\u548c\u7684\u5e73\u65b9\u503c

Itsa - [C_MM10-\u6613] \u651d\u6c0f\u6eab\u5ea6\u8f49\u83ef\u5f0f\u6eab\u5ea6

Itsa - [C_MM11-\u6613] \u8cfc\u7968\u8a08\u7b97

Itsa - [C_MM12-\u6613] \u76f8\u9047\u6642\u9593\u8a08\u7b97

Itsa - [C_MM14-\u6613] \u8a08\u7b97\u6642\u9593\u7684\u7d44\u5408

@EditTime : 2024-01-28 22:03

"},{"location":"fundamental/python/repetition_structures/","title":"Repetition Structures","text":""},{"location":"fundamental/python/repetition_structures/#introduction","title":"Introduction","text":"

\u96fb\u8166\u6700\u6703\u505a\u7684\u4e8b\uff0c\u5c31\u662f\u91cd\u8907\u7684\u4e8b\u60c5\uff0c\u6240\u4ee5\u9019\u88e1\u8981\u8ddf\u4f60\u4ecb\u7d39\u8ff4\u5708(Loop)\uff0c\u8ff4\u5708\u53ef\u4ee5\u8b93\u6211\u5011\u91cd\u8907\u57f7\u884c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u800c\u4e0d\u7528\u4e00\u884c\u4e00\u884c\u7684\u5beb\u51fa\u4f86\u3002

\u5982\u679c\u4f60\u8981\u5370 \\(100\\) \u6b21 Hello World!\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?

# ...\nprint(\"Hello World!\")\nprint(\"Hello World!\")\nprint(\"Hello World!\")\n

\u4f60\u6703\u771f\u7684\u5beb \\(100\\) \u884c\u55ce?\u7576\u7136\u4e0d\u6703\uff0c\u4e0d\u7136\u4f60\u6703\u6c23\u6b7b\uff0c\u7136\u5f8c\u628a\u96fb\u8166\u7838\u4e86\u3002

"},{"location":"fundamental/python/repetition_structures/#for_loop","title":"For Loop","text":"

\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b for \u8ff4\u5708\u7684\u7528\u6cd5\u3002\u4e26\u8209\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5370\u51fa \\(1\\) \u5230 \\(5\\):

for i in range(1, 6, 1):\n    print(i, end=' ')\n
ouput
1 2 3 4 5\n

Time Loop - Purrple Cat

range\uff0c\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u6578\u5217\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u503c\u3001\u7d50\u675f\u503c\u3001\u9593\u9694\u503c\u3002\u9019\u500b\u51fd\u5f0f\u7684\u7528\u6cd5\u5982\u4e0b:

\u5982\u679c\u53ea\u6709\u4e00\u500b\u53c3\u6578\uff0c\u90a3\u5c31\u662f\u7d50\u675f\u503c\uff0c\u8d77\u59cb\u503c\u9810\u8a2d\u70ba \\(0\\)\uff0c\u9593\u9694\u503c\u9810\u8a2d\u70ba \\(1\\)\u3002

range(start=0, stop, step=1) \u4e26\u4e14\u6ce8\u610f\uff0cstop \u662f\u4e0d\u5305\u542b\u5728\u6578\u5217\u4e2d\u7684\u3002

"},{"location":"fundamental/python/repetition_structures/#foreach","title":"Foreach","text":"

\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u60f3\u628a\u5b57\u4e32 \"Speed_of_Light\" \u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u90fd\u5370\u51fa\u4f86\u3002

for c in \"Speed_of_Light\":\n    print(c, end=' ')\n

ouput

S p e e d _ o f _ L i g h t \n
DJ OKAWARI feat. \u4e8c\u5bae\u611b \u300cSpeed of Light\u300d

\u9019\u88e1\u7684\u8ff4\u5708\u66f4\u63a5\u8fd1\u6982\u5ff5\u4e0a\u7684 Foreach\uff0c\u6bcf\u6b21\u8ff4\u5708\u90fd\u6703\u53d6\u51fa\u5b57\u4e32\u4e2d\u7684\u4e00\u500b\u5b57\u5143\uff0c\u4e26\u4e14\u5c07\u4ed6\u653e\u5230 c \u4e2d\uff0c\u9019\u500b\u6982\u5ff5\u8acb\u4f60\u8a18\u8d77\u4f86\u3002

"},{"location":"fundamental/python/repetition_structures/#break_and_continue","title":"Break and Continue","text":"

\u518d\u8209\u500b\u4f8b\u5b50\uff0c\u8a08\u7b97 \\(\\sum_{i=1}^n{i}=1+2+\\cdots+n\\)

n = int(input())\ntotal = 0\n\n# 1 + 2 + ... + n\nfor i in range(n + 1):\n    total += i\n\nprint(total)\n
input
100\n
ouput
5050\n

\u63a5\u4e0b\u4f86\uff0c\u8ddf\u4f60\u4ecb\u7d39 break \u8207 continue\uff0c\u9019\u5169\u500b\u95dc\u9375\u5b57\u53ef\u4ee5\u7528\u4f86\u63a7\u5236\u8ff4\u5708\u7684\u57f7\u884c\u3002

break \u53ef\u4ee5\u7528\u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u800c continue \u5247\u662f\u5f37\u5236\u8df3\u5230\u4e0b\u4e00\u6b21\u8ff4\u5708\u3002

for i in range(1, 11):\n    if i == 5:\n        break\n    print(i, end=' ')\n
ouput
1 2 3 4\n

\u53ef\u4ee5\u767c\u73fe\uff0ci == 5 \u7684\u6642\u5019\uff0c\u8ff4\u5708\u5c31\u88ab\u5f37\u5236\u4e2d\u65b7\u4e86\u3002

for i in range(1, 11):\n    if i == 5:\n        continue\n    print(i, end=' ')\n
ouput
1 2 3 4 6 7 8 9 10\n

\u53ef\u4ee5\u767c\u73fe\uff0c5 \u88ab\u8df3\u904e\u4e86\u3002

"},{"location":"fundamental/python/repetition_structures/#nested_loop","title":"Nested Loop","text":"

\u63a5\u8457\u4ecb\u7d39\u5de2\u72c0\u8ff4\u5708(Nested Loop)\uff0c\u4e5f\u5c31\u662f\u8ff4\u5708\u88e1\u9762\u9084\u6709\u8ff4\u5708\u3002\u505a\u500b\u6bd4\u55bb\u7684\u8a71\uff0c\u5982\u679c\u662f\u5169\u5c64\u7684\u8ff4\u5708\uff0c\u5c31\u50cf\u662f\u6642\u91dd\u8207\u5206\u91dd\uff0c\u5167\u5c64\u8ff4\u5708\u6bcf\u8dd1\u4e00\u5708\uff0c\u5916\u5c64\u8ff4\u5708\u624d\u8dd1\u4e00\u683c\u3002

\u6211\u5011\u4f86\u5370\u4e00\u500b\u76f4\u89d2\u4e09\u89d2\u5f62\u5427\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u5370\u51fa \\(n\\) \u5c64\u7684\u76f4\u89d2\u4e09\u89d2\u5f62\uff0c\u5c07\u6700\u9802\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(0\\)\uff0c\u6700\u5e95\u7aef\u7684\u90a3\u4e00\u5c64\u7de8\u865f\u70ba \\(n-1\\)\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u5c64\u6709 \\(i+1\\) \u500b\u661f\u661f *\u3002

n = int(input())\n\nfor i in range(n):\n    for j in range(i + 1):\n        print('*', end='')\n    print()\n

\u6211\u7a31\u5167\u5c64\u7684\u8ff4\u5708\u53eb\u505a j \u8ff4\u5708\uff0c\u5916\u5c64\u7684\u8ff4\u5708\u53eb\u505a i \u8ff4\u5708\u3002 j \u8ff4\u5708\u63a7\u5236\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\uff0c\u800c i \u8ff4\u5708\u5247\u63a7\u5236\u7e3d\u5171\u6709\u5e7e\u5c64\u3002\u6bcf\u4e00\u5c64\u7684\u661f\u661f\u6578\u91cf\u90fd\u662f i + 1\uff0c\u6700\u5f8c\u6703\u63db\u884c\u3002

input
5\n
output
*\n**\n***\n****\n*****\n

\u518d\u4f86\u770b\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u4e5d\u4e5d\u4e58\u6cd5\u8868\u3002

for i in range(1, 10):\n    for j in range(1, 10):\n        print(f\"{i} * {j} = {i * j}\", end='\\t')\n    print()\n
output
1 * 1 = 1   1 * 2 = 2   1 * 3 = 3   1 * 4 = 4   1 * 5 = 5   1 * 6 = 6   1 * 7 = 7   1 * 8 = 8   1 * 9 = 9   \n2 * 1 = 2   2 * 2 = 4   2 * 3 = 6   2 * 4 = 8   2 * 5 = 10  2 * 6 = 12  2 * 7 = 14  2 * 8 = 16  2 * 9 = 18  \n3 * 1 = 3   3 * 2 = 6   3 * 3 = 9   3 * 4 = 12  3 * 5 = 15  3 * 6 = 18  3 * 7 = 21  3 * 8 = 24  3 * 9 = 27  \n4 * 1 = 4   4 * 2 = 8   4 * 3 = 12  4 * 4 = 16  4 * 5 = 20  4 * 6 = 24  4 * 7 = 28  4 * 8 = 32  4 * 9 = 36  \n5 * 1 = 5   5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25  5 * 6 = 30  5 * 7 = 35  5 * 8 = 40  5 * 9 = 45  \n6 * 1 = 6   6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36  6 * 7 = 42  6 * 8 = 48  6 * 9 = 54  \n7 * 1 = 7   7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49  7 * 8 = 56  7 * 9 = 63  \n8 * 1 = 8   8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64  8 * 9 = 72  \n9 * 1 = 9   9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81  \n

i \u63a7\u5236\u5217(Row)\uff0cj \u63a7\u5236\u884c(Column)\uff0c\u6bcf\u4e00\u5217\u7684\u6578\u5b57\u90fd\u662f i\uff0c\u6bcf\u4e00\u884c\u7684\u6578\u5b57\u90fd\u662f j\uff0c\u6240\u4ee5 i * j \u5c31\u662f\u76f8\u61c9\u7684\u4e58\u7a4d\u3002

\u71b1\u8eab\u5b8c\u4e86\uff0c\u4f86\u770b\u4e00\u500b\u7a0d\u5fae\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57\uff0c\u5224\u65b7\u4ed6\u662f\u4e0d\u662f\u8cea\u6578\u3002

\u9019\u500b\u4f8b\u5b50\u6703\u662f\u7b49\u7b49\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\u7684\u57fa\u790e\uff0c\u6240\u4ee5\u8acb\u4ed4\u7d30\u770b\u3002

n = int(input())\nis_prime = True\n\nfor i in range(2, n):\n    if n % i == 0:\n        is_prime = False\n        break\n\nif is_prime:\n    print(\"Yes\")\nelse:\n    print(\"No\")\n
input
17\n4843\n
ouput
Yes\nNo\n

n \u5982\u679c\u53ef\u4ee5\u6574\u9664 1 \u8207\u81ea\u5df1\u4ee5\u5916\u7684\u6578\u5b57\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u7528 break \u4f86\u5f37\u5236\u8df3\u51fa\u8ff4\u5708\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8a08\u7b97\u3002

\u6211\u5011\u628a\u554f\u984c\u7528\u5f97\u66f4\u8907\u96dc\uff0c\u8f38\u5165\u4e00\u500b\u6578\u5b57 \\(n\\)\uff0c\u8f38\u51fa\u5728 \\([1, n]\\) \u4e4b\u9593\u6700\u5927\u7684\u8cea\u6578\u3002

\u5728\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u8b93 range \u5012\u8457\u6578\u56de\u4f86\u3002

n = int(input())\n\nfor i in range(n, 0, -1):\n    is_prime = True\n    for j in range(2, i):\n        if i % j == 0:\n            is_prime = False\n            break\n\n    if is_prime:\n        print(f\"{i} is the largest prime in [1, {n}]\")\n        break\n
input
100\n2\n
output
97 is the largest prime in [1, 97]\n2 is the largest prime in [1, 2]\n

i \u5217\u8209 \\([n, 1]\\) \u7684\u6578\u5b57\uff0c\u800c j \u5217\u8209 \\([2, i-1]\\) \u7684\u6578\u5b57\uff0c\u5982\u679c i \u53ef\u4ee5\u6574\u9664 j\uff0c\u90a3\u5c31\u4e0d\u662f\u8cea\u6578\uff0c\u99ac\u4e0a\u8df3\u51fa\u5167\u5c64 j \u8ff4\u5708\uff0c\u7e7c\u7e8c\u5217\u8209\u4e0b\u4e00\u500b\u6578\u5b57\u3002\u5982\u679c i \u662f\u8cea\u6578\uff0c\u90a3\u5c31\u5370\u51fa\u4f86\uff0c\u4e26\u4e14\u8df3\u51fa\u5916\u5c64 i \u8ff4\u5708\u3002

\u8acb\u4f60\u4ed4\u7d30\u7aef\u8a73\u9019\u5169\u500b\u4f8b\u5b50\u4e2d\u5f37\u8abf\u7684\u884c\u6578\u3002

@EditTime : 2024-01-30 16:33

"},{"location":"fundamental/python/repetition_structures/#while_loop","title":"While loop","text":"

\u63a5\u8457\u4f86\u4ecb\u7d39 while \u8ff4\u5708\uff0c\u4ed6\u7684\u4f7f\u7528\u5834\u666f\u662f\u7576\u4f60\u4e0d\u77e5\u9053\u8ff4\u5708\u8981\u57f7\u884c\u5e7e\u6b21\u7684\u6642\u5019\uff0c\u5c31\u53ef\u4ee5\u7528 while \u8ff4\u5708\uff0c\u4f46\u662f\u5225\u5beb\u51fa\u7121\u7aae\u8ff4\u5708(Infinite Loop)\u5594\u3002

\u4f46\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u5148\u4f86\u770b\u5982\u4f55\u7528 while \u8ff4\u5708\u4f86\u5370\u51fa \\(1\\) \u5230 \\(5\\)\u3002

i = 1\nwhile i < 6:\n    print(i, end=\" \")\n    i += 1\n
ouput
1 2 3 4 5\n

\u56de\u9867\u4e00\u4e0b for loop\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe\u908f\u8f2f\u5176\u5be6\u4e00\u6a23\u3002

\u7576\u689d\u4ef6\u6210\u7acb\u7684\u6642\u5019\uff0c\u5c31\u6703\u57f7\u884c\u8ff4\u5708\uff0c\u76f4\u5230\u689d\u4ef6\u4e0d\u6210\u7acb\u3002

\u518d\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa \\(n!\\)

n = int(input())\ni = 1\nfact = 1\n\nwhile i <= n:\n    fact *= i\n    i += 1\n\nprint(fact)\n
input
5\n1\n
ouput
120\n1\n

\u4f46\u76ee\u524d\u9019\u5169\u500b\u4f8b\u5b50\u7121\u6cd5\u770b\u51fa while \u7684\u9b45\u529b\uff0c\u56e0\u70ba\u4f60\u90fd\u77e5\u9053\u8ff4\u5708\u4ec0\u9ebc\u6642\u5019\u6703\u7d50\u675f\uff0c\u6240\u4ee5\u8ddf\u4f60\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u554f\u984c:

\\[ \\text{The} \\ 3n+1 \\ \\text{Problem} \\ aka \\ \\text{Collatz Conjecture} \\]

\u8003\u62c9\u8332\u731c\u60f3 wikipedia

\u662f\u6307\u5c0d\u65bc\u6bcf\u4e00\u500b\u6b63\u6574\u6578\uff0c\u5982\u679c\u5b83\u662f\u5947\u6578\uff0c\u5247\u5c0d\u5b83\u4e583\u518d\u52a01\uff0c\u5982\u679c\u5b83\u662f\u5076\u6578\uff0c\u5247\u5c0d\u5b83\u9664\u4ee52\uff0c\u5982\u6b64\u5faa\u74b0\uff0c\u6700\u7d42\u90fd\u80fd\u5920\u5f97\u52301\u3002

\u90a3\u6211\u5011\u8981\u5beb\u7684\u7a0b\u5f0f\u662f\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\)\uff0c\u8f38\u51fa\u4ed6\u6703\u5728\u5e7e\u6b65\u5f8c\u8b8a\u6210 \\(1\\)\u3002

\\[ f(n)=\\begin{cases}\\frac{n}{2}, & \\text{if } n \\text{ is even} \\\\ 3n+1, & \\text{if } n \\text{ is odd}\\end{cases} \\]

\u4f9d\u7167\u5b9a\u7fa9\uff0c\u6211\u5011\u53ef\u4ee5\u5beb\u51fa\u4ee5\u4e0b\u7a0b\u5f0f\u78bc:

n = int(input())\nstep = 0\n\nwhile n != 1:\n    print(n, end=\" -> \")\n    if n % 2 == 0:\n        n = n // 2\n    else:\n        n = 3 * n + 1\n    step += 1\n\nprint(1)\nprint(f\"step = {step}\")\n
input
22\n1\n
ouput
22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1\nstep = 15\n1\nstep = 0\n

\u518d\u4f86\u4ecb\u7d39\u4e00\u500b\u7d93\u5178\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\gt 1\\)\uff0c\u8f38\u51fa \\(n\\) \u7684\u8cea\u56e0\u6578\u5206\u89e3\u3002

\u8acb\u4f60\u5148\u81ea\u5df1\u60f3\u4e00\u4e0b\uff0c\u518d\u770b\u770b\u53c3\u8003\u7a0b\u5f0f\u78bc\u3002

Example
n = int(input())\nfactor = 2\n\nwhile n != 1:\n    while n % factor == 0:\n        print(factor, end=' ')\n        n //= factor\n    factor += 1\n
input
20\n4843\n
ouput
2 2 5\n29 167\n

\u5167\u5c64\u7684 while \u8ff4\u5708\u6703\u4e00\u76f4\u57f7\u884c\uff0c\u76f4\u5230 n \u4e0d\u662f factor \u7684\u500d\u6578\u70ba\u6b62\u3002\u5916\u5c64\u7684 while \u8ff4\u5708\u5247\u662f\u5217\u8209\u6240\u6709\u7684\u8cea\u56e0\u6578\uff0c\u76f4\u5230 n \u8b8a\u6210 \\(1\\) \u70ba\u6b62\u3002

\u984c\u5916\u8a71\uff0c\u7576\u521d\u9047\u5230\u9019\u500b\u984c\u76ee\u7684\u6642\u5019\uff0c\u60f3\u4e86\u4e00\u4e0b\u7d50\u679c\u4e00\u6b21\u5c31AC\uff0c\u662f\u5f88\u6709\u6210\u5c31\u611f\u7684\u4e00\u4ef6\u4e8b\uff0c\u6240\u4ee5\u4e5f\u8acb\u4f60\u597d\u597d\u52aa\u529b\uff0c\u9010\u6b65\u57f9\u990a\u81ea\u4fe1\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7684\u3002

@EditTime : 2024-01-30 17:55

"},{"location":"fundamental/python/repetition_structures/#bonus_for_else_and_while_else","title":"Bonus: for ... else and while ... else","text":"

for \u8207 while \u8ff4\u5708\u90fd\u53ef\u4ee5\u642d\u914d else \u4f86\u4f7f\u7528\uff0c\u7576\u8ff4\u5708\u6b63\u5e38\u7d50\u675f\u7684\u6642\u5019(\u6c92\u6709 break )\uff0c\u5c31\u6703\u57f7\u884c else\u3002

\n

to be continued...

"},{"location":"fundamental/python/repetition_structures/#practice","title":"Practice","text":"

Itsa - [C_MM03-\u6613] \u5169\u6578\u7e3d\u548c

Reference code

while True:\n    a, b = map(int, input().split())\n    print(a + b)\n
\u9019\u984c\u6703\u51fa\u73fe\u5728\u9019\u88e1\uff0c\u55ae\u7d14\u662f\u56e0\u70ba\u8981\u91cd\u8907\u8f38\u5165\u3002

Itsa - [C_MM26-\u6613] \u8f38\u51fa 1x1\u30012x2\u3001...\u3001NxN\u4e4b\u7d50\u679c

Reference code
N = int(input())\n\nfor i in range(1, N + 1):\n    print(f\"{i}*{i}={i * i}\")\n

Itsa - [C_MM33-\u6613] \u627e1~N\u7684\u5b8c\u7f8e\u6578

Reference code

N = int(input())\n\nfor i in range(6, N + 1, 2):\n    s = 0\n    for j in range(1, i):\n        if i % j == 0:\n            s += j\n    if s == i:\n        if i == 6:\n            print(i, end='')\n        else:\n            print(' ' + str(i), end='')\nprint()\n
\u9019\u662f\u66b4\u529b\u89e3\uff0c\u6703TLE\uff0c\u4e0b\u4e00\u7ae0\u6703\u4ecb\u7d39\u5982\u4f55\u6700\u4f73\u5316\uff0c \u4f46\u5982\u679c\u4f7f\u7528 C++ \u7684\u8a71\uff0c\u76f8\u540c\u908f\u8f2f\u7684\u7a0b\u5f0f\u78bc\u662f\u53ef\u4ee5AC\u7684\u3002

Itsa - [C_MM34-\u6613] \u56e0\u6578\u554f\u984c

Reference code
N = int(input())\n\nprint(1, end=\"\")\nfor i in range(2, N + 1):\n    if N % i == 0:\n        print(f\" {i}\", end=\"\")\nprint()\n

@EditTime : 2024-01-30 18:40

"},{"location":"fundamental/python/repetition_structures/#assignment","title":"Assignment","text":"

Itsa - [C_MM21-\u6613] \u7b97\u968e\u4e58

Itsa - [C_MM25-\u6613] \u8a08\u7b97\u6b63\u6574\u6578\u88ab3\u6574\u9664\u4e4b\u6578\u503c\u4e4b\u7e3d\u548c

Itsa - [C_MM27-\u6613] \u8a08\u7b97\u5169\u6574\u6578\u9593\u6240\u6709\u6574\u6578\u7684\u7e3d\u548c

Itsa - [C_MM28-\u6613] \u8a08\u7b971\u5230N\u4e4b\u9593\u5c6c\u65bc5\u548c7\u7684\u500d\u6578

Itsa - [C_MM29-\u6613] \u6700\u5927\u8cea\u6578\u554f\u984c

Itsa - [C_MM30-\u6613] \u8cea\u6578\u5224\u5225

Itsa - [C_MM31-\u6613] \u8a08\u7b971~N\u5167\u80fd\u88ab2\u8ddf3\u6574\u9664\uff0c\u4f46\u4e0d\u80fd\u88ab12\u6574\u9664\u7684\u6574\u6578\u7e3d\u548c

Itsa - [C_MM40-\u6613] 1~N\u4e4b\u9593\u7684\u7e3d\u548c

Itsa - [C_MM49-\u6613] \u9023\u7e8c1\u7684\u500d\u6578

Itsa - [C_ST09-\u6613] \u661f\u865f\u77e9\u5f62\u8f38\u51fa

Itsa - [C_ST11-\u6613] \u661f\u865f\u83f1\u5f62\u8f38\u51fa

Itsa - [C_ST14-\u6613] \u6578\u5b57\u76f4\u89d2\u4e09\u89d2\u5f62\u8f38\u51fa

@EditTime : 2024-01-30 21:51

"},{"location":"fundamental/python/say_hello/","title":"Say Hello to Python!","text":""},{"location":"fundamental/python/say_hello/#first_program","title":"First Program","text":"

\u55e8\uff0c\u9019\u662f\u4f60\u7b2c\u4e00\u500b Python \u7a0b\u5f0f\uff0c\u5728 Pycharm \u4e2d\u5efa\u7acb\u4e00\u500b main.py \u6a94\u6848\uff0c\u4e26\u5728\u6a94\u6848\u4e2d\u6253\u4e0a :

# print(\"Hello Cat!\")\nprint(\"Hello World!\")\nprint('Hello Python!')\nprint(\"cheung4843\")\n
output
Hello World!\nHello Python!\ncheung4843\n

\u6309\u4e0b\u57f7\u884c\u5f8c\uff0c\u4f60\u7684\u63a7\u5236\u53f0(Console)\u5c07\u6703\u5370\u51fa Hello World! \u63a5\u8457\u63db\u884c\uff0c\u518d\u5370\u51fa Hello Python!\uff0c\u63a5\u8457\u518d\u5370\u51fa\u6211 cheung4843\uff0c\u518d\u63db\u884c\u3002

\u90a3 # print(\"Hello Cat!\") \u662f\u4ec0\u9ebc?\u4ee5\u4e95\u5b57\u865f\u70ba\u958b\u982d\u7684\uff0c\u88ab\u7a31\u70ba\u8a3b\u89e3\uff0c\u4ed6\u4e0d\u6703\u5728\u7a0b\u5f0f\u4e2d\u88ab\u57f7\u884c\uff0c\u4ed6\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7406\u89e3\u7a0b\u5f0f\u78bc\uff0c\u6216\u8005\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u66f4\u4f73\u3002

\u4e0d\u66c9\u5f97\u4f60\u662f\u5426\u6709\u767c\u73fe \"Hello World!\" \u8207 'Hello Python!' \u7684\u5dee\u7570?\u7576\u7136\u9664\u4e86\u5b57\u6bcd\u4e0d\u4e00\u6a23\u4e4b\u5916\uff0c\u9084\u6709\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u7684\u5dee\u5225\uff0c\u4f46\u4ed6\u5011\u90fd\u88ab\u7a31\u70ba\u5b57\u4e32(String)\uff0c\u5728 Python \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6210\u5c0d\u7684\u96d9\u5f15\u865f\u8207\u55ae\u5f15\u865f\u4f86\u8868\u9054\u4e00\u500b\u5b57\u4e32\uff0c\u4f46\u662f\u4e0d\u80fd\u6df7\u7528\uff0c\u4f8b\u5982 :

print(\"I love cats')\n
output
SyntaxError: unterminated string literal (detected at line 1)\n

\u57f7\u884c\u5f8c\uff0c\u4f60\u6703\u767c\u73fe\u63a7\u5236\u53f0\u8ddf\u4f60\u5831\u544a\u4e86\u932f\u8aa4\uff0c\u9019\u662f\u8a9e\u6cd5\u932f\u8aa4\uff0c\u800c\u5f8c\u7e8c\u5beb\u7a0b\u5f0f\u7684\u904e\u7a0b\u4e2d\uff0c\u4f60\u5c07\u906d\u9047\u8a31\u591a\u8a9e\u610f\u932f\u8aa4\u3002

\u90a3\u4f60\u77e5\u9053\u4ec0\u9ebc\u662f\u7a7a\u5b57\u4e32\u55ce?

print('')\nprint(\"\")\nprint(\"Above are nothing, right?\")\n
output
Above are nothing, right?\n

\u57f7\u884c\u5b8c\u5f8c\uff0c\u4f60\u5c31\u77e5\u9053\u7a7a\u5b57\u4e32\u662f\u4ec0\u9ebc\u4e86\u5427?

\u63a5\u8457\u6211\u5011\u4f86\u8b1b\u8b1b print \u9019\u500b\u6771\u897f\uff0c\u6211\u5011\u5728 Python \u4e2d\u7a31\u4ed6\u70ba\u51fd\u5f0f(Function)\uff0c\u6211\u5011\u8981\u5982\u4f55\u4f7f\u7528\u5462?\u4e5f\u5c31\u662f\u5728\u51fd\u5f0f\u7684\u540d\u7a31\u5f8c\u52a0\u4e0a\u4e00\u500b\u62ec\u865f()\u3002\u800c print \u662f Python \u5167\u5efa\u7d66\u6211\u5011\u7684\u300c\u5de5\u5177\u300d\uff0c\u5c31\u662f\u8aaa\uff0c\u6211\u5011\u4e0d\u77e5\u9053\u4ed6\u662f\u600e\u9ebc\u88ab\u9020\u51fa\u4f86\u7684\uff0c\u6211\u5011\u73fe\u968e\u6bb5\u53ea\u8981\u77e5\u9053\u600e\u9ebc\u7528\u5c31\u597d\uff0c\u6216\u8a31\u4f60\u53ef\u80fd\u807d\u904e\u4e00\u500b\u8001\u6389\u7259\u7684\u6bd4\u55bb\uff0c\u7a31\u51fd\u5f0f\u5c31\u50cf\u662f\u4e00\u500b\u9ed1\u76d2\u5b50\u3002

\u95dc\u65bc\u51fd\u5f0f(Function)\uff0c\u6211\u6703\u5728\u5f80\u5f8c\u7684\u7ae0\u7bc0\u8ddf\u4f60\u4ecb\u7d39\uff0c\u800c\u73fe\u5728\u4f60\u53ea\u8981\u77e5\u9053 print() \u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u5370\u51fa\u4f86\uff0c\u4e26\u63db\u884c\u3002

\u90a3\u5982\u679c\u6211\u4e0d\u60f3\u63db\u884c\u5462?

print(\"You\")\nprint(\"are\")\nprint(\"my\", end=' ')\nprint(\"special\", end='\\n')\nprint(\"1234\", end=\"5\")\nprint(\"---------\")\n
ouput
You\nare\nmy special\n12345---------\n

\u5728\u9019\u500b\u5947\u602a\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u4f60\u767c\u73fe\u6211\u5728 print \u7684\u62ec\u865f\u4e2d\u591a\u52a0\u4e86\u4e00\u500b end= \u7684\u6771\u897f\uff0c\u8b93 print \u5370\u5b8c\u524d\u9762\u7684\u6771\u897f\u5f8c\uff0c\u518d\u5370\u51fa\u7b49\u65bc\u5f8c\u7684\u6771\u897f\u3002

You are my special

\u90a3 \\n \u662f\u4ec0\u9ebc\u6771\u897f?\u4ed6\u662f\u8df3\u812b\u5b57\u5143(Escape Character)\u5bb6\u65cf\u4e2d\u7684\u4e00\u54e1\uff0c\u4f60\u73fe\u5728\u53ea\u8981\u77e5\u9053\u4ed6\u80fd\u5920\u63db\u884c\u3002

Note

  1. \u96d9\u5f15\u865f/\u55ae\u5f15\u865f\u570d\u4f4f\u7684\u5167\u5bb9\uff0c\u7a31\u70ba\u5b57\u4e32\uff0c\u4f8b\u5982 \"cheung4843\" \u8207 '114514 + 1919810' \uff0c\u4f46\u8a18\u4f4f\u55ae\u96d9\u5f15\u865f\u4e0d\u5f97\u6df7\u7528\u3002
  2. print(x, end=y) \u6703\u5370\u51fa x \uff0c\u518d\u5370\u51fa y\uff0c\u800c y \u9810\u8a2d\u70ba \\n \u4e5f\u5c31\u662f\u63db\u884c\u3002

Question

  1. \u90a3\u9ebc \"'\" \u8207 \"\"\" \u662f\u5408\u6cd5\u7684\u5b57\u4e32\u55ce?
  2. print() \u62ec\u865f\u5167\u6c92\u6709\u653e\u6771\u897f\u6703\u5370\u51fa\u4ec0\u9ebc?
"},{"location":"fundamental/python/selection_structures/","title":"Selection Structures","text":""},{"location":"fundamental/python/selection_structures/#introduction","title":"Introduction","text":"

\u5728\u6211\u5011\u4eba\u751f\u4e2d\uff0c\u6211\u5011\u6703\u9762\u81e8\u5f88\u591a\u9078\u64c7\uff0c\u6703\u6839\u64da\u7576\u4e0b\u7684\u60c5\u6cc1\uff0c\u505a\u51fa\u4e0d\u540c\u7684\u6c7a\u5b9a\uff0c\u800c\u7a0b\u5f0f\u4e5f\u662f\u4e00\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u60c5\u6cc1\uff0c\u57f7\u884c\u4e0d\u540c\u7684\u7a0b\u5f0f\u78bc\uff0c\u9019\u7a31\u70ba\u9078\u64c7\u7d50\u69cb(Selection Structures)\uff0c\u662f\u6d41\u7a0b\u63a7\u5236\u7684\u4e00\u7a2e\u3002

\u4f8b\u5982\uff0c\u6211\u6839\u64da\u5b78\u6e2c\u6210\u7e3e\uff0c\u4f86\u6c7a\u5b9a\u8981\u586b\u54ea\u4e9b\u5fd7\u9858\uff0c\u6216\u662f\u6211\u6839\u64da\u5929\u6c23\uff0c\u4f86\u6c7a\u5b9a\u8981\u4e0d\u8981\u5e36\u5098\u3002

"},{"location":"fundamental/python/selection_structures/#if_elif_else","title":"if ... elif ... else","text":"

\u5728\u5f80\u4e0b\u4e4b\u524d\uff0c\u8acb\u4f60\u5148\u56de\u60f3 Operators - Comparison Operators \u7684\u5167\u5bb9\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u6bd4\u8f03\u904b\u7b97\u5b50\u4f86\u5f97\u5230\u4e00\u500b\u5e03\u6797\u503c\uff0c\u800c if \u6703\u6839\u64da True \u6216 False \u4f86\u6c7a\u5b9a\u662f\u5426\u57f7\u884c\u67d0\u6bb5\u7a0b\u5f0f\u78bc\u3002

\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50:

\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u8f38\u51fa\u4ed6\u7684\u7d55\u5c0d\u503c\u3002

num = int(input(\"Enter a number: \"))\n\n# get the absolute value of the input\nif num < 0:\n    num = -num\n\nprint(num)\n
input
-1984\n1984\n
ouput
1984\n1984\n

Ivan Torrent - \"1984\" Lyrics Video

\u908f\u8f2f\u5f88\u7c21\u55ae\uff0c\u5982\u679c\u8f38\u5165\u7684\u6578\u5b57\u5c0f\u65bc\u96f6\uff0c\u90a3\u9ebc\u5c31\u5c07\u4ed6\u653e\u4e0a\u8ca0\u865f\u3002\u63d0\u9192\u4f60\u4e00\u4e0b\uff0c\u9019\u88e1\u7684 - \u8207 not \u4e00\u6a23\uff0c\u90fd\u662f\u4e00\u5143\u904b\u7b97\u5b50\uff0c

\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u53ea\u6709\u7528\u5230 if\uff0c\u63a5\u8457\u6211\u5011\u6709\u8acb else \u767b\u5834\u3002

\u8acb\u770b\u4e0b\u4e00\u500b\u4f8b\u5b50\uff0c\u8f38\u5165\u4e00\u500b\u6574\u6578\uff0c\u5224\u65b7\u4ed6\u662f\u5947\u6578\u9084\u662f\u5076\u6578\u3002

num = int(input(\"Enter a number: \"))\n\n# odd or even\nif num % 2 == 0:\n    print(\"Even\")\nelse:\n    print(\"Odd\")\n
input
1983\n-1982\n
ouput
Odd\nEven\n

Timecop1983 - On the Run

\u9019\u88e1\u6211\u5011\u7528\u5230\u4e86 else\uff0c\u7576 if \u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c else \u7684\u5167\u5bb9\u3002

\u6211\u5011\u518d\u4f86\u770b\u66f4\u8907\u96dc\u7684\u4f8b\u5b50\uff0c\u8f38\u5165\u4f60\u7684\u5206\u6578\uff0c\u8f38\u51fa\u4f60\u7684\u8a55\u50f9\uff0c\u56e0\u70ba\u6709\u5f88\u591a\u7a2e\u8a55\u50f9\uff0c\u6240\u4ee5\u9700\u8981\u7528\u5230 elif\u3002

\u7576 if \u7684\u689d\u4ef6\u4e0d\u6210\u7acb\u6642\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u9762 elif \u7684\u689d\u4ef6\uff0c\u5982\u679c elif \u7684\u689d\u4ef6\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c elif \u7684\u5167\u5bb9\uff0c\u5982\u679c\u76ee\u524d\u7684 elif \u7684\u689d\u4ef6\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u6aa2\u67e5\u4e0b\u4e00\u500b elif \u7684\u689d\u4ef6\uff0c\u5982\u679c\u6240\u6709\u7684 elif \u7684\u689d\u4ef6\u90fd\u4e0d\u6210\u7acb\uff0c\u5c31\u6703\u57f7\u884c else \u7684\u5167\u5bb9\u3002

\u9019\u6a23\u8b1b\u6216\u8a31\u6709\u9ede\u7e5e\u53e3\uff0c\u4f60\u53ef\u4ee5\u770b\u770b\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u61c9\u8a72\u5c31\u80fd\u7406\u89e3\u4e86\u3002

score = int(input(\"Enter your score: \"))\n\nif score >= 90:\n    print(\"A\")\n    print(\"Excellent!\")\nelif score >= 80:\n    print(\"B\")\nelif score >= 70:\n    print(\"C\")\nelif score >= 60:\n    print(\"D\")\nelse:\n    print(\"F\")\n
input
60\n90\n49\n
ouput
D\nA\nExcellent!\nF\n

\u63a5\u4e0b\u4f86\u7d66\u4f60\u4e00\u500b\u53ef\u80fd\u6703\u7591\u60d1\u7684\u4f8b\u5b50:

score = int(input(\"Enter your score: \"))\nif score >= 60:\n    print(\"D\")\nelif score >= 70:\n    print(\"C\")\nelif score >= 80:\n    print(\"B\")\nelif score >= 90:\n    print(\"A\")\n    print(\"Excellent!\")\nelse:\n    print(\"F\")\n
input
90\n
ouput
D\n

\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u5947\u602a\uff0c\u70ba\u4ec0\u9ebc\u8f38\u5165 90 \u6703\u5370\u51fa D\uff0c\u800c\u4e0d\u662f A\uff0c\u9019\u662f\u56e0\u70ba if \u7684\u689d\u4ef6\u6210\u7acb\u6642\uff0c\u5c31\u6703\u57f7\u884c if \u7684\u5167\u5bb9\uff0c\u5c31\u4e0d\u6703\u6aa2\u67e5 elif \u7684\u689d\u4ef6\u4e86\u3002

\u6240\u4ee5\uff0c\u4f60\u5728\u64b0\u5beb\u689d\u4ef6\u5f0f\u7684\u6642\u5019\uff0c\u8981\u6ce8\u610f\u9806\u5e8f\uff0c\u4ee5\u53ca\u78ba\u4fdd\u6bcf\u500b\u689d\u4ef6\u662f\u5426\u662f\u4e92\u65a5\u7684\u3002

score = int(input(\"Enter your score: \"))\n\nif 70 > score >= 60:\n    print(\"D\")\nelif 80 > score >= 70:\n    print(\"C\")\nelif 90 > score >= 80:\n    print(\"B\")\nelif 100 >= score >= 90:\n    print(\"A\")\n    print(\"Excellent!\")\nelse:\n    print(\"F\")\n
input
90\n4843\n55\n
ouput
A\nExcellent!\nF\nF\n

\u9019\u6a23\u5c31\u4e0d\u6703\u6709\u554f\u984c\u4e86\uff0c\u4f46\u662f\uff0c\u7576\u6211\u8f38\u5165\u8d85\u904e 100 \u7684\u5206\u6578\u6642\uff0c\u662f\u6703\u5370\u51fa F \u7684\uff0c\u4f60\u8a72\u600e\u9ebc\u89e3\u6c7a\u5462?

"},{"location":"fundamental/python/selection_structures/#nested_if","title":"Nested if","text":"

if \u7684\u5167\u5bb9\u53ef\u4ee5\u662f\u53e6\u4e00\u500b if\uff0c\u9019\u7a2e\u7d50\u69cb\u7a31\u70ba\u5de2\u72c0\u689d\u4ef6\u5f0f(Nested if)\u3002

\u8209\u4e00\u500b\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6709\u4e09\u500b\u6574\u6578 a, b, c \uff0c\u8acb\u4f60\u8f38\u51fa\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002

a, b, c = map(int, input().split())\nmax_one = None\n\nif a > b:\n    max_one = a\n    if c > max_one:\n        max_one = c\nelse:\n    max_one = b\n    if c > max_one:\n        max_one = c\n\nprint(max_one)\n
input
10 20 30\n12 12 12\n-3 -4 -5\n
ouput
30\n12\n-3\n

\u60f3\u6cd5\u5f88\u7c21\u55ae\uff0c\u5148\u8b93 a, b \u9032\u884c\u6bd4\u8f03\uff0c\u5f97\u5230\u8f03\u5927\u7684\u90a3\u500b\u6578\u5b57\uff0c\u518d\u8b93 c \u8207 max_one \u6bd4\u8f03\uff0c\u5f97\u5230\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002

\u9019\u88e1\u5077\u5077\u544a\u8a34\u4f60\u4e00\u500b\u795e\u5947\u5999\u5999\u5de5\u5177\uff0cmax()\uff0c\u4ed6\u6703\u627e\u51fa\u62ec\u865f\u5167\u7684\u6771\u897f\u4e2d\u6700\u5927\u7684\u90a3\u500b\u6578\u5b57\u3002

a, b, c = map(int, input().split())\nprint(max(a, b, c))\n
input
114514 1919 810\n
ouput
114514\n
"},{"location":"fundamental/python/selection_structures/#match_case","title":"match ... case","text":"

\u5728 Python 3.10 \u4e2d\uff0c\u65b0\u589e\u4e86 match ... case \uff0c\u4f46\u907a\u61be\u7684\u662f\u4e00\u4e9b\u53e4\u8001\u7684 Online Judge \u7684 Python \u7248\u672c\u592a\u820a\u4e0d\u80fd\u7528\uff0c\u4f46\u6211\u9084\u662f\u60f3\u4ecb\u7d39\u7d66\u4f60\u3002

\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5728 if ... elif ... else \u4e2d\u7684\u4f8b\u5b50:

month = int(input(\"Enter a month (1-12): \"))\n\n# which season?\nif 3 <= month <= 5:\n    print(\"Spring\")\nelif 6 <= month <= 8:\n    print(\"Summer\")\nelif 9 <= month <= 11:\n    print(\"Fall\")\nelif month == 12 or month == 1 or month == 2:\n    print(\"Winter\")\nelse:\n    print(\"Invalid month\")\n
input
4\n7\n10\n1\n13\n
ouput
Spring\nSummer\nFall\nWinter\nInvalid month\n

\u518d\u4f86\u770b\u770b match ... case \u7684\u7248\u672c:

month = int(input(\"Enter a month (1-12): \"))\n\nmatch month:\n    case 3 | 4 | 5:\n        print(\"Spring\")\n    case 6 | 7 | 8:\n        print(\"Summer\")\n    case 9 | 10 | 11:\n        print(\"Autumn\")\n    case 12 | 1 | 2:\n        print(\"Winter\")\n    case _:\n        print(\"Invalid month\")\n
input
270\n11\n3\n
ouput
Invalid month\nAutumn\nSpring\n

\u662f\u4e0d\u662f\u5f88\u7c21\u6f54\u5462?\u5982\u679c\u4f60\u6709\u5b78\u904e\u5176\u4ed6\u50cf\u662f C, C++, Java \u7b49\u8a9e\u8a00\uff0c\u4f60\u53ef\u80fd\u770b\u904e switch .. case\uff0c\u4f46\u662f match ... case \u66f4\u5f37\u5927!

\u95dc\u65bc match ... case \u6211\u5c31\u4ecb\u7d39\u5230\u9019\u88e1\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u4f60\u8981\u77e5\u9053 if ... elif ... else \u7684\u7528\u6cd5\u3002

@EditTime : 2024-01-29 12:32

"},{"location":"fundamental/python/selection_structures/#practice","title":"Practice","text":"

Itsa - [C_MM09-\u6613] \u8a08\u7b97 i \u6b21\u65b9\u7684\u503c

Reference code
i = int(input())\nif i > 31:\n    print(\"Value of more than 31\")\nelse:\n    print(1 << i)\n

Itsa - [C_MM13-\u6613] \u505c\u8eca\u8cbb\u8a08\u7b97

Reference code
h1, m1 = map(int, input().split())\nh2, m2 = map(int, input().split())\n\npaid = 0\nminutes = (h2 - h1) * 60 + (m2 - m1)\nif minutes > 240:\n    paid += ((minutes - 240) // 30) * 60\n    minutes = 240\nif minutes > 120:\n    paid += ((minutes - 120) // 30) * 40\n    minutes = 120\npaid += (minutes // 30) * 30\n\nprint(paid)\n

Itsa - [C_MM15-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u6b63\u65b9\u5f62\u7684\u7bc4\u570d\u5167

Reference code
x, y = map(int, input().split())\n\nif 0 <= x <= 100 and 0 <= y <= 100:\n    print(\"inside\")\nelse:\n    print(\"outside\")\n
"},{"location":"fundamental/python/selection_structures/#assignment","title":"Assignment","text":"

Itsa - [C_MM16-\u6613] \u5224\u65b7\u5ea7\u6a19\u662f\u5426\u5728\u5713\u5f62\u7684\u7bc4\u570d\u5167

Itsa - [C_MM19-\u6613] \u96fb\u8a71\u8cbb\u8a08\u7b97

Itsa - [C_MM24-\u6613] \u8a08\u7b97\u85aa\u6c34

Itsa - [C_MM32-\u6613] Armstrong\u6578

Itsa - [C_MM35-\u6613] \u5e73\u3001\u958f\u5e74\u5224\u5b9a

Itsa - [C_MM36-\u6613] \u5b63\u7bc0\u5224\u5b9a

Itsa - [C_MM37-\u6613] \u5224\u65b7\u5ea7\u6a19\u4f4d\u65bc\u4f55\u8655

Itsa - [C_MM38-\u6613] \u5224\u65b73\u6574\u6578\u662f\u5426\u80fd\u69cb\u6210\u4e09\u89d2\u5f62\u4e4b\u4e09\u908a\u9577

Itsa - [C_MM39-\u6613] \u5224\u65b7\u662f\u4f55\u7a2e\u4e09\u89d2\u5f62

Itsa - [C_MM46-\u6613] \u8907\u6578\u904b\u7b97

Itsa - [C_AR36-\u6613] \u661f\u5ea7\u67e5\u8a62

@EditTime : 2024-01-29 14:38

"},{"location":"fundamental/python/sets/","title":"Sets","text":""},{"location":"fundamental/python/sets/#introduction","title":"Introduction","text":"

\u5982\u679c\u7d66\u4f60\u4e00\u500b\u4e32\u5217\uff0c\u8acb\u4f60\u6aa2\u67e5\u88e1\u9762\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u4f60\u6703\u600e\u9ebc\u505a\u5462?\u4f60\u6703\u600e\u9ebc\u6a23\u78ba\u4fdd\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\u5462?

\u5728\u9019\u4e00\u7ae0\uff0c\u6211\u5011\u5c07\u6703\u5b78\u7fd2\u5230\u96c6\u5408(Set)\uff0c\u5b83\u6709\u4ee5\u4e0b\u7279\u6027:

  • \u7121\u5e8f: \u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u6c92\u6709\u9806\u5e8f\u7684
  • \u4e0d\u91cd\u8907: \u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684
  • \u53ef\u8b8a: \u53ef\u4ee5\u65b0\u589e\u3001\u522a\u9664\u5143\u7d20\uff0c\u4f46\u662f\u88e1\u9762\u7684\u5143\u7d20\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)
  • \u7528\u5927\u62ec\u865f{}\u4f86\u8868\u793a\uff0c\u88e1\u9762\u7684\u5143\u7d20\u7528\u9017\u865f,\u9694\u958b
"},{"location":"fundamental/python/sets/#create_a_set","title":"Create a Set","text":"

\u4f60\u53ef\u4ee5\u7528set()\u4f86\u5efa\u7acb\u4e00\u500b\u96c6\u5408\uff0c\u6216\u8005\u7528\u5927\u62ec\u865f{}\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:

a = set(\"FOREVER\")\nprint(a)\n\nb = [4, 8, 4, 3]\nc = set(b)\nprint(c)\n\nd = {(0, 1), (1, 2), (2, 3)}\nprint(d)\nprint(type(d))\n\ne = {}\nprint(type(e))\n
Output
{'R', 'F', 'O', 'E', 'V'}\n{8, 3, 4}\n{(2, 3), (1, 2), (0, 1)}\n<class 'set'>\n<class 'dict'>\n

SUHO(\uc218\ud638) _ FOREVER

\u4f60\u5f97\u6ce8\u610f\uff0c\u5efa\u7acb\u7a7a\u96c6\u5408\u7684\u6642\u5019\uff0c\u4f60\u5fc5\u9808\u7528set()\uff0c\u56e0\u70ba{}\u662f\u7528\u4f86\u5efa\u7acb\u7a7a\u5b57\u5178\u7684\u3002

\u4ee5\u53ca\u6240\u8b02\u7684\u7121\u5e8f\uff0c\u4f60\u8a66\u8457\u591a\u57f7\u884c\u5e7e\u6b21\u7a0b\u5f0f\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u6bcf\u6b21\u8f38\u51fa\u7684\u7d50\u679c\u90fd\u4e0d\u4e00\u6a23\u3002

\u4f60\u751a\u81f3\u53ef\u4ee5\u7528 Comprehension \u4f86\u5efa\u7acb\u96c6\u5408\u3002

a = {1, 2, 3}\nb = {3, 4, 5}\ndiff_b = {x for x in b if x not in a}\nprint(diff_b)\n\nc = {x ** 2 for x in range(-3, 4)}\nprint(c)\n
Output
{4, 5}\n{0, 9, 4, 1}\n
"},{"location":"fundamental/python/sets/#operations","title":"Operations","text":""},{"location":"fundamental/python/sets/#accessing_elements","title":"Accessing elements","text":"

\u56e0\u70ba\u96c6\u5408\u662f\u7121\u5e8f\u7684\uff0c\u6240\u4ee5\u4f60\u4e0d\u80fd\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5143\u7d20\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u7528in\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u53ca\u642d\u914dfor\u8ff4\u5708\u4f86\u904d\u6b77\u96c6\u5408\u3002

perfect_nums_set = {6, 28, 496, 8128, 33550336}\nprint(4843 in perfect_nums_set)\n\nfor num in perfect_nums_set:\n    print(num, end=' ')\n
Output
False\n33550336 8128 496 6 28 \n

Question

\u5c0d\u65bc\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u96c6\u5408\uff0c\u4f60\u8a8d\u70ba\u9700\u8981\u82b1\u5e7e\u500b\u6b65\u9a5f\u6aa2\u67e5\u67d0\u4e00\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728?

\u7b54\u6848\u53ef\u80fd\u6703\u8b93\u4f60\u9a5a\u8a1d\uff0c\u4e0d\u9700\u8981 \\(n\\) \u500b\u6b65\u9a5f\uff0c\u53ea\u8981\u5e7e\u500b\u6b65\u9a5f\u5c31\u597d\uff0c\u53ef\u4ee5\u8aaa\u662f\u99ac\u4e0a\u3002

"},{"location":"fundamental/python/sets/#union","title":"Union","text":"

\u4f60\u53ef\u4ee5\u7528| \u6216\u8005 union() \u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u806f\u96c6\u3002

a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.union(b))\nc = a | b\nprint(c)\n\na |= b\nprint(a)\n
Output
{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n{1, 2, 3, 4, 5, 6}\n
"},{"location":"fundamental/python/sets/#intersection","title":"Intersection","text":"

\u4f60\u53ef\u4ee5\u7528& \u6216\u8005 intersection() \u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\u3002

a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.intersection(b))\nc = a & b\nprint(c)\n\na &= b\nprint(a)\n
Output
{5}\n{5}\n{5}\n
"},{"location":"fundamental/python/sets/#difference","title":"Difference","text":"

\u4f60\u53ef\u4ee5\u7528- \u6216\u8005 difference() \u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u5dee\u96c6\u3002

a = {1, 3, 5}\nb = {2, 4, 5, 6}\n\nprint(a.difference(b))\nc = b - a\nprint(c)\n\na -= b\nprint(a)\n
Output
{1, 3}\n{2, 4, 6}\n{1, 3}\n

\u6211\u5c31\u8209\u9019\u4e09\u500b\u4f8b\u5b50\uff0c\u5176\u4ed6\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\u3002

"},{"location":"fundamental/python/sets/#methods","title":"Methods","text":""},{"location":"fundamental/python/sets/#adding_elements","title":"Adding elements","text":"

\u4f60\u53ef\u4ee5\u7528 add() \u4f86\u65b0\u589e\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002

nums_set = {1, 2, 3, 4, 5}\nnums_set.add(6)\nnums_set.add(3)\nprint(nums_set)\n
Output
{1, 2, 3, 4, 5, 6}\n

\u4e5f\u53ef\u4ee5\u7528 update() \u4f86\u65b0\u589e\u591a\u500b\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002

\u5176\u5be6\u5c31\u8ddf |= \u4e00\u6a23\u3002

nums_set = {1, 2, 3, 4, 5}\nnums_set.update({6, 7})\nprint(nums_set)\n\nnums_set.update([1, 8, 9], {10, 11})\nprint(nums_set)\n
Output
{1, 2, 3, 4, 5, 6, 7}\n{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\n
"},{"location":"fundamental/python/sets/#removing_elements","title":"Removing elements","text":"

\u4f60\u53ef\u4ee5\u7528 remove() \u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u6703\u62cb\u51fa\u932f\u8aa4\u3002

a = {1, 2, 3}\na.remove(1)\nprint(a)\na.remove(1)\nprint(a)\n
Output
{2, 3}\nKeyError: 1\n

\u6216\u8005\u7528 discard() \u4f86\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4e0d\u6703\u62cb\u51fa\u932f\u8aa4\u3002

a = {1, 2, 3}\na.discard(2)\nprint(a)\na.discard(2)\nprint(a)\n
Output
{1, 3}\n{1, 3}\n

\u90a3\u600e\u9ebc\u6e05\u7a7a\u5462?\u4f60\u53ef\u4ee5\u7528 clear() \u4f86\u6e05\u7a7a\u96c6\u5408\u3002

a = {1, 2, 3}\na.clear()\nprint(len(a))\n
Output
0\n

\u9084\u6709\u66f4\u591a\u7684\u65b9\u6cd5\uff0c\u8acb\u4f60\u81ea\u5df1\u67e5 Docs \u4f86\u5b78\u7fd2\uff0c\u4f46\u9019\u4e9b\u76ee\u524d\u61c9\u8a72\u5920\u4f60\u7528\u3002

"},{"location":"fundamental/python/sets/#practice","title":"Practice","text":"

Itsa - [C_AR20-\u6613] \u6aa2\u67e5\u6578\u503c\u662f\u5426\u6709\u91cd\u8907

Reference code

n = int(input())\nnum_set = set(input().split())\n\nif len(num_set) == n:\n    print(1)\nelse:\n    print(0)\n
\u6aa2\u67e5\u6709\u6c92\u6709\u91cd\u8907\u7684\u5143\u7d20\uff0c\u9019\u500b\u554f\u984c\u5c31\u5f88\u9069\u5408\u7528\u96c6\u5408\u4f86\u89e3\u6c7a\uff0c\u56e0\u70ba\u96c6\u5408\u88e1\u9762\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u53ea\u8981\u6aa2\u67e5\u96c6\u5408\u7684\u9577\u5ea6\u662f\u5426\u7b49\u65bc\u8f38\u5165\u4e32\u5217\u7684\u9577\u5ea6\u5c31\u597d\u3002

"},{"location":"fundamental/python/sets/#assignment","title":"Assignment","text":"

\u5728 Itsa \u4e0a\u4e0d\u592a\u597d\u627e\u984c\u76ee\uff0c\u6240\u4ee5\u4e7e\u8106\u9644\u4e0a\u89e3\u7b54\u3002

Itsa - [C_ST23-\u6613] \u76f8\u540c\u5b57\u96c6\u7684\u5b57\u4e32

Reference code

a, b = input().split(',')\na_set = set(str.lower(a))\na_set.discard(\" \")\nb_set = set(str.lower(b))\nb_set.discard(\" \")\n\nif a_set == b_set:\n    print(1)\nelse:\n    print(0)\n
\u9019\u88e1\u61c9\u8a72\u80fd\u611f\u53d7 discard() \u7684\u7528\u8655\uff0c\u984c\u76ee\u8981\u6c42\u4e0d\u5340\u5206\u5927\u5c0f\u5beb\uff0c\u6240\u4ee5\u6211\u5011\u5148\u628a\u5b57\u4e32\u8f49\u6210\u5c0f\u5beb\uff0c\u7136\u5f8c\u518d\u628a\u7a7a\u767d\u5b57\u5143\u79fb\u9664(\u4e0d\u7ba1\u6709\u6c92\u6709)\uff0c\u6700\u5f8c\u518d\u6aa2\u67e5\u5169\u500b\u96c6\u5408\u662f\u5426\u76f8\u7b49\u3002

Itsa - [C_ST82-\u6613] \u4ea4\u96c6

Itsa - [C_ST83-\u6613] \u806f\u96c6

Itsa - [C_ST84-\u6613] \u5dee\u96c6

Itsa - [C_AR192-\u6613] \u5224\u65ad\u4efb\u610f\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\u662f\u5426\u6709\u91cd\u8907

@EditTime : 2024-02-06 23:16

"},{"location":"fundamental/python/tuples/","title":"Tuples","text":""},{"location":"fundamental/python/tuples/#introduction","title":"Introduction","text":"

\u6709\u4e86\u4e0a\u4e00\u7ae0\u7684\u57fa\u790e\u5f8c\uff0c\u6211\u76f8\u4fe1\u5e8f\u5c0d(Tuple)\u5c0d\u4f60\u4f86\u8aaa\u4e0d\u6703\u56f0\u96e3\uff0c\u56e0\u70ba\u5b83\u8ddf\u4e32\u5217(List)\u5f88\u50cf\uff0c\u53ea\u662f\u5b83\u662f\u7528\u5c0f\u62ec\u865f()\u4f86\u8868\u793a\uff0c\u800c\u4e14\u5b83\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\u3002\u9019\u8868\u793a\u4f60\u4e0d\u80fd\u65b0\u589e\u3001\u522a\u9664\u6216\u4fee\u6539\u88e1\u9762\u7684\u5143\u7d20\u3002\u90a3\u65e2\u7136\u5982\u6b64\uff0c\u70ba\u4ec0\u9ebc\u9084\u8981\u7528\u5b83\u5462?

"},{"location":"fundamental/python/tuples/#create_a_tuple","title":"Create a Tuple","text":"

\u8981\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\u6709\u5f88\u591a\u7a2e\u65b9\u6cd5\uff0c\u6700\u7c21\u55ae\u7684\u5c31\u662f\u7528\u5c0f\u62ec\u865f()\u628a\u5143\u7d20\u5305\u8d77\u4f86\uff0c\u7528\u9017\u865f,\u9694\u958b\u5c31\u597d\u4e86\u3002

\u4f46\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f60\u6703\u767c\u73fe\uff0c\u53ea\u6709\u4e00\u500b\u5143\u7d20\u6642\uff0c\u4f60\u5fc5\u9808\u5728\u5f8c\u9762\u52a0\u4e0a\u9017\u865f\u3002\u9019\u908a\u4f60\u81ea\u5df1\u8a66\u8a66\u770b\uff0c\u61c9\u8a72\u90fd\u80fd\u7406\u89e3\u3002

empty = ()\none = (1,)\ntwo = (1, 2)\nprint(len(empty), len(one), len(two))\nprint(type(empty))\n\nodd = 1, 3, 5\nprint(odd)\nprint(type(odd))\n
Output
0 1 2\n<class 'tuple'>\n(1, 3, 5)\n(1, 3, 5)\n<class 'tuple'>\n

\u4f60\u4e5f\u53ef\u4ee5\u7528tuple()\u4f86\u5efa\u7acb\u4e00\u500b\u5e8f\u5c0d\uff0c\u76f4\u63a5\u770b\u4f8b\u5b50:

lst = [1, 2, 3, 4, 5]\ntup = tuple(lst)\nprint(tup)\n\ns = \"know me...\"\ns_tup = tuple(s)\nprint(s_tup)\n
Output
(1, 2, 3, 4, 5)\n('k', 'n', 'o', 'w', ' ', 'm', 'e', '.', '.', '.')\n

\u516b\u6728\u6d77\u8389 \u300eknow me...\u300f

\u9084\u8a18\u5f97 List Comprehension \u55ce?\u6211\u5011\u5c07\u4e2d\u62ec\u865f\u6539\u6210\u5c0f\u62ec\u865f\u8a66\u8a66\u770b:

gen = (x ** 2 for x in range(5))\nprint(type(gen))\ntup = tuple(gen)\nprint(tup)\n
Output
<class 'generator'>\n(0, 1, 4, 9, 16)\n

\u4f60\u6703\u767c\u73fe\uff0c\u7d50\u679c\u4e26\u975e\u662f\u4f60\u9810\u671f\u7684\u5e8f\u5c0d\uff0c\u800c\u662f\u7522\u751f\u5668(Generator)\uff0c\u4f60\u9084\u9700\u8981\u4f7f\u7528tuple()\u4f86\u8f49\u63db\u3002

\u81f3\u65bc\u70ba\u4ec0\u9ebc\u8981\u7528\u7522\u751f\u5668(Generator)\uff0c\u6211\u6703\u5728\u672a\u4f86\u7684\u7ae0\u7bc0\u8ddf\u4f60\u8aaa\u660e\uff0c\u53c8\u6316\u5751\u4e86\u3002

\u90a3\u9ebc\u52a0\u6cd5\u8ddf\u4e58\u6cd5\u5462?\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u52a0\u6cd5\u4f86\u5408\u4f75\u5169\u500b\u5e8f\u5c0d\uff0c\u7528\u4e58\u6cd5\u4f86\u8907\u88fd\u5e8f\u5c0d\u3002

a = (1, 2, 3)\nb = 4, 5, 6\nc = a + b\nprint(c)\n\nd = a * 2 + b\nprint(d)\n
Output
(1, 2, 3, 4, 5, 6)\n(1, 2, 3, 1, 2, 3, 4, 5, 6)\n
"},{"location":"fundamental/python/tuples/#operations","title":"Operations","text":""},{"location":"fundamental/python/tuples/#accessing_elements","title":"Accessing elements","text":"

\u8ddf\u4e32\u5217(List)\u4e00\u6a23\uff0c\u4f60\u53ef\u4ee5\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u7528\u8ca0\u7d22\u5f15\u4f86\u5f9e\u5f8c\u5b58\u53d6\u3002

t = (1, 2, 3, 4, 5)\nprint(t[0], t[-1])\n
Output
1 5\n
"},{"location":"fundamental/python/tuples/#slicing","title":"Slicing","text":"

\u540c\u6a23\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u5207\u7247\u4f86\u53d6\u5f97\u5e8f\u5c0d\u4e2d\u7684\u5b50\u5e8f\u5c0d\u3002

t = (1, 2, 3, 4, 5)\nprint(t[1:3])\nprint(t[:3])\nprint(t[3:])\nprint(t[:])\n
Output
(2, 3)\n(1, 2, 3)\n(4, 5)\n(1, 2, 3, 4, 5)\n
"},{"location":"fundamental/python/tuples/#modifying_elements","title":"Modifying elements","text":"

\u4f46\u662f\u4f60\u4e0d\u80fd\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u5143\u7d20\uff0c\u9019\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u662f\u6709\u6298\u8877\u7684\u65b9\u6cd5\u5566\uff0c\u5c31\u662f\u628a\u5e8f\u5c0d\u8f49\u63db\u6210\u4e32\u5217\uff0c\u518d\u8f49\u63db\u56de\u4f86\u3002

cat_tup = (\"\ud83d\ude38\", \"\ud83d\ude3a\", \"\ud83d\ude3b\", [\"\ud83d\ude3f\", \"\ud83d\ude40\"])\n\ncat_lst = list(cat_tup)\ncat_lst[1] = \"\ud83d\ude3c\"\ncat_tup = tuple(cat_lst)\nprint(cat_tup)\n\ncat_tup[3][0] = \"\ud83d\ude3e\"\nprint(cat_tup)\n\ncat_tup[1] = \"\ud83d\ude3d\"\nprint(cat_tup)\n
Output
('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3f', '\ud83d\ude40'])\n('\ud83d\ude38', '\ud83d\ude3c', '\ud83d\ude3b', ['\ud83d\ude3e', '\ud83d\ude40'])\nTypeError: 'tuple' object does not support item assignment\n

\u4f46\u4f60\u6709\u6c92\u6709\u89ba\u5f97\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u6211\u53ef\u4ee5\u4fee\u6539\u5e8f\u5c0d\u4e2d\u7684\u4e32\u5217\u5462?

"},{"location":"fundamental/python/tuples/#checking_elements","title":"Checking elements","text":"

\u4f60\u53ef\u4ee5\u7528 in \u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5728\u5e8f\u5c0d\u4e2d\uff0c\u9019\u8ddf\u4e32\u5217\u662f\u4e00\u6a23\u7684\u3002

fib = (0, 1, 1, 2, 3, 5, 8)\nprint(0 not in fib)\nprint(5 in fib)\n
Output
False\nTrue\n

Question

\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u5e8f\u5c0d\uff0c\u8981\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u5e8f\u5c0d\u4e2d\uff0c\u6700\u597d\u7684\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u6aa2\u67e5\u591a\u5c11\u6b21?

\u53c8\u662f\u719f\u6089\u7684\u554f\u984c\uff0c\u4f60\u53ef\u4ee5\u7684\u3002

"},{"location":"fundamental/python/tuples/#methods","title":"Methods","text":"

\u56e0\u70ba\u5e8f\u5c0d\u662f\u4e0d\u53ef\u8b8a\u7684(immutable)\uff0c\u6240\u4ee5\u53ea\u6709\u5169\u500b\u65b9\u6cd5\uff0c\u4e00\u500b\u662fcount()\uff0c\u4e00\u500b\u662findex()\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u8a66\u8a66\u770b\u3002

"},{"location":"fundamental/python/tuples/#count","title":"count","text":"

count() \u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u7576\u7136 List \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\uff0c\u4f46\u524d\u9762\u6211\u4e26\u6c92\u6709\u63d0\uff0c\u56e0\u70ba\u53ef\u4ee5\u653e\u5728\u9019\u88e1\u6c34\u5167\u5bb9(X

t = ((1, 2), (3, 4), 6, 6, [7, 8])\nprint(t.count(1))\nprint(t.count([7, 8]))\nprint(t.count(6))\n
Output
0\n1\n2\n
"},{"location":"fundamental/python/tuples/#index","title":"index","text":"

index() \u6703\u56de\u50b3\u62ec\u865f\u5167\u7684\u5143\u7d20\u5728\u5e8f\u5c0d\u4e2d\u7684\u7d22\u5f15\uff0c\u4e00\u6a23\u7684\uff0cList \u4e5f\u6709\u9019\u500b\u65b9\u6cd5\u3002

t = (\"Love Me Again\", \"John Newman\", 2013)\nprint(t.index(\"John Newman\"))\nprint(t.index(2014))\n
Output
1\nValueError: tuple.index(x): x not in tuple\n

John Newman - Love Me Again

"},{"location":"fundamental/python/tuples/#tuple_vs_list","title":"Tuple vs List","text":"
  • List
    • \u7528\u4e2d\u62ec\u865f[]\u8868\u793a
    • \u53ef\u8b8a\u7684(mutable)
    • \u4e0d\u53ef\u96dc\u6e4a(unhashable)
    • \u6548\u80fd\u8f03\u5dee
    • \u9069\u7528\u65bc\u983b\u7e41\u7684\u589e\u522a\u6539
  • Tuple
    • \u7528\u5c0f\u62ec\u865f()\u8868\u793a
    • \u4e0d\u53ef\u8b8a\u7684(immutable)
    • \u53ef\u96dc\u6e4a(hashable)
    • \u6548\u80fd\u8f03\u597d
    • \u9069\u7528\u65bc\u4e0d\u9700\u8981\u8b8a\u52d5\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\u5e38\u6578\u3001\u5ea7\u6a19

\u95dc\u65bc\u300c\u53ef\u96dc\u6e4a\u7684(hashable)\u300d\uff0c\u6211\u6703\u5728\u5b57\u5178(Dict)\u9019\u7ae0\u4e2d\u8ddf\u4f60\u8aaa\u660e\u3002

"},{"location":"fundamental/python/tuples/#practice","title":"Practice","text":"

Itsa - [C_AR04-\u6613] \u908a\u7de3\u5075\u6e2c

Reference code

N = int(input())\n\ndirs = ((0, 1), (0, -1), (1, 0), (-1, 0))\n\nfor k in range(N):\n    n, m = map(int, input().split())\n    pic = []\n    for _ in range(n):\n        pic.append(input().split())\n\n    for i in range(n):\n        for j in range(m):\n            if pic[i][j] == '0':\n                print('_ ', end='')\n            else:\n                is_edge = False\n                for d in dirs:\n                    x, y = i + d[0], j + d[1]\n                    if 0 <= x < n and 0 <= y < m and pic[x][y] == '0':\n                        is_edge = True\n                        break\n                if is_edge:\n                    print('0 ', end='')\n                else:\n                    print('_ ', end='')\n        print()\n\n    if k != N - 1:\n        print()\n
\u65b9\u5411 dirs \u5c31\u5f88\u9069\u7528\u5e8f\u5c0d\u4f86\u8868\u793a\uff0c\u56e0\u70ba\u5b83\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u6703\u8b8a\u52d5\u3002

\u672a\u4f86\u5728\u5b78\u5716\u5f62\u8d70\u8a2a\u7684\u6642\u5019\uff0c\u4f60\u6703\u4e00\u76f4\u770b\u5230\u9019\u7a2e\u5beb\u6cd5\u3002

\u5c0d\u4e86\uff0c \u8ff4\u5708\u8b8a\u6578 _ \u88ab\u7a31\u70ba\u6368\u68c4\u8b8a\u6578\uff0c\u8868\u793a\u4e0d\u9700\u8981\u7528\u5230\u9019\u500b\u8b8a\u6578\uff0c\u53ea\u662f\u70ba\u4e86\u914d\u5408\u8ff4\u5708\u8a9e\u6cd5\u800c\u5df2\u3002

"},{"location":"fundamental/python/tuples/#assignment","title":"Assignment","text":"

Itsa - [C_AR119-\u6613] \u5730\u96f7\u5371\u96aa\u6307\u6578\u8868

Itsa - [C_AR139-\u6613] \u9ec3\u91d1\u63a2\u6e2c

Itsa - [C-AR140-\u6613] \u6c42\u6700\u5927\u7ce7\u98df\u7522\u91cf

@EditTime : 2024-02-04 21:15

"},{"location":"fundamental/python/variable_and_input/","title":"Variable and Input","text":""},{"location":"fundamental/python/variable_and_input/#variable","title":"Variable","text":"

\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f86\u770b\u600e\u9ebc\u4f7f\u7528\u8b8a\u6578\uff0c\u4ee5\u53ca\u8f38\u5165\u81ea\u5df1\u60f3\u8981\u7684\u6771\u897f\u3002

x = 4843\nprint(x)\n\nx = \"Memory Reboot\"\nprint(x)\n\nx = '4843'\nprint(x)\n

\u6216\u8a31\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u795e\u5947\uff0c\u70ba\u4ec0\u9ebc x \u53ef\u4ee5\u8b8a\u6210\u6578\u5b57\uff0c\u53c8\u53ef\u4ee5\u8b8a\u6210\u5b57\u4e32\uff0c\u9084\u6709\u70ba\u4ec0\u9ebc\u7b2c\u4e00\u500b\u8207\u7b2c\u4e09\u500b\u7684\u8f38\u51fa\u6703\u662f\u76f8\u540c\u7684\u3002

\u518d\u4f86\u7528\u4e00\u4e9b old-school \u7684\u6bd4\u55bb\uff0c\u4f60\u53ef\u4ee5\u5c07 x \u60f3\u50cf\u6210\u4e00\u500b\u7bb1\u5b50\uff0c\u800c\u7b49\u865f\u53f3\u908a\u7684\u300c\u503c\u300d\u5c31\u662f\u7bb1\u5b50\u88e1\u88dd\u7684\u6771\u897f\u3002\u65e2\u7136\u7bb1\u5b50\u88e1\u9762\u7684\u6771\u897f\u53ef\u4ee5\u8b8a\uff0c\u90a3\u6211\u662f\u4e0d\u662f\u53ef\u4ee5\u8aaa x \u662f\u4e00\u500b\u8b8a\u6578\u5462?

\u4f46\u8acb\u4f60\u5148\u5fd8\u6389\u9019\u500b\u6bd4\u55bb\uff0c\u56e0\u70ba\u9019\u500b\u6bd4\u55bb\u4e26\u4e0d\u5b8c\u7f8e\uff0c\u4f46\u6211\u60f3\u4f60\u61c9\u8a72\u80fd\u5920\u7406\u89e3\uff0cx \u662f\u4e00\u500b\u8b8a\u6578\uff0c\u800c = \u662f\u6307\u6d3e\u904b\u7b97\u5b50(Assignment Operator)\uff0c\u4ed6\u6703\u5c07\u7b49\u865f\u53f3\u908a\u7684\u6771\u897f\u6307\u6d3e\u7d66\u7b49\u865f\u5de6\u908a\u7684\u8b8a\u6578\uff0c\u6216\u8005\u8aaa x \u6703\u6307\u5411\u7b49\u865f\u53f3\u908a\u7684\u7269\u4ef6(Object)\u3002

Quote

\u5728Python\u4e2d\uff0c\u4e00\u5207\u90fd\u662f\u7269\u4ef6\u3002

\u8acb\u89c0\u770b\u4e0b\u9762\u7684\u52d5\u756b\u3002

V\u00d8J, Narvent - Memory Reboot (4K Music Video)

\u518d\u4f86\u4ecb\u7d39\u5e7e\u7a2e\u4e0d\u540c\u7684\u8b8a\u6578\u985e\u578b\u3002

a = 114514\nprint(type(a))\nb = 1919810.0\nprint(type(b))\nc = 10e3\nprint(type(c))\nd = \"cheung4843\"\nprint(type(d))\ne = True\nprint(type(e))\nf = False\nprint(type(f))\nh = None\nprint(type(h))\n
type \u544a\u8a34\u4f60\u62ec\u865f\u4e2d\u7684\u6771\u897f\u662f\u4ec0\u9ebc\u985e\u5225(Class)\u3002

b \u8207 c \u90fd\u662f\u6d6e\u9ede\u6578(Float)\uff0c\u4e5f\u5c31\u662f\u5c0f\u6578\u9ede\u7684\u6578\u5b57\uff0c\u800c d \u5247\u662f\u5b57\u4e32\uff0ce \u8207 f \u5247\u662f\u5e03\u6797(Boolean)\uff0c\u800c h \u5247\u662f\u7a7a\u503c\u3002

Note

  1. type(x) \u56de\u50b3 x \u7684\u985e\u5225\u3002
  2. int \u6574\u6578\u3002
  3. float \u6d6e\u9ede\u6578\u3002
  4. str \u5b57\u4e32\u3002
  5. bool \u5e03\u6797\u3002
  6. None \u7a7a\u503c\u3002

Question

  1. print(type(3 + 4.0)) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. print(type(3 + True)) \u6703\u5370\u51fa\u4ec0\u9ebc?

@EditTime : 2024-01-27 16:44

"},{"location":"fundamental/python/variable_and_input/#input","title":"Input","text":"

\u4f46\u662f\u5982\u679c\u6bcf\u6b21\u60f3\u8981\u4fee\u6539 x \u88e1\u9762\u7684\u6771\u897f\uff0c\u96e3\u9053\u90fd\u8981\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4fee\u6539\u55ce?\u80fd\u4e0d\u80fd\u6211\u81ea\u5df1\u4f86\u8f38\u5165\u5462?

x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = int(x) + int(y)\nprint(\"The sum is: \", z)\n

\u5594\u5e79\uff0c\u600e\u9ebc\u4e00\u4e0b\u5b50\u591a\u51fa\u90a3\u9ebc\u591a\u6771\u897f\uff0c\u5225\u614c\uff0c\u6211\u4f86\u89e3\u91cb\uff0c\u4f46\u8acb\u4f60\u5148\u56de\u60f3\u5728\u524d\u4e00\u7bc0\u4e2d\u5b78\u904e\u7684\u6771\u897f\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe input, int, \u90fd\u662f\u51fd\u5f0f\u3002\u800c input \u62ec\u865f\u4e2d\u7684\u5b57\u4e32\u6703\u986f\u793a\u5728\u63a7\u5236\u53f0\u4e2d\u63d0\u793a\u4f60\u8981\u8f38\u5165\u4ec0\u9ebc\uff0cint \u5247\u662f\u628a\u62ec\u865f\u4e2d\u7684\u6771\u897f\u7684\u985e\u5225\u8f49\u63db\u6210\u6574\u6578\u3002

\u90a3\u70ba\u4ec0\u9ebc\u5370\u51fatype(x) \u5f97\u5230 <class 'str'> \u5462?\u4ee3\u8868 x \u662f\u4e00\u500b\u5b57\u4e32\uff0c\u9019\u662f\u56e0\u70ba input \u7e3d\u662f\u5c07\u4f60\u8f38\u5165\u9032\u4f86\u7684\u6771\u897f\u7576\u6210\u5b57\u4e32\uff0c\u4f46\u6211\u60f3\u8981\u8b93 z = x + y \u9019\u500b\u6578\u5b78\u5f0f\u5b50\u6210\u7acb\uff0c\u6240\u4ee5\u9700\u8981\u7528 int \u4f86\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u6574\u6578\u518d\u9032\u884c\u904b\u7b97\u3002

\u90a3\u4e0b\u9762\u9019\u500b\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u662f\u4ec0\u9ebc\u5462?

x = input(\"Enter a number: \")\nprint(type(x))\ny = input(\"Enter another number: \")\nz = x + y\nprint(\"The sum is: \", z)\n

\u6c92\u932f\uff0c\u5b57\u4e32\u7684\u76f8\u52a0\uff0c\u5c31\u662f\u76f8\u9023\u3002\u90a3\u4f60\u8981\u4e0d\u8981\u8a66\u8a66\u770b\u76f8\u6e1b?

\u63a5\u4e0b\u4f86\u6211\u5011\u4f86\u505a\u500b\u6709\u8da3\u7684\u5be6\u9a57\uff0c\u9806\u4fbf\u8a8d\u8b58\u4e00\u4e0b f-string

a = 3.5\nb = int(a)\nprint(f'The value of b is {b}, and its type is {type(b)}')\nc = float(b)\nprint(f'The value of c is {c}, and its type is {type(c)}')\nprint(b == c)\n

\u90a3 f-string \u662f\u4ec0\u9ebc\u6771\u897f\u5462?\u4ed6\u662f\u4e00\u7a2e\u5b57\u4e32\u683c\u5f0f\u5316(String Formatting)\u7684\u65b9\u6cd5\uff0c\u4ed6\u6703\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u5b57\u4e32\uff0c\u4e26\u5c07\u5b57\u4e32\u4e2d\u7684 {} \u66ff\u63db\u6210\u62ec\u865f\u5167\u7684\u6771\u897f\u3002

\u800c\u57f7\u884c\u5b8c\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u4f60\u6703\u767c\u73fe b \u8207 c \u7684\u985e\u5225\u4e0d\u540c\uff0c\u4f46\u4ed6\u5011\u7684\u503c\u537b\u76f8\u540c\uff0c\u9019\u662f\u56e0\u70ba int() \u8207 float() \u90fd\u662f\u5c07\u62ec\u865f\u5167\u7684\u6771\u897f\u8f49\u63db\u6210\u6574\u6578\u8207\u6d6e\u9ede\u6578\uff0c\u800c int() \u6703\u5c07\u6d6e\u9ede\u6578\u7684\u5c0f\u6578\u9ede\u6368\u53bb\uff0c\u800c float() \u5247\u6703\u5c07\u6574\u6578\u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002

\u90a3\u9ebc b == c \u662f\u4ec0\u9ebc\u610f\u601d\u5462?\u5176\u4e2d == \u662f\u6bd4\u8f03\u904b\u7b97\u5b50(Comparison Operator)\uff0c\u4ed6\u6703\u6bd4\u8f03\u7b49\u865f\u5de6\u53f3\u5169\u908a\u7684\u6771\u897f\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5247\u56de\u50b3 True\uff0c\u5426\u5247\u56de\u50b3 False\u3002

"},{"location":"fundamental/python/variable_and_input/#multiple_input","title":"Multiple Input","text":"

\u90a3\u5982\u679c\u6211\u4eca\u5929\u60f3\u8981\u4e00\u6b21\u8f38\u5165\u597d\u5e7e\u500b\u5b57\u4e32\uff0c\u6bcf\u4e00\u500b\u5b57\u4e32\u4ee5\u7a7a\u683c\u4f86\u9694\u958b\u5462? \u4f46\u5728\u9019\u4e4b\u524d\uff0c\u6211\u5011\u5148\u4f86\u770b\u4e00\u500b\u5c0f\u7a0b\u5f0f :

a, b = \"Hello World\".split()\nprint(a)\nprint(b)\n

\u6211\u77e5\u9053\u602a\u602a\u7684\uff0c\u70ba\u4ec0\u9ebc\u5b57\u4e32\u5f8c\u9762\u63a5\u4e86\u4e00\u500b .split() \u5462?\u5728\u9019\u88e1\u4ed6\u5f88\u50cf\u662f\u51fd\u5f0f\uff0c\u4f46\u53c8\u4e0d\u662f\u51fd\u5f0f\uff0c\u90a3\u4ed6\u53eb\u4ec0\u9ebc\u5462?\u4ed6\u88ab\u7a31\u70ba \"Hello World\" \u9019\u500b\u5b57\u4e32\u7684\u65b9\u6cd5(Method)\uff0c\u4f46\u672a\u4f86\u4f60\u53ef\u80fd\u9084\u6703\u807d\u5230\u985e\u5225\u65b9\u6cd5(Class Method)\uff0c\u4ee5\u53ca\u975c\u614b\u65b9\u6cd5(Static Method) \u7b49\u540d\u8a5e\uff0c\u6211\u6015\u4f60\u6703\u641e\u6df7\uff0c\u6240\u4ee5\u4f60\u5c31\u5148\u8a8d\u9017\u9ede\u5f8c\u9762\u7684\u662f\u300c\u65b9\u6cd5\u300d\u5c31\u597d\u4e86\u3002

\u800c .split() \u6703\u628a\u5b57\u4e32\u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\u4e26\u56de\u50b3(\u90a3\u4f60\u60f3\u60f3\u62ec\u865f\u5167\u4ec0\u9ebc\u90fd\u6c92\u653e\uff0c\u9810\u8a2d\u6703\u662f\u4ec0\u9ebc?)\uff0c\u800c\u770b\u770b\u7b49\u865f\u5de6\u908a\uff0c\u6211\u7528 a, b \u53bb\u63a5\u8457\uff0c\u9019\u7a31\u70ba\u958b\u7bb1(Unpacking)

\u90a3\u73fe\u5728\u4f60\u61c9\u8a72\u80fd\u770b\u61c2\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u4e86\uff0c\u56e0\u70ba input() \u4e5f\u6703\u56de\u50b3\u4e00\u500b\u5b57\u4e32\uff0c\u56e0\u6b64\u4ed6\u4e5f\u80fd\u5920\u4f7f\u7528 .split()

a, b = input().split()\nprint(a)\nprint(b)\n

Note

  1. input() \u63a5\u53d7\u8f38\u5165\uff0c\u56de\u50b3\u4e00\u500b\u5b57\u4e32\u3002
  2. int(x) \u5c07 x \u8f49\u63db\u6210\u6574\u6578\u3002
  3. float(x) \u5c07 x \u8f49\u63db\u6210\u6d6e\u9ede\u6578\u3002
  4. str.split() \u4ee5\u62ec\u865f\u5167\u7684\u6771\u897f\u4f86\u5207\u5272\u5b57\u4e32\uff0c\u4e26\u56de\u50b3\u3002

@EditTime : 2024-01-25 22:13

Question

  1. print(int(input()) + int(input())) \u6703\u5370\u51fa\u4ec0\u9ebc?
  2. 124.spilt(\"1\") \u662f\u5408\u6cd5\u7684\u55ce?
  3. bool(0) \u8207 bool(1) \u6703\u56de\u50b3\u4ec0\u9ebc?
  4. \u770b\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u70ba\u4ec0\u9ebc\u5b83\u5011\u7684\u529f\u80fd\u76f8\u540c\uff0ca \u8207 b \u6210\u529f\u4e92\u63db\u4e86\u5462?
a, b = 4, 5\nprint(a, b)\na, b = b, a\nprint(a, b)\n
a, b = 4, 5\nprint(a, b)\ntmp = a\na = b\nb = tmp\nprint(a, b)\n

@EditTime : 2024-01-25 22:17

"},{"location":"fundamental/python/variable_and_input/#practice","title":"Practice","text":"

\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u5b78\u5230\u4e86:

Info

  1. \u5982\u4f55\u4f7f\u7528 print() \u8207 input()\u3002
  2. \u77e5\u9053\u8b8a\u6578\u7684\u6982\u5ff5\u3002
  3. \u77e5\u9053 type(x) \u8207 int(x), float(x) \u7684\u7528\u9014\u3002
  4. \u77e5\u9053 str.split() \u7684\u7528\u9014\u3002
  5. \u77e5\u9053\u5982\u4f55\u4f7f\u7528 f-string\u3002

\u90a3\u73fe\u5728\u4f60\u53ef\u4ee5\u8a66\u8a66\u770b\u4ee5\u4e0b\u7684\u984c\u76ee\u4e86\u3002

ZeroJudge - a001. \u54c8\u56c9

Reference code
word = input()\nprint(f'hello, {word}')\n

@EditTime : 2024-01-27 17:02

"},{"location":"useful_packages/","title":"Useful Packages","text":"

\u5728\u9019\u88e1\uff0c\u5c07\u6703\u5411\u60a8\u4ecb\u7d39\u4e00\u4e9b\u975e\u5e38\u6709\u7528\u7684\u5957\u4ef6\u3002

"},{"location":"useful_packages/tkinter/","title":"Tkinter","text":""},{"location":"blog/archive/2024/","title":"2024","text":""},{"location":"blog/category/blog/","title":"Blog","text":""}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index c752054..870bb7f 100755 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,192 +2,192 @@ https://cheung4843.github.io/ZestAlgo/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/algorithm/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/algorithm/what_is_algo/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/algorithm/basic_algo/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/algorithm/basic_algo/binary_search/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/algorithm/basic_algo/linear_search/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/blog/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/blog/2024/02/06/hello-blog-/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/blog/2024/02/19/%E7%B6%B2%E7%AB%99%E7%9A%84%E6%96%B0%E5%90%8D%E5%AD%97/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/data_structure/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/0/create_env/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/1/hello_world/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/array/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/char/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/floating/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/interger/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/practice/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/practive/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/cpp/2/string/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/classes/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/dictionaries/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/functions/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/lists/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/operators/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/repetition_structures/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/say_hello/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/selection_structures/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/sets/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/tuples/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/fundamental/python/variable_and_input/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/useful_packages/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/useful_packages/tkinter/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/blog/archive/2024/ - 2024-02-24 + 2024-02-26 daily https://cheung4843.github.io/ZestAlgo/blog/category/blog/ - 2024-02-24 + 2024-02-26 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 6ec253d..fc50872 100755 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ