From 73558ebb2c0388c4db7a701110a0fcbd2c9a0fb5 Mon Sep 17 00:00:00 2001 From: feilong Date: Wed, 22 Dec 2021 00:00:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AC=AC3=E7=AB=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dict.json" | 19 +-- .../dict.md" | 88 ++++++++++ .../dict.py" | 6 +- .../iter.md" | 9 - .../list01.json" | 19 +-- .../list01.md" | 60 +++++++ .../list02.json" | 19 +-- .../list02.md" | 69 ++++++++ .../encode.json" | 16 +- .../encode.md" | 56 +++++++ .../triple01.json" | 16 +- .../triple01.md" | 51 ++++++ .../triple02.json" | 16 +- .../triple02.md" | 55 ++++++ .../3.\346\226\255\350\250\200/assert.json" | 18 +- .../3.\346\226\255\350\250\200/assert.md" | 105 ++++++++++++ .../4.with-as/enter_exit.json" | 15 +- .../4.with-as/enter_exit.md" | 95 +++++++++++ .../4.with-as/withas.json" | 15 +- .../4.with-as/withas.md" | 99 +++++++++++ .../try01.json" | 13 +- .../try01.md" | 103 ++++++++++++ .../str.json" | 21 +-- .../str.md" | 148 +++++++++++++++++ .../lambda.json" | 12 +- .../lambda.md" | 91 ++++++++++ .../file_basic.json" | 18 +- .../8.\346\226\207\344\273\266/file_basic.md" | 61 +++++++ .../file_piece_sha256.json" | 18 +- .../file_piece_sha256.md" | 98 +++++++++++ .../std01.json" | 21 +-- .../std01.md" | 66 ++++++++ .../std02.json" | 15 +- .../std02.md" | 47 ++++++ main.py | 10 +- src/md.py | 157 ++++++++++++++++++ 36 files changed, 1514 insertions(+), 231 deletions(-) create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.md" create mode 100644 "data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.md" create mode 100644 src/md.py diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.json" index 64bb24b..6809e5c 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.json" @@ -1,15 +1,8 @@ { - "one_line": { - "if key != 'w'": [ - "if key == 'w'", - "" - ], - "key: install[key]": [ - "install[key]" - ] - }, - "source": "dict.py", - "depends": [], - "exercise_id": 207, - "type": "code_options" + "author": "huanhuilong", + "source": "dict.md", + "depends": [], + "exercise_id": 207, + "type": "code_options", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.md" new file mode 100644 index 0000000..7203d0c --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.md" @@ -0,0 +1,88 @@ +# Python 字典推导式 + +Python 独步天下的推导式表达式,使用字典推导式过滤,打印非windows系统的 Python 安装介绍 + +```python +if __name__ == '__main__': + install = { + "w": { + "platform": "Window", + "desc": "请下载 Windows 安装包安装:https://www.python.org/downloads/windows/" + }, + "l": { + "platform": "Linux", + "desc": "请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/", + }, + "m": { + "platform": "MacOS", + "desc": "请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装", + } + } + + # TODO(you): 请在此实现 non_windows 赋值代码 + + print("打算最近只用非Windows系统安装Python了:") + print() + for key in non_windows: + target = non_windows[key] + print("安装平台:{}".format(target['platform'])) + print("安装说明:{}".format(target['desc'])) + print() +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +if __name__ == '__main__': + install = { + "w": { + "platform": "Window", + "desc": "请下载 Windows 安装包安装:https://www.python.org/downloads/windows/" + }, + "l": { + "platform": "Linux", + "desc": "请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/", + }, + "m": { + "platform": "MacOS", + "desc": "请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装", + } + } + + non_windows = {key: install[key] for key in install if key != 'w'} + print("打算最近只用非Windows系统安装Python了:") + print() + for key in non_windows: + target = non_windows[key] + print("安装平台:{}".format(target['platform'])) + print("安装说明:{}".format(target['desc'])) + print() +``` + +## 答案 + +```python +non_windows = {key: install[key] for key in install if key != 'w'} +``` + +## 选项 + +### A + +```python +non_windows = {key: install[key] for key in install: if key != 'w'} +``` + +### B + +```python +non_windows = {install[key] for key in install if key != 'w'} +``` + +### C + +```python +non_windows = {key:install[key] for key in install} +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.py" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.py" index b1f6240..3c5bec6 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.py" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/dict.py" @@ -4,7 +4,7 @@ # 描述:Python 独步天下的推导式表达式,使用字典推导式过滤,打印非windows系统的 Python 安装介绍 -def test(): +if __name__ == '__main__': install = { "w": { "platform": "Window", @@ -28,7 +28,3 @@ def test(): print("安装平台:{}".format(target['platform'])) print("安装说明:{}".format(target['desc'])) print() - - -if __name__ == '__main__': - test() diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/iter.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/iter.md" index 39a9dcd..f0b7d25 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/iter.md" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/iter.md" @@ -21,8 +21,6 @@ if __name__ == '__main__': 请选出下列能**正确**实现这一功能的选项。 - - ## template ```python @@ -71,10 +69,3 @@ reading = (book for book in books if len(book) < 4) ```python reading = (book for book in books: if len(book) <= 4) ``` - - - - - - - diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.json" index 67ef170..fee4073 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.json" @@ -1,15 +1,8 @@ { - "one_line": { - "if n % 2 == 0": [ - "if n % 2 != 0", - "where n % 2 == 0" - ], - "for n in list": [ - "for n in list:" - ] - }, - "source": "list01.py", - "depends": [], - "exercise_id": 195, - "type": "code_options" + "author": "huanhuilong", + "source": "list01.md", + "depends": [], + "exercise_id": 195, + "type": "code_options", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.md" new file mode 100644 index 0000000..c0b075a --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list01.md" @@ -0,0 +1,60 @@ +# Python 列表推导式(1) + +Python 独步天下的推导式表达式,使用列表推导式过滤出偶数列表 + +```python +if __name__ == '__main__': + list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + + print('') + print("# 使用列表推导式过滤出偶数") + + # TODO(you): 请在此实现过滤代码 + + for n in even: + print(n) + + print('') +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +if __name__ == '__main__': + list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + print('') + print("# 使用列表推导式过滤出偶数") + even = [n for n in list if n % 2 == 0] + for n in even: + print(n) + + print('') +``` + +## 答案 + +```python +even = [n for n in list if n % 2 == 0] +``` + +## 选项 + +### A + +```python +even = [n for n in list: if n % 2 == 0] +``` + +### B + +```python +even = [n for n in list if n % 2 != 0] +``` + +### C + +```python +even = [for n in list if n % 2 == 0] +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.json" index 90210e6..3f17c05 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.json" @@ -1,15 +1,8 @@ { - "one_line": { - "if '不热' in d['tags']": [ - "if '热' in d['tags']", - "" - ], - "d for d": [ - "for d" - ] - }, - "source": "list02.py", - "depends": [], - "exercise_id": 214, - "type": "code_options" + "author": "huanhuilong", + "source": "list02.md", + "depends": [], + "exercise_id": 195, + "type": "code_options", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.md" new file mode 100644 index 0000000..7bd53d8 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/1.\345\210\227\350\241\250\346\216\250\345\257\274\345\274\217/list02.md" @@ -0,0 +1,69 @@ +# Python 列表推导式(2) + +Python 独步天下的推导式表达式,使用列表推导式过滤不热的天气 + +```python +if __name__ == '__main__': + obj_list = [ + {"key": "day1", "value": "大雨哗啦啦啦啦下", 'tags': ["不热"]}, + {"key": "day2", "value": "很热很热,哥哥不在家,我一个人出去旅行", 'tags': ["热"]}, + {"key": "day3", "value": "阴天有时下肉丸", 'tags': ["不热"]} + ] + + print('') + print("# 过滤出不热的日子") + + # TODO(you): 请在此实现过滤代码 + + for day in non_hot_days: + print("* [{}]: {}".format(day['key'], day['value'])) + + print('') +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +if __name__ == '__main__': + obj_list = [ + {"key": "day1", "value": "大雨哗啦啦啦啦下", 'tags': ["不热"]}, + {"key": "day2", "value": "很热很热,哥哥不在家,我一个人出去旅行", 'tags': ["热"]}, + {"key": "day3", "value": "阴天有时下肉丸", 'tags': ["不热"]} + ] + + print('') + print("# 过滤出不热的日子") + non_hot_days = [d for d in obj_list if '不热' in d['tags']] + for day in non_hot_days: + print("* [{}]: {}".format(day['key'], day['value'])) + + print('') +``` + +## 答案 + +```python +non_hot_days = [d for d in obj_list if '不热' in d['tags']] +``` + +## 选项 + +### A + +```python +non_hot_days = [d for d in obj_list if '热' in d['tags']] +``` + +### B + +```python +non_hot_days = [d for d in obj_list: if '不热' in d['tags']] +``` + +### C + +```python +non_hot_days = [for d in obj_list if '不热' in d['tags']] +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.json" index 842956d..ddd2526 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.json" @@ -1,18 +1,8 @@ { - "multiline": [ - { - "u'二": "'二" - }, - { - "encode": "decode", - "decode": "encode" - }, - { - "'utf-8'": "'utf8'" - } - ], "source": "encode.py", "depends": [], "exercise_id": 107, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" new file mode 100644 index 0000000..aa4dbe8 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" @@ -0,0 +1,56 @@ +# Python 字符编解码 + +二进制只是二进制,取决于怎么编码和解码,unicode 转 utf8 + +```python +if __name__ == '__main__': + unicode_str = u'二进制只是二进制,取决于怎么编码和解码' + print(unicode_str) + + utf8_str = unicode_str.encode('utf-8') + print(utf8_str) + + unicode_str_again = utf8_str.decode('utf-8') + print(unicode_str_again) + assert unicode_str_again == unicode_str +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +if __name__ == '__main__': + unicode_str = u'二进制只是二进制,取决于怎么编码和解码' + print(unicode_str) + + utf8_str = unicode_str.encode('utf-8') + print(utf8_str) + + unicode_str_again = utf8_str.decode('utf-8') + print(unicode_str_again) + assert unicode_str_again == unicode_str +``` + +## 答案 + +```python + +``` + +## 选项 + +### A +```python + +``` + +### B +```python + +``` + +### C +```python + +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.json" index 0bb0d58..3ec3f74 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.json" @@ -1,18 +1,8 @@ { - "one_line": { - "1 if i % 2 == 0 else 0": [ - "0 if i % 2 == 0 else 1" - ], - "even_count == 4": [ - "even_count == 3", - "even_count == 5" - ], - "i % 2 == 0": [ - "i % 2 == 1" - ] - }, "source": "triple01.py", "depends": [], "exercise_id": 215, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.md" new file mode 100644 index 0000000..e1a7977 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple01.md" @@ -0,0 +1,51 @@ +# Python 三元表达式(1) + +Python 没有问号表达式了!使用三元组表达式统计偶数个数 + +```python +if __name__ == '__main__': + pi = [3, 14, 15, 9, 26, 5, 35, 8, 97, 932] + even_count = 0 + for i in pi: + # TODO(you): 请在此实现统计代码 + assert even_count == 4 +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +if __name__ == '__main__': + pi = [3, 14, 15, 9, 26, 5, 35, 8, 97, 932] + even_count = 0 + for i in pi: + even_count += 1 if i % 2 == 0 else 0 + assert even_count == 4 +``` + +## 答案 + +```python +even_count += 1 if i % 2 == 0 else 0 +``` + +## 选项 + +### A + +```python +even_count += if i % 2 == 0: 1 else 0 +``` + +### B + +```python +even_count += 0 if i % 2 == 0 else 1 +``` + +### C + +```python +even_count += 1 if i % 2 != 0 else 0 +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.json" index 961881f..5877d58 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.json" @@ -1,18 +1,8 @@ { - "one_line": { - "if i % 4 == 0 else 1 if i % 2 == 0": [ - "if i % 2 == 0 else 1 if i % 4 == 0" - ], - "2 if i % 4 == 0 else 1 if i % 2 == 0 else 0": [ - "1 if i % 4 == 0 else 2 if i % 2 == 0 else 0" - ], - "even_count == 6": [ - "even_count == 5", - "even_count == 7" - ] - }, "source": "triple02.py", "depends": [], "exercise_id": 203, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.md" new file mode 100644 index 0000000..135256b --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/2.\344\270\211\345\205\203\350\241\250\350\276\276\345\274\217/triple02.md" @@ -0,0 +1,55 @@ +# Python 三元表达式(2) + +使用嵌套的三元组表达式统计数字频率,如果是2的倍数加1,如果是4的倍数加2,否则加0 + +```python + +if __name__ == '__main__': + pi = [3, 14, 15, 9, 26, 5, 35, 8, 97, 932] + even_count = 0 + + for i in pi: + # TODO(You): 请在此实现统计代码 + assert even_count == 6 +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python + +if __name__ == '__main__': + pi = [3, 14, 15, 9, 26, 5, 35, 8, 97, 932] + even_count = 0 + + for i in pi: + even_count += 2 if i % 4 == 0 else 1 if i % 2 == 0 else 0 + assert even_count == 6 +``` + +## 答案 + +```python +even_count += 2 if i % 4 == 0 else 1 if i % 2 == 0 else 0 +``` + +## 选项 + +### A + +```python +even_count += if i % 4 == 0: 2 else: if i % 2 == 0: 1 else: 0 +``` + +### B + +```python +even_count += 1 if i % 2 == 0 else 2 if i % 4 == 0 else 0 +``` + +### C + +```python +even_count += 2 if i % 2 == 0 else 1 if i % 4 == 0 else 0 +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.json" index bf40156..480e889 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.json" @@ -1,20 +1,8 @@ { - "one_line": { - "assert key_value_map is not None": [ - "assert key_value_map is None" - ], - "assert type(key_value_map) == type({})": [ - "assert type(key_value_map) == type([])" - ], - "assert key is not None": [ - "assert key is None" - ], - "assert type(key) == type(\"\")": [ - "assert type(key) is not str" - ] - }, "source": "assert.py", "depends": [], "exercise_id": 110, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.md" new file mode 100644 index 0000000..5f0e69e --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/3.\346\226\255\350\250\200/assert.md" @@ -0,0 +1,105 @@ +# Python 断言 + +所谓断言,就是证明,使用 assert 对输入函数输入参数和函数返回结果分别做前校验和后校验 + +```python + +def check_param(key_value_map, key): + '''参数校验,断言就是对输入参数的一个证明,这些参数必须符合这些要求 + key_value_map: 非空字典 + key:非空字符串 + ''' + # TODO(You): 请在此实现校验代码 + + +def get(key_value_map, key): + check_param(key_value_map, key) + return key_value_map.get(key) + + +def set(key_value_map, key, value): + check_param(key_value_map, key) + key_value_map[key] = value + + +if __name__ == '__main__': + key_value_map = {} + set(key_value_map, "test", {}) + value = get(key_value_map, "test") + print("后校验,返回值必须符合预期") + assert type(value) == type({}) + assert value == {} +``` + +请选出下列对函数 check_param **实现错误**的选项。 + +## template + +```python + +def check_param(key_value_map, key): + '''参数校验,断言就是对输入参数的一个证明,这些参数必须符合这些要求''' + assert key_value_map is not None + assert type(key_value_map) == type({}) + assert key is not None + assert type(key) == type("") + + +def get(key_value_map, key): + check_param(key_value_map, key) + return key_value_map.get(key) + + +def set(key_value_map, key, value): + check_param(key_value_map, key) + key_value_map[key] = value + + +if __name__ == '__main__': + key_value_map = {} + set(key_value_map, "test", {}) + value = get(key_value_map, "test") + print("后校验,返回值必须符合预期") + assert type(value) == type({}) + assert value == {} +``` + +## 答案 + +```python +def check_param(key_value_map, key): + assert key_value_map + assert type(key_value_map) == dict + assert key + assert type(key) == str +``` + +## 选项 + +### A + +```python +def check_param(key_value_map, key): + assert key_value_map is not None + assert type(key_value_map) == type({}) + assert key is not None + assert type(key) == type("") +``` + +### B + +```python +def check_param(key_value_map, key): + assert key_value_map is not None and type(key_value_map) == type({}) + assert key is not None and type(key) == type("") +``` + +### C + +```python +def check_param(key_value_map, key): + assert key_value_map is not None + assert type(key_value_map) == dict + assert key is not None + assert type(key) == str +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.json" index 0843b9b..53fda7b 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.json" @@ -1,17 +1,8 @@ { - "one_line": { - "with TimeSpan() as t": [ - "with TimeSpan()" - ], - "__enter__(self)": [ - "enter(self)" - ], - "__exit__": [ - "exit" - ] - }, "source": "enter_exit.py", "depends": [], "exercise_id": 178, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.md" new file mode 100644 index 0000000..cb6c6df --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/enter_exit.md" @@ -0,0 +1,95 @@ +# Python with-as 语句(2) + +实现一个范围耗时统计类。 实现了 `__enter__` 和 `__exit__` 成员的类,可以通过 with as 语法使用,程序进入和离开范围的时候会自动调用 `__enter__` 和 `__exit__` 方法。 + +```python +import time + + +class TimeSpan: + # TODO(You): 请正确实现计时器的__enter__和__exit成员 + +if __name__ == '__main__': + with TimeSpan() as t: + for i in range(0, 1000): + print(i) +``` + +下列哪个实现是**错误的**? + +## template + +```python +import time + + +class TimeSpan: + def __init__(self) -> None: + self.start = None + + def __enter__(self): + self.end = None + self.start = time.time() + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end = time.time() + print('耗时:{}毫秒'.format((self.end-self.start))) + +if __name__ == '__main__': + with TimeSpan() as t: + for i in range(0, 1000): + print(i) +``` + +## 答案 + +```python +class TimeSpan: + def __enter__(self): + return time.time() + + def __exit__(self, exc_type, exc_val, exc_tb): + end = time.time() + print('耗时:{}毫秒'.format((end-exc_val))) +``` + +## 选项 + +### A + +```python +class TimeSpan: + def __enter__(self): + self.start = time.time() + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end = time.time() + print('耗时:{}毫秒'.format((self.end-self.start))) +``` + +### B + +```python +class TimeSpan: + def __enter__(self): + self.end = None + self.start = time.time() + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end = time.time() + print('耗时:{}毫秒'.format((self.end-self.start))) +``` + +### C + +```python +class TimeSpan: + def __enter__(self): + self.end = None + self.start = time.time() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end = time.time() + print('耗时:{}毫秒'.format((self.end-self.start))) +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.json" index 64b7485..aee0ab5 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.json" @@ -1,17 +1,8 @@ { - "one_line": { - "as f": [ - "" - ], - "with ": [ - "" - ], - "with open(file, 'w') as f": [ - "with f = open(file, 'w')" - ] - }, "source": "withas.py", "depends": [], "exercise_id": 118, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.md" new file mode 100644 index 0000000..34042f7 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/4.with-as/withas.md" @@ -0,0 +1,99 @@ +# Python with-as 语句(1) + +JSON文件读写 + +```python + +import json + + +def load_json(file): + # TODO(You): 实现加载json文件代码 + + +def dump_json(file, obj): + # TODO(You): 实现将dict写入json文件的代码 + +if __name__ == '__main__': + data = { + 'test': 1, + } + dump_json('test.json', data) + load_json('test.json') +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python + +import json + + +def load_json(file): + with open(file, 'r') as f: + return json.loads(f.read()) + + +def dump_json(file, obj): + with open(file, 'w') as f: + f.write(json.dumps(obj, indent=2, ensure_ascii=False)) + +if __name__ == '__main__': + data = { + 'test': 1, + } + dump_json('test.json', data) + load_json('test.json') +``` + +## 答案 + +```python +def load_json(file): + with open(file, 'r') as f: + return json.loads(f.read()) + +def dump_json(file, obj): + with open(file, 'w') as f: + f.write(json.dumps(obj, indent=2, ensure_ascii=False)) +``` + +## 选项 + +### A + +```python +def load_json(file): + with open(file, 'w') as f: + return json.loads(f.read()) + +def dump_json(file, obj): + with open(file, 'r') as f: + f.write(json.dumps(obj, ensure_ascii=False, indent=2)) +``` + +### B + +```python +def load_json(file): + with open(file, 'w') as f: + return json.loads(f.read()) + +def dump_json(file, obj): + with open(file, 'r') as f: + f.write(json.dumps(obj, indent=2, ensure_ascii=False)) +``` + +### C + +```python +def load_json(file): + with open(file, 'r') as f: + return json.loads(f.read()) + +def dump_json(file, obj): + with open(file, 'w') as f: + f.write(json.dumps(obj, ensure_ascii=False, indent=2)) +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.json" index e4b735b..a31e5f3 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.json" @@ -1,15 +1,8 @@ { - "one_line": { - "except Exception as e": [ - "except e", - "except as e" - ], - "logger.error(traceback.format_exc()):": [ - "logger.error(e.stack())" - ] - }, "source": "try01.py", "depends": [], "exercise_id": 237, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.md" new file mode 100644 index 0000000..dfb1504 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/5.\345\274\202\345\270\270\346\215\225\350\216\267\351\242\204\345\244\204\347\220\206/try01.md" @@ -0,0 +1,103 @@ +# Python 异常处理 + +'a.json'文件不存在,下面代码会有异常,请编写异常控制代码,控制异常的最小范围,出现异常正常打印日志和堆栈 + +```python +import json +import traceback +import logging +logger = logging.getLogger(__name__) + + +def load_json(file): + with open(file, 'r') as f: + return json.loads(f.read()) + + +def test(): + # TODO(You): + ret = load_json('a.json') + + +if __name__ == '__main__': + test() +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +import json +import traceback +import logging +logger = logging.getLogger(__name__) + + +def load_json(file): + with open(file, 'r') as f: + return json.loads(f.read()) + + +def test(): + try: + ret = load_json('a.json') + return {'err': 'success', 'result': ret} + except Exception as e: + logger.error(f"load json exception:{str(e)}") + logger.error(traceback.format_exc()) + return {'err': 'exception'} + +if __name__ == '__main__': + test() +``` + +## 答案 + +```python +try: + ret = load_json('a.json') + return {'err': 'success', 'result': ret} +except Exception as e: + logger.error(f"load json exception:{str(e)}") + logger.error(traceback.format_exc()) + return {'err': 'exception'} +``` + +## 选项 + +### A + +```python +try: + ret = load_json('a.json') + return {'err': 'success', 'result': ret} +except Exception(e): + logger.error(f"load json exception:{str(e)}") + logger.error(traceback.format_exc()) + return {'err': 'exception'} +``` + +### B + +```python +try: + ret = load_json('a.json') + return {'err': 'success', 'result': ret} +except e: + logger.error(f"load json exception:{str(e)}") + logger.error(e.format_exc()) + return {'err': 'exception'} +``` + +### C + +```python +try: + ret = load_json('a.json') + return {'err': 'success', 'result': ret} +except Exception as e: + logger.error(f"load json exception:{str(e)}") + logger.error(e.format_exc()) + return {'err': 'exception'} +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.json" index c6dca91..4d4ca54 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.json" @@ -1,23 +1,8 @@ { - "multiline": [ - { - "l.strip() != ''": "l.strip() == ''" - }, - { - "code.split('\n')": "code.split()" - }, - { - "int(left)+int(right))": "left+right", - "int(left)-int(right))": "left-right", - "int(left)*int(right))": "left*right", - "int(left)/int(right))": "left/right" - }, - { - "and right != '0'": "" - } - ], "source": "str.py", "depends": [], "exercise_id": 112, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.md" new file mode 100644 index 0000000..058622b --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/6.\345\255\227\347\254\246\344\270\262\346\226\271\346\263\225/str.md" @@ -0,0 +1,148 @@ +# Python 字符串方法 + +Python 的字符串处理, 一个朴实无华的四则运算计算器,批量计算小学生四则运算表达式 + +```python +import re + +def naive_calc(code): + # TODO(You): 请在此实现简易四则运算 + +def test(): + code = ''' + 1+2 + 3+4 + 5-3 + 4*3 + 10/2 + ''' + naive_calc(code) + +if __name__ == '__main__': + test() +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +import re + +def naive_calc(code): + code_lines = [l for l in code.split('\n') if l.strip() != ''] + + for line in code_lines: + + ret = re.match("\s*(\d+)([\+\-\*\/])(\d+)\s*", line) + left = ret.group(1) + op = ret.group(2) + right = ret.group(3) + if op == '+': + print('{}+{}={}'.format(left, right, int(left)+int(right))) + elif op == '-': + print('{}-{}={}'.format(left, right, int(left)-int(right))) + elif op == '*': + print('{}*{}={}'.format(left, right, int(left)*int(right))) + elif op == '/' and right != '0': + print('{}/{}={}'.format(left, right, int(left)/int(right))) + + +def test(): + code = ''' + 1+2 + 3+4 + 5-3 + 4*3 + 10/2 + ''' + naive_calc(code) + +if __name__ == '__main__': + test() +``` + +## 答案 + +```python +def naive_calc(code): + code_lines = [l for l in code.split('\n') if l.strip() != ''] + for line in code_lines: + ret = re.match("\s*(\d+)([\+\-\*\/])(\d+)\s*", line) + left = ret.group(1) + op = ret.group(2) + right = ret.group(3) + if op == '+': + print('{}+{}={}'.format(left, right, int(left)+int(right))) + elif op == '-': + print('{}-{}={}'.format(left, right, int(left)-int(right))) + elif op == '*': + print('{}*{}={}'.format(left, right, int(left)*int(right))) + elif op == '/' and right != '0': + print('{}/{}={}'.format(left, right, int(left)/int(right))) +``` + +## 选项 + +### A + +```python +def naive_calc(code): + code_lines = [l for l in code.split() if l.strip() == ''] + for line in code_lines: + ret = re.match("\s*(\d+)([\+\-\*\/])(\d+)\s*", line) + left = ret.group(1) + op = ret.group(2) + right = ret.group(3) + + if op == '+': + print('{}+{}={}'.format(left, right, int(left)+int(right))) + elif op == '-': + print('{}-{}={}'.format(left, right, int(left)-int(right))) + elif op == '*': + print('{}*{}={}'.format(left, right, int(left)*int(right))) + elif op == '/' and right != '0': + print('{}/{}={}'.format(left, right, int(left)/int(right))) +``` + +### B + +```python +def naive_calc(code): + code_lines = [l for l in code.split('\n') if l.strip() != ''] + for line in code_lines: + ret = re.match("\s*(\d+)([\+\-\*\/])(\d+)\s*", line) + left = ret.group(1) + op = ret.group(2) + right = ret.group(3) + + if op == '+': + print('{}+{}={}'.format(left, right, left+right)) + elif op == '-': + print('{}-{}={}'.format(left, right, left-right)) + elif op == '*': + print('{}*{}={}'.format(left, right, left*right)) + elif op == '/' and right != '0': + print('{}/{}={}'.format(left, right, left/right)) +``` + +### C + +```python +def naive_calc(code): + code_lines = [l for l in code.split('\n') if l.strip() != ''] + for line in code_lines: + ret = re.match(line, "\s*(\d+)([\+\-\*\/])(\d+)\s*") + left = ret.group(1) + op = ret.group(2) + right = ret.group(3) + + if op == '+': + print('{}+{}={}'.format(left, right, left+right)) + elif op == '-': + print('{}-{}={}'.format(left, right, left-right)) + elif op == '*': + print('{}*{}={}'.format(left, right, left*right)) + elif op == '/' and right != '0': + print('{}/{}={}'.format(left, right, left/right)) +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.json" index f980865..1c62830 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.json" @@ -1,14 +1,8 @@ { - "one_line": { - "lambda item: item['age']": [ - "lambda (item): item['age']", - "lambda item: return item['age']", - "item => item['age']", - "item -> item['age']" - ] - }, "source": "lambda.py", "depends": [], "exercise_id": 108, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.md" new file mode 100644 index 0000000..a66eea8 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/7.lambda\345\207\275\346\225\260/lambda.md" @@ -0,0 +1,91 @@ +# Python 匿名表达式 + +Python 高阶函数编程,使用 lambda 表达式获取key,将list转成dict + +```python + +def list_to_dict(list, key_func): + d = {} + for item in list: + k = key_func(item) + v = item + list = d.get(k) + if list is None: + d[k] = [v] + else: + d[k].append(v) + + return d + + +if __name__ == '__main__': + list = [ + {"name": "alice", "age": 100}, + {"name": "middle", "age": 100}, + {"name": "bob", "age": 200} + ] + # TODO(You): 请在此调用 list_to_dict 方法,将 list 转成dict + print(ret) +``` + +请选出下列**实现错误**的选项。 + +## template + +```python + +def list_to_dict(list, key_func): + d = {} + for item in list: + k = key_func(item) + v = item + list = d.get(k) + if list is None: + d[k] = [v] + else: + d[k].append(v) + + return d + + +if __name__ == '__main__': + list = [ + {"name": "alice", "age": 100}, + {"name": "middle", "age": 100}, + {"name": "bob", "age": 200} + ] + ret = list_to_dict(list, lambda item: item['age']) + print(ret) +``` + +## 答案 + +```python +ret = list_to_dict(list, lambda item: + item['age'] +) +``` + +## 选项 + +### A + +```python +ret = list_to_dict(list, lambda item: item['age']) +``` + +### B + +```python +def get_key(item): + return item['age'] + +ret = list_to_dict(list, get_key) +``` + +### C + +```python +get_key = lambda item: item['age'] +ret = list_to_dict(list, get_key) +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.json" index 5de1b30..1e7bd8c 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.json" @@ -1,20 +1,8 @@ { - "multiline": [ - { - "'w'": "'r'", - "'r'": "'w'" - }, - { - "write": "read", - "read": "write" - }, - { - "with open('/tmp/test.txt', 'w') as f": "f = open('/tmp/test.txt', 'w')", - "with open('/tmp/test.txt', 'r') as f": "f = open('/tmp/test.txt', 'r')" - } - ], "source": "file_basic.py", "depends": [], "exercise_id": 199, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.md" new file mode 100644 index 0000000..066bdc7 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_basic.md" @@ -0,0 +1,61 @@ +# Python 文件读写 + +基本文件读写,写入 "test" 到 "/tmp/test.txt",再次打开读取 + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python + +if __name__ == '__main__': + with open('/tmp/test.txt', 'w') as f: + f.write("test") + with open('/tmp/test.txt', 'r') as f: + print(f.read()) +``` + +## 答案 + +```python +if __name__ == '__main__': + with open('/tmp/test.txt', 'w') as f: + f.write("test") + with open('/tmp/test.txt', 'r') as f: + print(f.read()) +``` + +## 选项 + +### A + +```python +if __name__ == '__main__': + with open('/tmp/test.txt', 'r') as f: + f.write("test") + + with open('/tmp/test.txt', 'w') as f: + print(f.read()) +``` + +### B + +```python +if __name__ == '__main__': + with open('/tmp/test.txt', 'r') as f: + f.write("test") + + with open('/tmp/test.txt', 'r') as f: + print(f.read()) +``` + +### C + +```python +if __name__ == '__main__': + f = open('/tmp/test.txt', 'w'): + f.write("test") + + f = open('/tmp/test.txt', 'r'): + print(f.read()) +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.json" index 8cc031f..683c1ac 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.json" @@ -1,20 +1,8 @@ { - "one_line": { - "break": [ - "continue" - ], - "in_file.read": [ - "in_file.readline" - ], - "piece.hex()": [ - "piece" - ], - "sha256.update": [ - "sha256.append" - ] - }, "source": "file_piece_sha256.py", "depends": [], "exercise_id": 213, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.md" new file mode 100644 index 0000000..3ee3afe --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/8.\346\226\207\344\273\266/file_piece_sha256.md" @@ -0,0 +1,98 @@ +# Python 文件读写 + +一种基于 sha256 的文件分片 hash 计算方式 + +```python +import hashlib + +def file_piece_sha256(in_file_path, piece_size): + sha256 = hashlib.sha256() + with open(in_file_path, "rb") as in_file: + # TODO(You): 请在此实现 hash 计算 + return sha256.digest().hex() + +if __name__ == '__main__': + ret = file_piece_sha256('file_piece_sha256.py', 128) + print("file hash is: ", ret) +``` + +请选出下列能**正确**实现TODO功能的选项。 + +## template + +```python +import hashlib + + +def file_piece_sha256(in_file_path, piece_size): + sha256 = hashlib.sha256() + with open(in_file_path, "rb") as in_file: + while True: + piece = in_file.read(piece_size) + if piece: + sha256.update(piece.hex().encode('utf-8')) + else: + break + return sha256.digest().hex() + +if __name__ == '__main__': + ret = file_piece_sha256('file_piece_sha256.py', 128) + print("file hash is: ", ret) +``` + +## 答案 + +```python +while True: + piece = in_file.read(piece_size) + if piece: + sha256.update(piece.hex().encode('utf-8')) + else: + break +``` + +## 选项 + +### A + +```python +while True: + piece = in_file.read(piece_size) + if piece: + sha256.update(piece.hex().encode('utf-8')) + else: + continue +``` + +### B + +```python +while True: + piece = in_file.readline(piece_size) + if piece: + sha256.update(piece.hex().encode('utf-8')) + else: + continue +``` + +### C + +```python +while True: + piece = in_file.read(piece_size) + if piece: + sha256.update(piece.encode('utf-8')) + else: + continue +``` + +### D + +```python +while True: + piece = in_file.read(piece_size) + if piece: + sha256.append(piece.encode('utf-8')) + else: + continue +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.json" index d186530..255f4c2 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.json" @@ -1,23 +1,8 @@ { - "one_line": { - "type([])": [ - "type({})" - ], - "== True": [ - "== False" - ], - "math.pow(2, 3)": [ - "math.pow(2, 4)" - ], - "== float": [ - "== int" - ], - "datetime.now().year": [ - "datetime.now().year()" - ] - }, "source": "std01.py", "depends": [], "exercise_id": 222, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.md" new file mode 100644 index 0000000..a04dcb1 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std01.md" @@ -0,0 +1,66 @@ +# Python 常用标准库(1) + +sys, os, math, time, date + +请在使用的时候查文档,例如日期文档:https://docs.python.org/zh-cn/3/library/datetime.html#datetime.datetime + +请选出以下所有的 assert **不全部为 True** 的选项 + +## template + +```python + +import sys +import os +import math +import time +from datetime import datetime +if __name__ == '__main__': + assert type(sys.argv) == type([]) + assert os.path.exists(__file__) == True + assert math.pow(2, 3) == 8 + assert type(time.time()) == float + assert datetime.now().year >= 2021 +``` + +## 答案 + +```python +assert type(sys.argv) == type([]) +assert os.path.exists(__file__) == True +assert math.pow(2, 3) == 2+2+2+2 +assert type(time.time()) == int +assert datetime.now().year >= 2121 +``` + +## 选项 + +### A + +```python +assert type(sys.argv) == type([]) +assert os.path.exists(__file__) == True +assert math.pow(2, 3) == 2*2*2 +assert type(time.time()) == float +assert datetime.now().year >= 2021 +``` + +### B + +```python +assert type(sys.argv) == type([]) +assert os.path.exists(__file__) == True +assert math.pow(2, 3) == 8 +assert type(time.time()) == type(2.3) +assert datetime.now().year >= 2021 +``` + +### C + +```python +assert type(sys.argv) == list +assert os.path.exists(__file__) == True +assert math.pow(2, 3) == 8 +assert type(time.time()) == float +assert datetime.now().year >= 2021 +``` diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.json" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.json" index ec03fc0..c078791 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.json" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.json" @@ -1,17 +1,8 @@ { - "one_line": { - "json.dumps": [ - "json.dump" - ], - "json.loads": [ - "json.load" - ], - "== \"test\"": [ - "== 2" - ] - }, "source": "std02.py", "depends": [], "exercise_id": 196, - "type": "code_options" + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.md" new file mode 100644 index 0000000..7393345 --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/9.\345\270\270\347\224\250\346\240\207\345\207\206\345\272\223/std02.md" @@ -0,0 +1,47 @@ +# Python 常用标准库(2) + +json 序列化和反序列化 + +```python +import json +if __name__ == '__main__': + # TODO(You): 请在此实现json序列化和反序列化代码 + assert obj['key']['items'][2] == "test" +``` + +请选出下列能**正确**实现这一功能的选项。 + +## template + +```python +import json +if __name__ == '__main__': + obj = json.loads(json.dumps({"key": {"items": [1, 2, "test"]}})) + assert obj['key']['items'][2] == "test" +``` + +## 答案 + +```python +obj = json.loads(json.dumps({"key": {"items": [1, 2, "test"]}})) +``` + +## 选项 + +### A + +```python +obj = json.load(json.dumps({"key": {"items": [1, 2, "test"]}})) +``` + +### B + +```python +obj = json.loads(json.dump({"key": {"items": [1, 2, "test"]}})) +``` + +### C + +```python +obj = json.load(json.dump({"key": {"items": [1, 2, "test"]}})) +``` diff --git a/main.py b/main.py index ccc10a8..4dcb66c 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,13 @@ from json import load -from src.tree import TreeWalker, load_json, dump_json +from src.tree import TreeWalker +from src.md import MDWalker import os import re if __name__ == '__main__': - walker = TreeWalker("data", "python", "python") - walker.walk() + # walker = TreeWalker("data", "python", "python") + # walker.walk() + + md = MDWalker('data/1.python初阶/3.进阶语法') + md.walk() diff --git a/src/md.py b/src/md.py new file mode 100644 index 0000000..bb75123 --- /dev/null +++ b/src/md.py @@ -0,0 +1,157 @@ +import os +from sys import path, version +from types import new_class +from .tree import load_json, dump_json + + +def simple_list_md_load(p): + with open(p, 'r', encoding='utf-8') as f: + lines = f.readlines() + result = [] + for line in lines: + item = line.strip('\n') + result.append(item) + return result + + +def simple_list_md_dump(p, lines): + with open(p, 'w', encoding='utf-8') as f: + f.write('\n'.join(lines)) + + +def simple_py_load(p): + with open(p, 'r', encoding='utf-8') as f: + lines = f.readlines() + author = '' + titles = [] + contents = [] + codes = [] + space_id = 0 + max_space_id = 0 + for i, line in enumerate(lines): + item = line.strip('\n') + if i == 0: + pass + elif i == 1: + author = item[5:] + elif i == 2: + titles.append(item[5:]) + elif i == 3: + contents.append(item[5:]) + elif item.startswith('#'): + contents.append(item[2:]) + else: + if item.strip() == '': + codes.append({ + 'space_id': space_id, + 'code': item + }) + if max_space_id < space_id: + max_space_id = space_id + space_id += 1 + else: + codes.append({ + 'space_id': -1, + 'code': item + }) + + strip_codes = [] + for line in codes: + if line['space_id'] == 0 or line['space_id'] == max_space_id: + pass + else: + strip_codes.append(line['code']) + + return { + 'author': author, + 'title': '\n'.join(titles), + 'contents': contents, + 'codes': strip_codes, + } + + +class MDWalker(): + + def __init__(self, root) -> None: + self.root = root + + def walk(self): + for base, dirs, files in os.walk(self.root): + for file in files: + if file[-3:] == '.py': + print(file) + py_file = os.path.join(base, file) + py = simple_py_load(py_file) + md_file = os.path.join(base, file.replace('.py', '.md')) + json_file = os.path.join( + base, file.replace('.py', '.json')) + + print(py_file) + print(md_file) + print(json_file) + + if os.path.exists(md_file): + continue + md = [] + self.emit_head(md, py) + self.emit_template(md, py) + self.emit_answer(md, py) + self.emit_options(md, py) + simple_list_md_dump(md_file, md) + + if os.path.exists(json_file): + j = load_json(json_file) + if j.get('one_line'): + del j['one_line'] + if j.get('multiline'): + del j['multiline'] + j['author'] = py['author'] + j['notebook_enable'] = True + dump_json(json_file, j, True, True) + + def emit_head(self, md, py): + title = py['title'] + contents = py['contents'] + codes = py['codes'] + md.append(f'# {title}') + md.append('') + for content in contents: + md.append(content) + md.append('') + md.append('```python') + for code in codes: + md.append(code) + md.append('```') + md.append('') + md.append('请选出下列能**正确**实现这一功能的选项。') + md.append('') + + def emit_template(self, md, py): + codes = py['codes'] + md.append(f'## template') + md.append('') + md.append('```python') + for code in codes: + md.append(code) + md.append('```') + md.append('') + + def emit_answer(self, md, py): + md.append(f'## 答案') + md.append('') + md.append('```python') + md.append('') + md.append('```') + md.append('') + + def emit_options(self, md, py): + md.append(f'## 选项') + md.append('') + + for tag in ['A', 'B', 'C']: + md.append(f'### {tag}') + md.append('') + md.append('```python') + md.append('') + md.append('```') + md.append('') -- GitLab