第五章课后练习

本文深入探讨了选择排序和冒泡排序算法的实现,并介绍了如何判断闰年、统计字符串中字符类型数量、计算数字立方和及阶乘、去除列表重复元素、生成不重复随机数及随机大写字母序列、创建并转置矩阵等实用数据处理技巧。
摘要由CSDN通过智能技术生成
1
def choose_sort(list):
    list_len = len(list)
    for i in range(0, list_len):
        for j in range(i + 1, list_len):
            if list[i] > list[j]:
                list[i], list[j] = list[j], list[i]
list1 = [3, 6, 1, 8, 5, -20, 100, 50, 200]
choose_sort(list1)
print(list1)
2
def bubble_sort(list):
    list_len = len(list)
    for i in range(0, list_len):
        is_sorted = True
        for j in range(0, list_len - i - 1):
            if list[j] > list[j + 1]:
                list[j], list[j + 1] = list[j + 1], list[j]
                is_sorted = False
        if is_sorted: return
list1 = [3, 6, 1, 8, 5, -20, 100, 50, 200, -32, 123]
bubble_sort(list1)
print(list1)

3
def is_leap(year):
    year = int(year)
    if (year % 4 == 0) and (year % 100 != 0):
        return True
    elif year % 400 == 0:
        return True
    else:
        return False
while(True):
    year = input("请输入一个年份:")
    if year == 'exit':
        import sys
        sys.exit(0)
    print("%s是闰年吗? %s" % (year, is_leap(year)))
4
def count_str_char(string):
    char_num, digit_num, space_num, other_num = 0, 0, 0, 0
    for c in string:
        if c.isdigit(): digit_num += 1
        elif c.isalpha(): char_num += 1
        elif c.isspace(): space_num += 1
        else: other_num += 1
    return char_num, digit_num, space_num, other_num
while(True):
    string = input("请输入一个字符串:")
    if string == 'exit':
        import sys
        sys.exit(0)
    char_num, digit_num, space_num, other_num = count_str_char(string)
    print('字母个数', char_num)
    print('数字个数', digit_num)
    print('空白个数', space_num)
    print('其他字母个数', other_num)
5
def fn(n):
    if n < 2:
        exit()
    sum = 1
    for i in range(2, n+1):
        sum += i ** 3
    return sum
n = int(input("请输入数字:"))
result = fn(n)
print("1~%d的立方和是%d" % (n, result))
6
def fn(n):
    result_list = [1]
    for i in range(n):
        result_list.append(result_list[-1] * len(result_list))
    return result_list[-1]

n = int(input("请输入数字:"))
result = fn(n)
print("%d的阶乘是%d" % (n, result))
7
def remove_duplicate(source_list):
    new_list = list({}.fromkeys(source_list).keys())
    return new_list
    # 另一种方法
#    new_list = []
#    [new_list.append(i) for i in source_list if not i in new_list]
#    return new_list

length = int(input("请输入列表的长度:"))
my_list = []
for i in range(length):
    my_list.append(input('请输入字符串:'))
print(remove_duplicate(my_list))
8
import random

def fn(n):
    i, tmp_list = 0, []
    while True:
        num = random.randint(0, 100)
        # 如果随机数不包含在列表中,则保存
        if num not in tmp_list:
            tmp_list.append(num)
            i += 1
        if i == n:
            break
    # 将列表转成元组返回
    return tuple(tmp_list)
n = int(input("请输入整数n:"))
print(fn(n))
9
import random

def fn(n):
    i, tmp_list = 0, []
    while True:
        num = random.randint(65, 65 + 25)
        # 如果随机数不包含在列表中,则保存
        if chr(num) not in tmp_list:
            tmp_list.append(chr(num))
            i += 1
        if i == n:
            break
    # 将列表转成元组返回
    return tuple(tmp_list)
n = int(input("请输入整数n:"))
print(fn(n))
10
import random

def fn(n):
    # 输出矩阵
    for i in range(n):
        for j in range(n):
            print(' %2d ' % (i * n + j + 1), end="")
        print()
    print('-' * (4 * n))
    # 输出转置矩阵
    for i in range(n):
        for j in range(n):
            print(' %2d ' % (j * n + i + 1), end="")
        print()

n = int(input("请输入整数n:"))
fn(n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值