← 返回 Day 3 正文

Day 3 · 练习与清单

Day 003 — 字符串深入:练习与检查清单

✅ 完成一项就勾一项,跟踪你的学习进度


今日完成清单


练习题

练习 1:密码强度检测器

编写一个函数 check_password_strength(password: str),根据以下规则给密码打分:

条件 加分
长度 >= 8 +1
包含大写字母 +1
包含小写字母 +1
包含数字 +1
包含特殊字符(!@#$%^&*) +1

返回分数(0-5)和评价("弱"/"中等"/"强")。

提示:用 any() + 推导式判断字符类别。

python
def check_password_strength(password: str) -> tuple:
    score = 0
    # 你的代码
    
    return score, rating

练习 2:字符串压缩

实现 compress_string(s: str) -> str,对连续重复字符进行压缩:

"aaabbc"    → "a3b2c1"
"abcd"      → "a1b1c1d1"
"aabbaa"    → "a2b2a2"

条件:只能遍历字符串一次(O(n) 时间复杂度)。

练习 3:URL 解析器

实现 parse_url(url: str) -> dict,将 URL 解析为以下格式:

python
parse_url("https://user:pass@example.com:8080/path/to/page?q=hello&lang=zh#section")
# 返回:
# {
#     "protocol": "https",
#     "username": "user",
#     "password": "pass",
#     "hostname": "example.com",
#     "port": "8080",
#     "path": "/path/to/page",
#     "query": {"q": "hello", "lang": "zh"},
#     "fragment": "section"
# }

要求:只使用字符串方法(.split(), .partition(), .find() 等),不能使用 urllib.parse。

练习 4:驼峰命名转换

实现两个函数:

python
camel_to_snake("helloWorld")      # "hello_world"
camel_to_snake("XMLParser")       # "xml_parser"(注意连续大写)

snake_to_camel("hello_world")     # "helloWorld"
snake_to_camel("xml_parser")      # "xmlParser"

提示:用正则或遍历字符串构建。

练习 5:文本对齐工具

实现 justify(text: str, width: int) -> str:

python
justify("The quick brown fox jumps over the lazy dog.", 16)
# 输出:
# "The  quick brown"
# "fox  jumps  over"
# "the   lazy dog."

参考答案提示

先自己尝试,实在卡住了再参考提示

练习 1 提示
python
def check_password_strength(password: str) -> tuple:
    score = 0
    if len(password) >= 8: score += 1
    if any(c.isupper() for c in password): score += 1
    if any(c.islower() for c in password): score += 1
    if any(c.isdigit() for c in password): score += 1
    if any(c in "!@#$%^&*" for c in password): score += 1
    
    ratings = {0: "极弱", 1: "弱", 2: "弱", 3: "中等", 4: "强", 5: "极强"}
    return score, ratings[score]
练习 2 提示 遍历字符串,记录当前字符和计数。当前后字符不同时,输出 `字符+计数` 并重置。