疯狂Python讲义第五章练习

import random
def subject1(input_list):
    #1.定义一个函数,该函数可接收一个list作为参数,该函数使用直接选择排序对list排序
    for i in range(len(input_list)): 
        min_idx = i 
        for j in range(i+1, len(input_list)): 
            if input_list[min_idx] > input_list[j]: 
                min_idx = j 
        input_list[i], input_list[min_idx] = input_list[min_idx], input_list[i] 
    return input_list

def subject2(input_list):
    #2.定义一个函数,该函数可接收一个list作为参数,该函数使用冒泡排序对list排序
    n = len(input_list)
    for i in range(n):
        for j in range(0, n-i-1):
            if input_list[j] > input_list[j+1] :
                input_list[j], input_list[j+1] = input_list[j+1], input_list[j]
    return input_list

def is_leap(year):
    #3.定义一个is_leap(year)函数,改函数可判断year是否为闰年。若是闰年,则返回True;否则返回False。
    if (year % 4) == 0:
       if (year % 100) == 0:
           if (year % 400) == 0:
               return True
           else:
               return False
       else:
           return True
    else:
       return False

def count_str_char(my_str):
    #4.定义一个count_str_char(my_str)函数,
    #该函数返回参数字符串中包含
    #多少个数字、多少个英文字母、多少个空白字符、多少个其他字符
    num_str = '0123456789'
    num_str_num = 0
    char_str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    char_str_num = 0
    blank_str = '\f\n\r\t\v '
    blank_str_num = 0
    other_str_num = 0;
    for chr in my_str:
        if num_str.find(chr)>=0:
            num_str_num +=1
            continue
        if char_str.find(chr)>=0:
            char_str_num +=1
            continue
        if blank_str.find(chr)>=0:
            blank_str_num +=1
            continue
        other_str_num += 1
    print("数字:",num_str_num,"英文:",char_str_num,"空白:",blank_str_num,"其他:",other_str_num)

def subject5(n):
    #定义一个fn(n)函数,改函数返回1~n的立方和,即求1+2*2*2+3*3*3+···+n*n*n.
    ret = 0
    for i in range(1,n+1):
        ret += (i*i*i)
    return ret

def subject6(n):
    #定义一个fn(n)函数,改函数返回n的阶乘
    ret = 1
    for i in range(1,n+1):
        ret *=i
    return ret

def subject7(input_list):
    #定义一个函数,该函数可接收一个list作为参数,该函数用于去除list中重复的元素
    return list(set(input_list))

def subject8(n):
    #定义一个fn(n)函数,该函数返回一个包含n个不重复的0~100之间整数的元祖
    return tuple(random.sample(range(100),n))

def subject9(n):
    #定义一个fn(n)函数,该函数返回一个包含n个不重复的大写字母的元祖
    return tuple(random.sample([chr(i) for i in range(ord('A'),ord('Z')+1)],n))

def subject10(n):
    #定义一个fn(n)函数,其中n表示输入n行n列的矩阵(数的方阵)。
    #在输出时,先输出n行n列的矩阵,再输出改矩阵的转置形式。
    value = [i for i in range(1,n*n+1)]
    print("-------------------------------")
    for i in range(n):
        for j in range(n):
            print(value[i*n+j],end=" ")
        print("")
    print("-------------------------------")
    for i in range(n):
        for j in range(n):
            print(value[i+j*n],end=" ")
        print("")
    print("-------------------------------")

if __name__ == '__main__':

    input_list = [64, 25, 12, 22, 11]
    print(subject1(input_list))
    
    input_list = [64, 25, 12, 22, 11]
    print(subject2(input_list))

    print("1990是闰年?",is_leap(1990))
    print("2020是闰年?",is_leap(2020))

    my_str = "sdfdsj128kldjf83sd0f238sdjf4f9fsokfj983 dskf093jf 9023rjd983 \n \f"
    count_str_char(my_str)
    
    print(subject5(5))
    print(subject6(6))

    input_list = [64, 25, 12, 22, 11,64,25,12]
    print(subject7(input_list))

    print(subject8(8))
    print(subject9(9))
    subject10(4)

 

你可能感兴趣的:(python)