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]))
相关推荐
小码编匠4 小时前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
数据智能老司机7 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机8 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机8 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机8 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i8 小时前
drf初步梳理
python·django
每日AI新事件8 小时前
python的异步函数
python
这里有鱼汤9 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
唐青枫11 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez201017 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr