python第四次作业

说明

python是我的一门跨学科选修课,属于是错选了,哈哈无所谓

题目


参考代码

python 复制代码
# 40
def find_max(numbers):
    # 将输入的字符串按逗号分割成列表,并转换为整数类型
    nums = [int(num) for num in numbers.split(',')]
    # 使用内置的max函数找到列表中的最大值
    max_num = max(nums)
    return max_num

# 获取用户输入的三个数字
input_numbers = input("")
# 调用函数并输出结果
print(find_max(input_numbers))

# 41
def find_strange_license_plate():
    # 遍历四位数的所有可能车牌号
    for num in range(32, 99):
        # 计算该数的平方
        square = num ** 2
        # 将平方数转换为字符串
        square_str = str(square)
        # 判断车牌号是否满足条件:从左至右一个数字比一个数字大
        if square_str == ''.join(sorted(square_str)) and len(set(square_str)) == 4:
            return square
# 调用函数并获取结果
license_plate= find_strange_license_plate()
# 输出结果
print(license_plate)

# 42
def print_words(sentence):
    # 使用 split() 函数将句子按空格分割成单词列表
    words = sentence.split()
    # 逐行打印每个单词
    for word in words:
        print(word)

# 从键盘输入句子
input_sentence = input()
# 调用函数逐行打印单词
print_words(input_sentence)

# 43
def count_characters(input_string):
    # 初始化各类字符的计数器
    english_count = 0
    digit_count = 0
    space_count = 0
    other_count = 0

    # 遍历输入字符串中的每个字符
    for char in input_string:
        # 判断字符类型并增加相应计数器的值
        if char.isalpha():
            english_count += 1
        elif char.isdigit():
            digit_count += 1
        elif char.isspace():
            space_count += 1
        else:
            other_count += 1

    return english_count, digit_count, space_count, other_count

# 从键盘输入一行字符
input_string = input()
# 调用函数统计字符数量
english_count, digit_count, space_count, other_count = count_characters(input_string)
# 输出统计结果
print("{},{},{},{}".format(english_count, digit_count, space_count, other_count))

# 44
def count_alpha_frequency(input_string):
    # 初始化字母频率字典
    frequency = {}

    # 遍历输入字符串中的每个字符
    for char in input_string:
        # 判断字符是否为字母,并转换为小写
        if char.isalpha():
            char = char.lower()
            # 如果该字母已经在字典中,则增加其频率;否则将其添加到字典中并初始化频率为1
            if char in frequency:
                frequency[char] += 1
            else:
                frequency[char] = 1

    # 将字母频率字典按频率值降序排序
    sorted_frequency = sorted(frequency.items(), key=lambda x: x[1], reverse=True)

    return sorted_frequency

# 从键盘输入一行字符
input_string = input()
# 调用函数统计字母频率
alpha_frequency = count_alpha_frequency(input_string)
# 输出统计结果

for item in alpha_frequency:
    print("{},{}".format(item[0], item[1]))
相关推荐
喵手6 分钟前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_9449347310 分钟前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy17 分钟前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖1 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472462 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
肖永威2 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ2 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha2 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy2 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
lly2024062 小时前
C++ 文件和流
开发语言