Repository navigation
Expand file tree
/
Copy path4_3_code_quality.py
More file actions
129 lines (98 loc) · 4.14 KB
/
Copy path4_3_code_quality.py
File metadata and controls
129 lines (98 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
================================================================
Exercise 4.3: 代码质量练习
对应课程: 4_3_code_quality.py
任务描述
--------
练习类型注解、代码规范和简单测试。
要求
----
实现 4 个函数和 1 个测试类(带完整类型注解):
1. filter_even(numbers: list[int]) -> list[int]
返回列表中的偶数(保持顺序)
2. find_duplicates(items: list[str]) -> list[str]
返回出现超过一次的元素(去重,按首次出现顺序)
3. divide_with_validation(a: int | float, b: int | float)
-> int | float
带校验的除法:
- b == 0 抛 ZeroDivisionError(自定义消息)
- 参数必须是 int 或 float;字符串和 bool 都抛 TypeError
- 两个整数相除返回 float(Python 默认行为)
4. is_snake_case_name(name) -> bool
检查本题约定的简化 ASCII snake_case 风格:
- 小写字母或下划线开头,单个下划线也允许
- 只含小写字母、数字和下划线
这不是 Python 标识符语法检查。
5. DivideTests(unittest.TestCase)
至少写两个 test_ 方法:验证正常除法,以及除数为 0 时抛出异常。
"""
import unittest
# ─────────────────────────────────────────────
# 你的代码写在这里
# ─────────────────────────────────────────────
def filter_even(numbers: list[int]) -> list[int]:
"""返回偶数"""
pass
def find_duplicates(items: list[str]) -> list[str]:
"""返回重复元素"""
pass
def divide_with_validation(a: int | float, b: int | float) -> float:
"""带校验的除法"""
pass
def is_snake_case_name(name: str) -> bool:
"""
判断名称是否符合本题约定的简化 ASCII snake_case 风格。
"""
pass
class DivideTests(unittest.TestCase):
"""请补全两个 test_ 测试方法。"""
def test_normal_division(self):
self.fail("请补全正常除法测试")
def test_zero_raises(self):
self.fail("请补全除零异常测试")
# ─────────────────────────────────────────────
# 测试用例
# ─────────────────────────────────────────────
if __name__ == "__main__":
# Test 1: filter_even
assert filter_even([1, 2, 3, 4, 5, 6]) == [2, 4, 6]
assert filter_even([1, 3, 5]) == []
assert filter_even([]) == []
# Test 2: find_duplicates
assert find_duplicates(["a", "b", "a", "c", "b", "a"]) == ["a", "b"]
assert find_duplicates(["x", "y"]) == []
assert find_duplicates(["same", "same"]) == ["same"]
# Test 3: divide_with_validation 正常
assert divide_with_validation(10, 2) == 5.0
# Test 4: 除零
try:
divide_with_validation(10, 0)
assert False, "应抛 ZeroDivisionError"
except ZeroDivisionError:
pass
# Test 5: 类型校验
try:
divide_with_validation("10", 2)
assert False, "应抛 TypeError"
except TypeError:
pass
try:
divide_with_validation(True, 2)
assert False, "bool 不在本题接受的数字类型中"
except TypeError:
pass
# Test 6: is_snake_case_name
assert is_snake_case_name("user_name") is True
assert is_snake_case_name("userName") is False, "驼峰不符合本题风格"
assert is_snake_case_name("2users") is False, "不能数字开头"
assert is_snake_case_name("user-name") is False, "不能有横线"
assert is_snake_case_name("UserName") is False, "含大写字母"
assert is_snake_case_name("_private") is True, "下划线开头可以"
assert is_snake_case_name("_") is True
# Test 7: 学生编写的 unittest 也必须通过
suite = unittest.defaultTestLoader.loadTestsFromTestCase(DivideTests)
test_result = unittest.TextTestRunner(verbosity=1).run(suite)
assert test_result.wasSuccessful()
print("所有测试通过!")