forked from WTFAcademy/WTF-zk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66e83a8
commit 9b2ffde
Showing
11 changed files
with
1,073 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0xaa\n", | ||
"0.123\n", | ||
"123\n", | ||
"a\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 集合定义\n", | ||
"S = {'0xaa', 123, 'a', 0.123}\n", | ||
"# S = {2, 1, 1} \n", | ||
"\n", | ||
"\n", | ||
"# 确定性, 互异性,无序性\n", | ||
"for elem in S:\n", | ||
" print(elem)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"集合S的基数为:4\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 求基数\n", | ||
"S = {'0xaa', 123, 'a', 0.123}\n", | ||
"print(f'集合S的基数为:{len(S)}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\"0xaa\" in S\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 判断元素是否属于集合\n", | ||
"S = {'0xaa', 123, 'a', 0.123}\n", | ||
"if \"0xaa\" in S: \n", | ||
" print(f'\"0xaa\" in S')\n", | ||
"else:\n", | ||
" print(\"Nope\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"s1 和 s2 的差集为 {'0xaa', 1, 2}\n", | ||
"s1 和 s2 的交集为 {3, 4}\n", | ||
"s1 和 s2 的并集为 {1, 2, 3, 4, 5, 6, '0xaa', '0xbb'}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 集合之间的运算\n", | ||
"s1 = {1, 2, 3, 4, \"0xaa\"}\n", | ||
"s2 = {3, 4 ,5, 6, \"0xbb\"}\n", | ||
"\n", | ||
"## 求差集\n", | ||
"print(f's1 和 s2 的差集为 {s1.difference(s2)}')\n", | ||
"\n", | ||
"## 求交集\n", | ||
"print(f's1 和 s2 的交集为 {s1.intersection(s2)}')\n", | ||
"\n", | ||
"## 求并集\n", | ||
"print(f's1 和 s2 的并集为 {s1.union(s2)}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0xaa\n", | ||
"123\n", | ||
"a\n", | ||
"0.123\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 元组有序\n", | ||
"S = ('0xaa', 123, 'a', 0.123)\n", | ||
"for i in S:\n", | ||
" print(i)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"s1 和 s2 的笛卡尔积为:\n", | ||
"(1, 3)\n", | ||
"(1, 4)\n", | ||
"(1, 5)\n", | ||
"(1, 6)\n", | ||
"(1, '0xbb')\n", | ||
"(2, 3)\n", | ||
"(2, 4)\n", | ||
"(2, 5)\n", | ||
"(2, 6)\n", | ||
"(2, '0xbb')\n", | ||
"(3, 3)\n", | ||
"(3, 4)\n", | ||
"(3, 5)\n", | ||
"(3, 6)\n", | ||
"(3, '0xbb')\n", | ||
"(4, 3)\n", | ||
"(4, 4)\n", | ||
"(4, 5)\n", | ||
"(4, 6)\n", | ||
"(4, '0xbb')\n", | ||
"('0xaa', 3)\n", | ||
"('0xaa', 4)\n", | ||
"('0xaa', 5)\n", | ||
"('0xaa', 6)\n", | ||
"('0xaa', '0xbb')\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 求笛卡尔积\n", | ||
"import itertools\n", | ||
"\n", | ||
"s1 = {1, 2, 3, 4, \"0xaa\"}\n", | ||
"s2 = {3, 4 ,5, 6, \"0xbb\"}\n", | ||
"\n", | ||
"s = itertools.product(s1, s2)\n", | ||
"print(f's1 和 s2 的笛卡尔积为:')\n", | ||
"for i in s:\n", | ||
" print(i)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "4b4a18e3", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"加法示例: 12\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"a, b = 7, 5\n", | ||
"sum_result = a + b\n", | ||
"print(f'加法示例: {sum_result}')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "eabe9790", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"减法示例: 2\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"diff_result = a - b\n", | ||
"print(f'减法示例: {diff_result}')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "b695268e", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"乘法示例: 35\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"product_result = a * b\n", | ||
"print(f'乘法示例: {product_result}')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"id": "9ff1c6e1", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"除法示例: 商为 1, 余数为 2\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"\n", | ||
"def euclidean_division(a, b):\n", | ||
" quotient, remainder = divmod(a, b)\n", | ||
" if remainder < 0:\n", | ||
" # 调整余数确保非负\n", | ||
" remainder += abs(b)\n", | ||
" # 调整商使等式仍然成立\n", | ||
" quotient += 1\n", | ||
" return quotient, remainder\n", | ||
"\n", | ||
"quotient, remainder = euclidean_division(a, b)\n", | ||
"print(f'除法示例: 商为 {quotient}, 余数为 {remainder}')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"id": "947c925c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"-1\n", | ||
"-2\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(3 % -2) # 3 - (3//(-2) * -2) = -1\n", | ||
"print(3 // -2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a1997880", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.