一个程序,给定一个字符串,统计字母a的数量,根据这个程序设计测试用例设计
时间: 2024-09-12 12:04:11 浏览: 48
当你设计一个程序来统计一个字符串中字母"a"的数量时,你可以考虑以下Python示例代码:
```python
def count_a(s):
return s.count('a')
# 测试用例设计:
# 1. 非空字符串,包含多个"a":
test_case_1 = "apple"
expected_output_1 = 1
assert count_a(test_case_1) == expected_output_1, f"Test case 1 failed: Got {count_a(test_case_1)} instead of {expected_output_1}"
# 2. 空字符串,没有"a":
test_case_2 = ""
expected_output_2 = 0
assert count_a(test_case_2) == expected_output_2, f"Test case 2 failed: Got {count_a(test_case_2)} instead of {expected_output_2}"
# 3. 包含其他字符的混合字符串:
test_case_3 = "banana and cat"
expected_output_3 = 3
assert count_a(test_case_3) == expected_output_3, f"Test case 3 failed: Got {count_a(test_case_3)} instead of {expected_output_3}"
# 4. 全部由"a"组成的字符串:
test_case_4 = "aaaaa"
expected_output_4 = 5
assert count_a(test_case_4) == expected_output_4, f"Test case 4 failed: Got {count_a(test_case_4)} instead of {expected_output_4}"
#
阅读全文